@babel/plugin-transform-modules-umd

NOTE: This plugin is included in @babel/preset-env under the modules option

This plugin transforms ES2015 modules to UMD. Note that only the syntax of import/export statements (import "./mod.js") is transformed, as Babel is unaware of different resolution algorithms between implementations of ES2015 modules and UMD.

⚠️ This plugin does not support dynamic import (import('./lazy.js')).

Example

In

  1. export default 42;

Out

  1. (function(global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define(["exports"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports);
  6. } else {
  7. var mod = {
  8. exports: {},
  9. };
  10. factory(mod.exports);
  11. global.actual = mod.exports;
  12. }
  13. })(this, function(exports) {
  14. "use strict";
  15. Object.defineProperty(exports, "__esModule", {
  16. value: true,
  17. });
  18. exports.default = 42;
  19. });

Installation

  1. npm install --save-dev @babel/plugin-transform-modules-umd

Usage

  1. {
  2. "plugins": ["@babel/plugin-transform-modules-umd"]
  3. }

You can also override the names of particular libraries when this module is running in the browser. For example the es6-promise library exposes itself as global.Promise rather than global.es6Promise. This can be accommodated by:

  1. {
  2. "plugins": [
  3. [
  4. "@babel/plugin-transform-modules-umd",
  5. {
  6. "globals": {
  7. "es6-promise": "Promise"
  8. }
  9. }
  10. ]
  11. ]
  12. }

Default semantics

There are a few things to note about the default semantics.

First, this transform uses the basename of each import to generate the global names in the UMD output. This means that if you’re importing multiple modules with the same basename, like:

  1. import fooBar1 from "foo-bar";
  2. import fooBar2 from "./mylib/foo-bar";

it will transpile into two references to the same browser global:

  1. factory(global.fooBar, global.fooBar);

If you set the plugin options to:

  1. {
  2. "globals": {
  3. "foo-bar": "fooBAR",
  4. "./mylib/foo-bar": "mylib.fooBar"
  5. }
  6. }

it will still transpile both to one browser global:

  1. factory(global.fooBAR, global.fooBAR);

because again the transform is only using the basename of the import.

Second, the specified override will still be passed to the toIdentifier function in babel-types/src/converters. This means that if you specify an override as a member expression like:

  1. {
  2. "globals": {
  3. "fizzbuzz": "fizz.buzz"
  4. }
  5. }

this will not transpile to factory(global.fizz.buzz). Instead, it will transpile to factory(global.fizzBuzz) based on the logic in toIdentifier.

Third, you cannot override the exported global name.

More flexible semantics with exactGlobals: true

All of these behaviors can limit the flexibility of the globals map. To remove these limitations, you can set the exactGlobals option to true. Doing this instructs the plugin to:

  1. always use the full import string instead of the basename when generating the global names
  2. skip passing globals overrides to the toIdentifier function. Instead, they are used exactly as written, so you will get errors if you do not use valid identifiers or valid uncomputed (dot) member expressions.
  3. allow the exported global name to be overridden via the globals map. Any override must again be a valid identifier or valid member expression.

Thus, if you set exactGlobals to true and do not pass any overrides, the first example of:

  1. import fooBar1 from "foo-bar";
  2. import fooBar2 from "./mylib/foo-bar";

will transpile to:

  1. factory(global.fooBar, global.mylibFooBar);

And if you set the plugin options to:

  1. {
  2. "globals": {
  3. "foo-bar": "fooBAR",
  4. "./mylib/foo-bar": "mylib.fooBar"
  5. },
  6. "exactGlobals": true
  7. }

then it’ll transpile to:

  1. factory(global.fooBAR, global.mylib.fooBar);

Finally, with the plugin options set to:

  1. {
  2. "plugins": [
  3. "@babel/plugin-external-helpers",
  4. [
  5. "@babel/plugin-transform-modules-umd",
  6. {
  7. "globals": {
  8. "my/custom/module/name": "My.Custom.Module.Name"
  9. },
  10. "exactGlobals": true
  11. }
  12. ]
  13. ],
  14. "moduleId": "my/custom/module/name"
  15. }

it will transpile to:

  1. factory(mod.exports);
  2. global.My = global.My || {};
  3. global.My.Custom = global.My.Custom || {};
  4. global.My.Custom.Module = global.My.Custom.Module || {};
  5. global.My.Custom.Module.Name = mod.exports;

Via CLI

  1. babel --plugins @babel/plugin-transform-modules-umd script.js

Via Node API

  1. require("@babel/core").transformSync("code", {
  2. plugins: ["@babel/plugin-transform-modules-umd"],
  3. });