Babel Plugin Transform ES2015 Modules to AMD (Lazy)
raw JSON → 2.0.1 verified Sat Apr 25 auth: no javascript
A Babel transform plugin (v2.0.1, stable) that converts ES2015 modules to AMD format, but only when the file contains import/export statements. Unlike the standard babel-plugin-transform-es2015-modules-amd, this lazy variant skips files without imports or exports, preventing unnecessary wrapping of plain scripts or existing AMD modules. This is particularly useful in transitional or mixed codebases (e.g., combining ES2015, existing AMD modules, and non-module scripts). The plugin is a fork of Atlassian's AMD transform and is designed for legacy projects migrating to ES modules with minimal disruption. It is not intended for pure ES2015 projects.
Common errors
error Error: Cannot find module 'babel-plugin-transform-es2015-modules-amd-lazy' ↓
cause Plugin not installed or not in node_modules.
fix
Run: npm install babel-plugin-transform-es2015-modules-amd-lazy --save-dev
error SyntaxError: Unexpected token 'export' (while using without @babel/preset-env) ↓
cause Babel not configured to handle ES module syntax; this plugin only transforms import/export to AMD, but parsing requires @babel/preset-env or @babel/plugin-syntax-async-generators etc.
fix
Add @babel/preset-env with modules: false to your config.
error Duplicate module name [...] (when multiple AMD modules have same name) ↓
cause AMD naming collisions if the module IDs are inferred incorrectly.
fix
Explicitly set module IDs using the 'moduleIds' option or custom resolver.
Warnings
gotcha Files without imports/exports are not transformed at all. This can cause silent omission if you expect all files to be AMD-wrapped. ↓
fix If you need all files transformed, use the standard babel-plugin-transform-es2015-modules-amd instead.
deprecated This plugin targets ES2015 modules, not the newer Babel 7 module transform. It may not work with @babel/preset-env's modules option. ↓
fix Consider using @babel/plugin-transform-modules-amd for new projects, which is better maintained.
gotcha Plugin name uses 'es2015' but works with modern ES modules; naming confusion may cause developers to seek alternatives unnecessarily. ↓
fix This plugin works with ES modules in @babel/core v7; the name is legacy.
Install
npm install babel-plugin-transform-es2015-modules-amd-lazy yarn add babel-plugin-transform-es2015-modules-amd-lazy pnpm add babel-plugin-transform-es2015-modules-amd-lazy Imports
- default (plugin) wrong
import plugin from 'babel-plugin-transform-es2015-modules-amd-lazy'correctmodule.exports = require('babel-plugin-transform-es2015-modules-amd-lazy') - plugin in .babelrc or babel.config.js wrong
{ "plugins": ["babel-plugin-transform-es2015-modules-amd-lazy"] }correct{ "plugins": ["transform-es2015-modules-amd-lazy"] } - plugin options (if any)
{ "plugins": [["transform-es2015-modules-amd-lazy", { "skipUnusedExports": true }]] }
Quickstart
// babel.config.js
module.exports = {
presets: [['@babel/preset-env', { modules: false }]],
plugins: ['transform-es2015-modules-amd-lazy']
};
// Input: source.js with import/export
import { foo } from './module';
export const bar = foo;
// Output: AMD define wrapping
// If source.js had no imports/exports, it stays untouched.