-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add additional information for plugin-syntax-dynamic-import #2197
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,14 @@ sidebar_label: syntax-dynamic-import | |
original_id: babel-plugin-syntax-dynamic-import | ||
--- | ||
|
||
`babel-plugin-syntax-dynamic-import` is needed to enable support for parsing `import()` | ||
|
||
**Usage notes:** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think this applies for v6?
Should we link to https://github.com/airbnb/babel-plugin-dynamic-import-node? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😆 oops! Yeah maybe something like:
@nicolo-ribaudo Just just verify, can you confirm that babel-preset-env (v6) does not automatically handle this? If not, I'll strike that line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, it's only supported in Babel 7 (7.5.0) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! I'll strike that line. |
||
|
||
1. If you are using `babel-preset-env`, it's automatically handled | ||
2. If you are using Webpack or Rollup, you shouldn't transpile `import()` with Babel and let the bundler handle it for you | ||
3. Otherwise, you need `babel-plugin-proposal-dynamic-import` | ||
|
||
## Installation | ||
|
||
```sh | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
--- | ||
id: version-7.8.0-babel-plugin-syntax-dynamic-import | ||
title: @babel/plugin-syntax-dynamic-import | ||
sidebar_label: syntax-dynamic-import | ||
original_id: babel-plugin-syntax-dynamic-import | ||
--- | ||
|
||
`@babel/plugin-syntax-dynamic-import` is needed to enable support for parsing `import()` | ||
|
||
This plugin is enabled by default since Babel 7.8.0, so you shouldn't need to enable this plugin separately. | ||
|
||
**Usage notes:** | ||
|
||
1. If you are using `@babel/preset-env`, it's automatically handled | ||
2. If you are using Webpack or Rollup, you shouldn't transpile `import()` with Babel and let the bundler handle it for you | ||
3. Otherwise, you need `@babel/plugin-proposal-dynamic-import` | ||
|
||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-syntax-dynamic-import | ||
``` | ||
|
||
## Usage | ||
|
||
### With a configuration file (Recommended) | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-syntax-dynamic-import"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins @babel/plugin-syntax-dynamic-import script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-syntax-dynamic-import"], | ||
}); | ||
``` | ||
|
||
## Working with Webpack and @babel/preset-env | ||
|
||
Currently, `@babel/preset-env` is unaware that using `import()` with [Webpack relies on `Promise` internally](https://webpack.js.org/guides/code-splitting/#dynamic-imports). Environments which do not have builtin support for `Promise`, like Internet Explorer, will require both the `promise` and `iterator` polyfills be added manually. | ||
|
||
For example, with `core-js@3`: | ||
|
||
```js | ||
// webpack config | ||
const config = { | ||
entry: [ | ||
"core-js/modules/es.promise", | ||
"core-js/modules/es.array.iterator", | ||
path.resolve(__dirname, "src/main.js"), | ||
], | ||
// ... | ||
}; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
// src/main.js | ||
import "core-js/modules/es.promise"; | ||
import "core-js/modules/es.array.iterator"; | ||
|
||
// ... | ||
``` | ||
|
||
This is the same for `core-js@2`, except the imports paths are slightly different: | ||
|
||
```js | ||
// webpack config | ||
const config = { | ||
entry: [ | ||
"core-js/modules/es6.promise", | ||
"core-js/modules/es6.array.iterator", | ||
path.resolve(__dirname, "src/main.js"), | ||
], | ||
// ... | ||
}; | ||
``` | ||
|
||
or | ||
|
||
```js | ||
// src/main.js | ||
import "core-js/modules/es6.promise"; | ||
import "core-js/modules/es6.array.iterator"; | ||
|
||
// ... | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gunn4r Can you rebase on upstream? We have removed versioned_docs so I think only this section is required.