This plugin transforms ES2015 modules to AMD (Asynchronous Module Definition) format. It converts ES6 import/export statements to AMD define() calls and provides comprehensive module transformation capabilities including dynamic import support, module interoperability, and configurable transformation options.
npm install --save-dev @babel/plugin-transform-modules-amd// Plugin registration (CommonJS)
const transformModulesAmd = require("@babel/plugin-transform-modules-amd");
// Plugin registration (ES modules)
import transformModulesAmd from "@babel/plugin-transform-modules-amd";Add to your Babel configuration:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-transform-modules-amd", {
// Optional configuration
allowTopLevelThis: false,
strict: true,
noInterop: false
}]
]
};Input ES6 module:
import { helper } from "./utils";
export const result = helper("data");Output AMD module:
define(["exports", "./utils"], function (_exports, _utils) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.result = void 0;
const result = _exports.result = (0, _utils.helper)("data");
});The main export is a Babel plugin factory function that accepts configuration options.
interface Options {
/** Allow top-level `this` references (default: false) */
allowTopLevelThis?: boolean;
/** Import interop strategy for cross-module compatibility */
importInterop?: "none" | "babel" | "node" | ((source: string, filename?: string) => "none" | "babel" | "node");
/** Enable loose transformation mode (default: false) */
loose?: boolean;
/** Disable module interop transformations (default: false) */
noInterop?: boolean;
/** Enable strict mode in output (default: false) */
strict?: boolean;
/** Alternative name for strict mode */
strictMode?: boolean;
}
interface BabelPlugin {
name: string;
pre?(): void;
visitor: {
[key: string]: any;
};
}
/**
* Babel plugin factory function
* @param api - Babel API object with assertVersion method
* @param options - Plugin configuration options
* @returns Babel plugin object with transformation logic
*/
declare function plugin(api: any, options?: Options): BabelPlugin;
export default plugin;
export { Options };Transforms ES6 module syntax to AMD format:
Converts ES6 dynamic import() expressions to AMD-compatible Promise-based require() calls:
Input:
import("./module").then(mod => mod.default());Output:
new Promise((resolve, reject) =>
require(["./module"], imported => resolve(imported), reject)
);allowTopLevelThis (boolean, default: false)
this at the top level of modulesthis refers to the global objectimportInterop ("babel" | "node" | "none")
"babel": Use Babel's interop helpers (default)"node": Use Node.js-style interop"none": No interop transformationloose (boolean, default: false)
noInterop (boolean, default: false)
strict/strictMode (boolean, default: false)
The plugin operates as a standard Babel plugin with:
CallExpression|ImportExpression: Handles dynamic import transformationsProgram.exit: Main module transformation logicThe plugin includes validation for:
Common transformation scenarios are handled gracefully: