UNPKG

4.58 kBMarkdownView Raw
1# babel-plugin-transform-es2015-modules-umd
2
3> This plugin transforms ES2015 modules to [Universal Module Definition (UMD)](https://github.com/umdjs/umd).
4
5## Example
6
7**In**
8
9```javascript
10export default 42;
11```
12
13**Out**
14
15```javascript
16(function (global, factory) {
17 if (typeof define === "function" && define.amd) {
18 define(["exports"], factory);
19 } else if (typeof exports !== "undefined") {
20 factory(exports);
21 } else {
22 var mod = {
23 exports: {}
24 };
25 factory(mod.exports);
26 global.actual = mod.exports;
27 }
28})(this, function (exports) {
29 "use strict";
30
31 Object.defineProperty(exports, "__esModule", {
32 value: true
33 });
34
35 exports.default = 42;
36});
37```
38
39## Installation
40
41```sh
42npm install --save-dev babel-plugin-transform-es2015-modules-umd
43```
44
45## Usage
46
47### Via `.babelrc` (Recommended)
48
49**.babelrc**
50
51```json
52{
53 "plugins": ["transform-es2015-modules-umd"]
54}
55```
56
57You can also override the names of particular libraries when this module is
58running in the browser. For example the `es6-promise` library exposes itself
59as `global.Promise` rather than `global.es6Promise`. This can be accommodated by:
60
61```json
62{
63 "plugins": [
64 ["transform-es2015-modules-umd", {
65 "globals": {
66 "es6-promise": "Promise"
67 }
68 }]
69 ]
70}
71```
72
73#### Default semantics
74
75There are a few things to note about the default semantics.
76
77_First_, this transform uses the
78[basename](https://en.wikipedia.org/wiki/Basename) of each import to generate
79the global names in the UMD output. This means that if you're importing
80multiple modules with the same basename, like:
81
82```js
83import fooBar1 from "foo-bar";
84import fooBar2 from "./mylib/foo-bar";
85```
86
87it will transpile into two references to the same browser global:
88
89```js
90factory(global.fooBar, global.fooBar);
91```
92
93If you set the plugin options to:
94
95```json
96{
97 "globals": {
98 "foo-bar": "fooBAR",
99 "./mylib/foo-bar": "mylib.fooBar"
100 }
101}
102```
103
104it will still transpile both to one browser global:
105
106```js
107factory(global.fooBAR, global.fooBAR);
108```
109
110because again the transform is only using the basename of the import.
111
112_Second_, the specified override will still be passed to the `toIdentifier`
113function in [babel-types/src/converters](https://github.com/babel/babel/blob/master/packages/babel-types/src/converters.js).
114This means that if you specify an override as a member expression like:
115
116```json
117{
118 "globals": {
119 "fizzbuzz": "fizz.buzz"
120 }
121}
122```
123
124this will _not_ transpile to `factory(global.fizz.buzz)`. Instead, it will
125transpile to `factory(global.fizzBuzz)` based on the logic in `toIdentifier`.
126
127_Third_, you cannot override the exported global name.
128
129#### More flexible semantics with `exactGlobals: true`
130
131All of these behaviors can limit the flexibility of the `globals` map. To
132remove these limitations, you can set the `exactGlobals` option to `true`.
133Doing this instructs the plugin to:
134
1351. always use the full import string instead of the basename when generating
136the global names
1372. skip passing `globals` overrides to the `toIdentifier` function. Instead,
138they are used exactly as written, so you will get errors if you do not use
139valid identifiers or valid uncomputed (dot) member expressions.
1403. allow the exported global name to be overridden via the `globals` map. Any
141override must again be a valid identifier or valid member expression.
142
143Thus, if you set `exactGlobals` to `true` and do not pass any overrides, the
144first example of:
145
146```js
147import fooBar1 from "foo-bar";
148import fooBar2 from "./mylib/foo-bar";
149```
150
151will transpile to:
152
153```js
154factory(global.fooBar, global.mylibFooBar);
155```
156
157And if you set the plugin options to:
158
159```json
160{
161 "globals": {
162 "foo-bar": "fooBAR",
163 "./mylib/foo-bar": "mylib.fooBar"
164 },
165 "exactGlobals": true
166}
167```
168
169then it'll transpile to:
170
171```js
172factory(global.fooBAR, global.mylib.fooBar)
173```
174
175Finally, with the plugin options set to:
176
177```json
178{
179 "plugins": [
180 "external-helpers",
181 ["transform-es2015-modules-umd", {
182 "globals": {
183 "my/custom/module/name": "My.Custom.Module.Name"
184 },
185 "exactGlobals": true
186 }]
187 ],
188 "moduleId": "my/custom/module/name"
189}
190```
191
192it will transpile to:
193
194```js
195factory(mod.exports);
196global.My = global.My || {};
197global.My.Custom = global.My.Custom || {};
198global.My.Custom.Module = global.My.Custom.Module || {};
199global.My.Custom.Module.Name = mod.exports;
200```
201
202### Via CLI
203
204```sh
205babel --plugins transform-es2015-modules-umd script.js
206```
207
208### Via Node API
209
210```javascript
211require("babel-core").transform("code", {
212 plugins: ["transform-es2015-modules-umd"]
213});
214```