@babel/plugin-transform-modules-systemjs
这个插件包含在 @babel/preset-env 的 modules 选项下
这个插件可以将 ECMAScript 模块转换为 SystemJS。请注意,只有 import/export 语句(import "./mod.js")和 import 表达式(import('./mod.js'))的 语法 会被转换,因为 Babel 并不了解 ECMAScript 模块和 SystemJS 实现之间不同的解析算法。
🌐 This plugin transforms ECMAScript modules to SystemJS. Note that only the syntax of import/export statements (import "./mod.js") and import expressions (import('./mod.js')) is transformed, as Babel is unaware of different resolution algorithms between implementations of ECMAScript modules and SystemJS.
示例
🌐 Example
在
export default 42;
出
System.register([], function(_export, _context) {
return {
setters: [],
execute: function() {
_export("default", 42);
},
};
});
要支持动态导入(import('./lazy.js').then(m => ...)),请在这个插件之前启用 @babel/plugin-syntax-dynamic-import 插件。
🌐 For dynamic import support (import('./lazy.js').then(m => ...)), enable the @babel/plugin-syntax-dynamic-import plugin before this one.
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/plugin-transform-modules-systemjs
yarn add --dev @babel/plugin-transform-modules-systemjs
pnpm add --save-dev @babel/plugin-transform-modules-systemjs
bun add --dev @babel/plugin-transform-modules-systemjs
用法
🌐 Usage
使用配置文件(推荐)
🌐 With a configuration file (Recommended)
没有选项:
🌐 Without options:
{
"plugins": ["@babel/plugin-transform-modules-systemjs"]
}
有选项:
🌐 With options:
{
"plugins": [
[
"@babel/plugin-transform-modules-systemjs",
{
// outputs SystemJS.register(...)
"systemGlobal": "SystemJS"
}
]
]
}
通过 CLI
🌐 Via CLI
babel --plugins @babel/plugin-transform-modules-systemjs script.js
通过 Node API
🌐 Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-modules-systemjs"],
});
选项
🌐 Options
allowTopLevelThis
boolean,默认值为 false。
已添加于:v7.8.0
🌐 Added in: v7.8.0
当这个选项是 true 时,Babel 会将顶层 this(在 ES 模块中总是 undefined)转换为 void 0。
🌐 When this option is true, Babel will transform top level this (always undefined in ES Modules) to void 0.
moduleIds
boolean 默认为 !!moduleId
已添加于:v7.9.0
🌐 Added in: v7.9.0
启用模块 ID 生成。
🌐 Enables module ID generation.
moduleId
string
已添加于:v7.9.0
🌐 Added in: v7.9.0
用于模块的硬编码 ID。不能与 getModuleId 一起使用。
🌐 A hard-coded ID to use for the module. Cannot be used alongside getModuleId.
getModuleId
(name: string) => string
已添加于:v7.9.0
🌐 Added in: v7.9.0
给定 babel 生成的模块名称,返回要使用的名称。返回一个假值将使用原始的 name。
🌐 Given the babel-generated module name, return the name to use. Returning
a falsy value will use the original name.
moduleRoot
string
已添加于:v7.9.0
🌐 Added in: v7.9.0
包含在生成的模块名称上的根路径。
🌐 A root path to include on generated module names.