@babel/plugin-transform-modules-amd
History
| Version | Changes |
|---|---|
v7.14.0 | Implemented the importInterop option |
This plugin is included in @babel/preset-env under the modules option
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
In
export default 42;
Out
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"]
}
Via CLI
babel --plugins @babel/plugin-transform-modules-amd script.js
Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-modules-amd"],
});
Options
allowTopLevelThis
boolean, defaults to false.
When this option is true, Babel will transform top level this (always undefined in ES Modules) to void 0.
moduleIds
boolean defaults to !!moduleId
Added in: v7.9.0
Enables module ID generation.
moduleId
string
Added in: v7.9.0
A hard-coded ID to use for the module. Cannot be used alongside getModuleId.
getModuleId
(name: string) => string
Added in: v7.9.0
Given the babel-generated module name, return the name to use. Returning
a falsy value will use the original name.
moduleRoot
string
Added in: v7.9.0
A root path to include on generated module names.
For options not listed here, see options for @babel/plugin-transform-modules-commonjs.