@babel/plugin-transform-modules-amd
历史
| 版本 | 变更 |
|---|---|
v7.14.0 | 实现了 importInterop 选项 |
这个插件包含在 @babel/preset-env 的 modules 选项下
这个插件会把 ECMAScript 模块转换成 AMD。注意,只有 import/export 语句(import "./mod.js")和 import 表达式(import('./mod.js'))的语法会被转换,因为 Babel 并不清楚 ECMAScript 模块和 AMD 在实现上的不同解析算法。
🌐 This plugin transforms ECMAScript modules to AMD. 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 the different resolution algorithms between implementations of ECMAScript modules and AMD.
示例
🌐 Example
在
export default 42;
出
define(["exports"], function(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.default = 42;
});
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/plugin-transform-modules-amd
yarn add --dev @babel/plugin-transform-modules-amd
pnpm add --save-dev @babel/plugin-transform-modules-amd
bun add --dev @babel/plugin-transform-modules-amd
用法
🌐 Usage
使用配置文件(推荐)
🌐 With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-transform-modules-amd"]
}
通过 CLI
🌐 Via CLI
babel --plugins @babel/plugin-transform-modules-amd script.js
通过 Node API
🌐 Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-modules-amd"],
});
选项
🌐 Options
allowTopLevelThis
boolean,默认值为 false。
当这个选项是 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.
对于这里没有列出的选项,请查看 @babel/plugin-transform-modules-commonjs 的选项。
🌐 For options not listed here, see options for @babel/plugin-transform-modules-commonjs.