@babel/plugin-transform-modules-amd
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
History
| Version | Changes |
|---|---|
v7.14.0 | Implemented the importInterop option |
信息
此插件已包含在 @babel/preset-env 的 modules 选项中
本插件可将 ECMAScript 模块转换为 AMD 格式。请注意,仅会转换 import/export 语句(import "./mod.js")和 import 表达式(import('./mod.js'))的语法,因为 Babel 不了解 ECMAScript 模块与 AMD 在解析算法上的实现差异。
示例
输入
JavaScript
export default 42;
输出
JavaScript
define(["exports"], function(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.default = 42;
});
安装
- 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
用法
通过配置文件(推荐)
babel.config.json
{
"plugins": ["@babel/plugin-transform-modules-amd"]
}
通过命令行
Shell
babel --plugins @babel/plugin-transform-modules-amd script.js
通过 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-modules-amd"],
});
配置选项
moduleIds
boolean 默认值:!!moduleId
添加于:v7.9.0
启用模块 ID 生成功能。
moduleId
string
添加于:v7.9.0
用于模块的硬编码 ID。不可与 getModuleId 同时使用。
getModuleId
(name: string) => string
添加于:v7.9.0
接收 Babel 生成的模块名,返回要使用的名称。返回假值将保留原始 name。
moduleRoot
string
添加于:v7.9.0
要包含在生成模块名称中的根路径。
未列出的选项请参阅 @babel/plugin-transform-modules-commonjs 的选项。