codecamp

@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

input.js

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

will be transformed to

  • CommonJS
  • AMD
  • SystemJS

output.js

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

Installation

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

Usage

With a configuration file (Recommended)

babel.config.json

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

Via CLI

Shell

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

Via Node API

JavaScript

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

References


@babel/plugin-proposal-numeric-separator
@babel/plugin-proposal-export-namespace-from
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Babel 杂项

Babel 工具软件包

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }