Merge pull request #3366 from clayreimann/umd-plugin-add-global-name-override

[UMD] Fixed T6832
This commit is contained in:
Henry Zhu 2016-02-29 09:45:10 -05:00
commit 3e4668dc14
5 changed files with 46 additions and 2 deletions

View File

@ -18,6 +18,22 @@ $ npm install babel-plugin-transform-es2015-modules-umd
}
```
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 accomidated by:
```
{
"plugins": [
["transform-es2015-modules-umd", {
"globals": {
"es6-promise": "Promise"
}
}]
]
}
```
### Via CLI
```sh

View File

@ -39,7 +39,7 @@ export default function ({ types: t }) {
visitor: {
Program: {
exit(path) {
exit(path, state) {
let last = path.get("body").pop();
if (!isValidDefine(last)) return;
@ -49,6 +49,7 @@ export default function ({ types: t }) {
let moduleName = args.length === 3 ? args.shift() : null;
let amdArgs = call.arguments[0];
let func = call.arguments[1];
let browserGlobals = state.opts.globals || {};
let commonArgs = amdArgs.elements.map((arg) => {
if (arg.value === "module" || arg.value === "exports") {
@ -64,8 +65,11 @@ export default function ({ types: t }) {
} else if (arg.value === "exports") {
return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
} else {
let requireName = basename(arg.value, extname(arg.value));
let globalName = browserGlobals[requireName] || requireName;
return t.memberExpression(t.identifier("global"), t.identifier(
t.toIdentifier(basename(arg.value, extname(arg.value)))
t.toIdentifier(globalName)
));
}
});

View File

@ -0,0 +1 @@
import "babel-template";

View File

@ -0,0 +1,13 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["babel-template"], factory);
} else if (typeof exports !== "undefined") {
factory(require("babel-template"));
} else {
var mod = {
exports: {}
};
factory(global.baz);
global.actual = mod.exports;
}
})(this, function () {});

View File

@ -0,0 +1,10 @@
{
"plugins": [
"external-helpers",
["transform-es2015-modules-umd", {
"globals": {
"babel-template": "baz"
}
}]
]
}