W3cubDocs

/Babel 7

@babel/plugin-proposal-dynamic-import

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

Transforms import() expressions to non-ESM module formats.

When (not) to use this plugin

If you are using a bundler, such as Webpack, Rollup or Parcel, you should not use this plugin and let your bundler handle import() expressions.

You should use this plugin if:

This plugin must be used with one of the module transform plugins mentioned above.

Example

import("jquery").then($ => {});

will be transformed to

  • CommonJS
  • AMD
  • SystemJS
Promise.resolve()
  .then(() => _interopRequireWildcard(require("jquery")))
  .then(($) => {});

Installation

  • npm
  • Yarn
  • pnpm
npm install --save-dev @babel/plugin-proposal-dynamic-import

Usage

{
  "plugins": [
    "@babel/plugin-proposal-dynamic-import",
    "@babel/plugin-transform-modules-commonjs"
  ]
}

Via CLI

babel --plugins=@babel/plugin-proposal-dynamic-import,@babel/plugin-transform-modules-amd script.js

Via Node API

require("@babel/core").transformSync("code", {
  plugins: [
    "@babel/plugin-proposal-dynamic-import",
    "@babel/plugin-transform-modules-systemjs"
  ],
});

References

© 2014-present Sebastian McKenzie
Licensed under the MIT License.
https://babeljs.io/docs/babel-plugin-proposal-dynamic-import/