babel-plugin-namespace-amd-define
raw JSON → 2.32.2 verified Sat Apr 25 auth: no javascript
A Babel plugin that prefixes AMD define() calls with a configurable namespace string, primarily used by Liferay projects. Current stable version is 2.32.2. It transforms define() to Liferay.Loader.define() by default, and supports custom namespaces via options. Unlike general-purpose plugins, it targets a niche use case: prepending a namespace to all AMD define calls in legacy codebases.
Common errors
error Error: Cannot find module 'babel-plugin-namespace-amd-define' ↓
cause Plugin not installed or incorrect import in Babel config.
fix
Run 'npm install --save-dev babel-plugin-namespace-amd-define' and use shorthand 'namespace-amd-define' in plugin list.
error TypeError: plugin is not a function ↓
cause Plugin was imported using default import but used incorrectly.
fix
Use the plugin as a string in Babel config: 'namespace-amd-define'.
error Windows path issues when using absolute paths in options ↓
cause The plugin may not normalize backslashes on Windows.
fix
Use forward slashes or path.resolve() to create cross-platform paths.
Warnings
gotcha If you omit options, the plugin uses 'Liferay.Loader' as the default namespace, which may not be appropriate for non-Liferay projects. ↓
fix Explicitly set the 'namespace' option to your desired prefix.
gotcha The plugin only transforms top-level define() calls; nested define() inside functions or conditional blocks are not affected. ↓
fix Ensure your define() calls are at module top level.
deprecated The plugin does not handle AMD require() calls; only define() is namespaced. ↓
fix Use a separate plugin or manual transformation for require() calls.
Install
npm install babel-plugin-namespace-amd-define yarn add babel-plugin-namespace-amd-define pnpm add babel-plugin-namespace-amd-define Imports
- default wrong
const { namespaceAmdDefine } = require('babel-plugin-namespace-amd-define')correctimport plugin from 'babel-plugin-namespace-amd-define' - plugin as wrong
module.exports = { plugins: ['babel-plugin-namespace-amd-define'] }correct// In .babelrc or babel.config.js: module.exports = { plugins: ['namespace-amd-define'] } - with options wrong
module.exports = { plugins: [{ name: 'namespace-amd-define', options: { namespace: 'MyApp' } }] }correctmodule.exports = { plugins: [['namespace-amd-define', { namespace: 'MyApp' }]] }
Quickstart
// babel.config.js
module.exports = {
plugins: [
[
'namespace-amd-define',
{ namespace: 'window.MyApp' }
]
]
};
// Input: define(['dep'], function() { });
// Output: window.MyApp.define(['dep'], function() { });