@babel/plugin-syntax-dynamic-import

syntax-dynamic-import - 图1info

This plugin is included in @babel/preset-env, in ES2020.

syntax-dynamic-import - 图2tip

You can safely remove this plugin from your Babel config if using @babel/core 7.8.0 or above.

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-syntax-dynamic-import
  1. yarn add --dev @babel/plugin-syntax-dynamic-import
  1. pnpm add --save-dev @babel/plugin-syntax-dynamic-import

Usage

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-syntax-dynamic-import"]
  3. }

Via CLI

Shell

  1. babel --plugins @babel/plugin-syntax-dynamic-import script.js

Via Node API

JavaScript

  1. require("@babel/core").transformSync("code", {
  2. plugins: ["@babel/plugin-syntax-dynamic-import"],
  3. });

Working with Webpack and @babel/preset-env

Currently, @babel/preset-env is unaware that using import() with Webpack relies on Promise internally. 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:

webpack.config.js

  1. const config = {
  2. entry: [
  3. "core-js/modules/es.promise",
  4. "core-js/modules/es.array.iterator",
  5. path.resolve(__dirname, "src/main.js"),
  6. ],
  7. // ...
  8. };

or

src/main.js

  1. import "core-js/modules/es.promise";
  2. import "core-js/modules/es.array.iterator";
  3. // ...

This is the same for core-js@2, except the imports paths are slightly different:

webpack.config.js

  1. const config = {
  2. entry: [
  3. "core-js/modules/es6.promise",
  4. "core-js/modules/es6.array.iterator",
  5. path.resolve(__dirname, "src/main.js"),
  6. ],
  7. // ...
  8. };

or

src/main.js

  1. import "core-js/modules/es6.promise";
  2. import "core-js/modules/es6.array.iterator";
  3. // ...