Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/plugin-syntax-dynamic-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ title: @babel/plugin-syntax-dynamic-import
sidebar_label: 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`


Copy link
Contributor

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.

## Installation

```sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using babel-preset-env, it's automatically handled

I don't think this applies for v6?

Otherwise, you need babel-plugin-proposal-dynamic-import

babel-plugin-proposal-dynamic-import doesn't exist :)

Should we link to https://github.com/airbnb/babel-plugin-dynamic-import-node?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 oops!

Yeah maybe something like:

Otherwise, consider using babel-plugin-dynamic-import-node.

@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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it's only supported in Babel 7 (7.5.0)

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

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
Expand Down
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";

// ...
```