Skip to main content

@babel/plugin-transform-modules-amd

历史
版本变更
v7.14.0实现了 importInterop 选项
info

这个插件包含在 @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

在

JavaScript
export default 42;

出

JavaScript
define(["exports"], function(exports) {
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true,
});

exports.default = 42;
});

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-modules-amd

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-modules-amd"]
}

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/plugin-transform-modules-amd script.js

通过 Node API​

🌐 Via Node API

JavaScript
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.