babel-plugin-wrap-modules-amd

raw JSON →
2.32.2 verified Sat Apr 25 auth: no javascript maintenance

A Babel plugin (v2.32.2, last released 2024-03-05) that wraps CommonJS modules inside AMD define() calls. It automatically detects require() calls and adds them as AMD dependencies, removing the need for manual dependency arrays. Part of the Liferay Frontend Projects monorepo, maintained for liferay-portal build system. Infrequently updated; stable but low community adoption outside Liferay. Requires Babel 6/7 and the plugin must be listed in .babelrc. No active development seen in recent years.

error Error: Cannot find module 'babel-plugin-wrap-modules-amd'
cause The plugin is not installed, or Node.js cannot resolve it when running Babel.
fix
Run: npm install --save-dev babel-plugin-wrap-modules-amd and ensure node_modules path is correct.
error ReferenceError: define is not defined
cause The wrapped AMD module is executed in an environment without an AMD loader (like RequireJS).
fix
Include an AMD loader such as RequireJS, or use the plugin to output CommonJS instead.
gotcha Plugin only supports CommonJS require() calls; does not handle dynamic imports or ES modules.
fix Use Babel plugin-transform-modules-commonjs first if using ES import syntax.
gotcha The plugin does not support named AMD modules; define() always uses anonymous definition.
fix If you need named modules, use a different plugin like babel-plugin-amd-define.
deprecated Maintenance mode; no new features planned. Consider migrating to modern bundlers like Webpack or Rollup if possible.
fix Use a modern bundler that outputs AMD if needed, or adopt a different output format.
npm install babel-plugin-wrap-modules-amd
yarn add babel-plugin-wrap-modules-amd
pnpm add babel-plugin-wrap-modules-amd

Shows how to configure the plugin in .babelrc and its effect: a CommonJS module with require() gets wrapped in an AMD define() call with dependencies auto-detected.

// .babelrc
{
  "plugins": ["wrap-modules-amd"]
}

// Input file (source.js)
var fs = require('fs');
console.log(fs.readFileSync('data.txt', 'utf8'));

// After Babel transformation, output becomes:
define(['module', 'exports', 'require', 'fs'], function (module, exports, require) {
  var fs = require('fs');
  console.log(fs.readFileSync('data.txt', 'utf8'));
});