This plugin transforms ES2015 modules to UMD
npx @tessl/cli install tessl/npm-babel--plugin-transform-modules-umd@7.27.0@babel/plugin-transform-modules-umd is a Babel plugin that transforms ES2015 module syntax (import/export statements) into Universal Module Definition (UMD) format. UMD enables JavaScript modules to work across different environments including AMD loaders (RequireJS), CommonJS (Node.js), and browser globals with automatic environment detection.
npm install --save-dev @babel/plugin-transform-modules-umd// CommonJS - Babel plugin usage
const transformModulesUmd = require("@babel/plugin-transform-modules-umd");// ESM - TypeScript types only
import type { Options } from "@babel/plugin-transform-modules-umd";
import type { PluginObj, NodePath } from "@babel/core";
import type { Program } from "@babel/types";@babel/plugin-transform-modules-umd works by:
@babel/helper-module-transformsThe plugin integrates with Babel's compilation pipeline and requires Babel 7.0.0 or higher.
Add the plugin to your Babel configuration:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-transform-modules-umd"]
};With options:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-transform-modules-umd", {
globals: {
"lodash": "_",
"react": "React"
},
exactGlobals: true
}]
]
};Input (ES2015 modules):
import React from "react";
import { map } from "lodash";
export function processData(data) {
return map(data, item => React.createElement("div", null, item));
}Output (UMD format):
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["react", "lodash"], factory);
} else if (typeof exports !== "undefined") {
factory(require("react"), require("lodash"));
} else {
var mod = { exports: {} };
factory(global.React, global._);
global.myModule = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (React, lodash) {
function processData(data) {
return lodash.map(data, item => React.createElement("div", null, item));
}
exports.processData = processData;
});The main plugin function that transforms ES2015 modules to UMD format.
/**
* @babel/plugin-transform-modules-umd plugin function
* Requires Babel 7.0.0 or higher
*/
declare function plugin(api: PluginAPI, options?: Options): PluginObj;
interface PluginAPI {
assertVersion(range: number | string): void;
assumption(name: string): boolean | undefined;
version: string;
cache: {
forever(): boolean;
never(): boolean;
using<T>(callback: () => T): T;
invalidate(reason: string): void;
};
}
interface PluginObj {
name: "transform-modules-umd";
visitor: {
Program: {
exit(path: NodePath<Program>, state: any): void;
};
};
}The plugin function is the default export and should be used in Babel configuration files.
Configuration options for customizing the UMD transformation behavior.
interface Options extends PluginOptions {
/** Allow top-level `this` usage in the transformed module */
allowTopLevelThis?: boolean;
/** Use exact global variable paths for complex nested globals */
exactGlobals?: boolean;
/** Mapping of module names to global variable names */
globals?: Record<string, string>;
/** Import interoperability options for handling different module formats */
importInterop?: "babel" | "node" | "none";
/** Enable loose mode transformations for smaller output */
loose?: boolean;
/** Disable interop helpers for simpler output */
noInterop?: boolean;
/** Enable strict mode in the module wrapper */
strict?: boolean;
/** Enable strict mode in output (alias for strict) */
strictMode?: boolean;
}
interface PluginOptions {
/** Module ID for the transformed module */
moduleId?: string;
/** Whether to enable module IDs */
moduleIds?: boolean;
/** Function to generate module ID from module name */
getModuleId?: (moduleName: string) => string | null | undefined;
/** Root directory for resolving module names */
moduleRoot?: string;
}Maps module names to global variable names for browser environments:
{
"globals": {
"jquery": "$",
"lodash": "_",
"react": "React",
"react-dom": "ReactDOM"
}
}When true, enables support for nested global paths:
{
"exactGlobals": true,
"globals": {
"my-lib": "MyCompany.Utils.MyLib"
}
}This generates code that creates intermediate objects if they don't exist:
global.MyCompany = global.MyCompany || {};
global.MyCompany.Utils = global.MyCompany.Utils || {};
global.MyCompany.Utils.MyLib = mod.exports;Controls how imports are handled for different module systems:
"babel" (default): Use Babel's interop helpers for ES6/CommonJS compatibility"node": Use Node.js-style require semantics"none": No interop, direct property accessEnables loose mode for smaller, faster output with some spec compliance trade-offs.
Allows this to be used at the top level of modules (normally undefined in UMD).
The plugin will throw errors in these cases:
Common error messages:
"Requires Babel \"^7.0.0-0\", but was loaded with \"...\": Babel version mismatch"Unknown option": Invalid configuration option provided