@babel/plugin-transform-modules-umd

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

.babelrc

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

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

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

Default semantics

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

First, this transform uses thebasename of each import to generatethe global names in the UMD output. This means that if you're importingmultiple 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 toIdentifierfunction 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 willtranspile 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. Toremove these limitations, you can set the exactGlobals option to true.Doing this instructs the plugin to:

  • always use the full import string instead of the basename when generatingthe global names
  • skip passing globals overrides to the toIdentifier function. Instead,they are used exactly as written, so you will get errors if you do not usevalid identifiers or valid uncomputed (dot) member expressions.
  • allow the exported global name to be overridden via the globals map. Anyoverride must again be a valid identifier or valid member expression.Thus, if you set exactGlobals to true and do not pass any overrides, thefirst 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. ["@babel/plugin-transform-modules-umd", {
  5. "globals": {
  6. "my/custom/module/name": "My.Custom.Module.Name"
  7. },
  8. "exactGlobals": true
  9. }]
  10. ],
  11. "moduleId": "my/custom/module/name"
  12. }

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").transform("code", {
  2. plugins: ["@babel/plugin-transform-modules-umd"]
  3. });