Merge pull request #3366 from clayreimann/umd-plugin-add-global-name-override
[UMD] Fixed T6832
This commit is contained in:
commit
3e4668dc14
@ -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
|
### Via CLI
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@ -39,7 +39,7 @@ export default function ({ types: t }) {
|
|||||||
|
|
||||||
visitor: {
|
visitor: {
|
||||||
Program: {
|
Program: {
|
||||||
exit(path) {
|
exit(path, state) {
|
||||||
let last = path.get("body").pop();
|
let last = path.get("body").pop();
|
||||||
if (!isValidDefine(last)) return;
|
if (!isValidDefine(last)) return;
|
||||||
|
|
||||||
@ -49,6 +49,7 @@ export default function ({ types: t }) {
|
|||||||
let moduleName = args.length === 3 ? args.shift() : null;
|
let moduleName = args.length === 3 ? args.shift() : null;
|
||||||
let amdArgs = call.arguments[0];
|
let amdArgs = call.arguments[0];
|
||||||
let func = call.arguments[1];
|
let func = call.arguments[1];
|
||||||
|
let browserGlobals = state.opts.globals || {};
|
||||||
|
|
||||||
let commonArgs = amdArgs.elements.map((arg) => {
|
let commonArgs = amdArgs.elements.map((arg) => {
|
||||||
if (arg.value === "module" || arg.value === "exports") {
|
if (arg.value === "module" || arg.value === "exports") {
|
||||||
@ -64,8 +65,11 @@ export default function ({ types: t }) {
|
|||||||
} else if (arg.value === "exports") {
|
} else if (arg.value === "exports") {
|
||||||
return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
|
return t.memberExpression(t.identifier("mod"), t.identifier("exports"));
|
||||||
} else {
|
} else {
|
||||||
|
let requireName = basename(arg.value, extname(arg.value));
|
||||||
|
let globalName = browserGlobals[requireName] || requireName;
|
||||||
|
|
||||||
return t.memberExpression(t.identifier("global"), t.identifier(
|
return t.memberExpression(t.identifier("global"), t.identifier(
|
||||||
t.toIdentifier(basename(arg.value, extname(arg.value)))
|
t.toIdentifier(globalName)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
import "babel-template";
|
||||||
@ -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 () {});
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"external-helpers",
|
||||||
|
["transform-es2015-modules-umd", {
|
||||||
|
"globals": {
|
||||||
|
"babel-template": "baz"
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user