Add an option for custom runtime (#3612)
This commit is contained in:
parent
288e956699
commit
4ac0df00f1
@ -42,7 +42,8 @@ Add the following line to your `.babelrc` file:
|
||||
["transform-runtime", {
|
||||
"helpers": false, // defaults to true
|
||||
"polyfill": false, // defaults to true
|
||||
"regenerator": true // defaults to true
|
||||
"regenerator": true, // defaults to true
|
||||
"moduleName": "babel-runtime" // defaults to "babel-runtime"
|
||||
}]
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import definitions from "./definitions";
|
||||
|
||||
export default function ({ types: t }) {
|
||||
const RUNTIME_MODULE_NAME = "babel-runtime";
|
||||
function getRuntimeModuleName(opts) {
|
||||
return opts.moduleName || "babel-runtime";
|
||||
}
|
||||
|
||||
function has(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
@ -11,16 +13,18 @@ export default function ({ types: t }) {
|
||||
|
||||
return {
|
||||
pre(file) {
|
||||
const moduleName = getRuntimeModuleName(this.opts);
|
||||
|
||||
if (this.opts.helpers !== false) {
|
||||
file.set("helperGenerator", function (name) {
|
||||
if (HELPER_BLACKLIST.indexOf(name) < 0) {
|
||||
return file.addImport(`${RUNTIME_MODULE_NAME}/helpers/${name}`, "default", name);
|
||||
return file.addImport(`${moduleName}/helpers/${name}`, "default", name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.setDynamic("regeneratorIdentifier", function () {
|
||||
return file.addImport(`${RUNTIME_MODULE_NAME}/regenerator`, "default", "regeneratorRuntime");
|
||||
return file.addImport(`${moduleName}/regenerator`, "default", "regeneratorRuntime");
|
||||
});
|
||||
},
|
||||
|
||||
@ -40,8 +44,9 @@ export default function ({ types: t }) {
|
||||
if (scope.getBindingIdentifier(node.name)) return;
|
||||
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(state.addImport(
|
||||
`${RUNTIME_MODULE_NAME}/core-js/${definitions.builtins[node.name]}`,
|
||||
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
|
||||
"default",
|
||||
node.name
|
||||
));
|
||||
@ -59,9 +64,10 @@ export default function ({ types: t }) {
|
||||
if (!callee.computed) return;
|
||||
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) return;
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.callExpression(
|
||||
state.addImport(
|
||||
`${RUNTIME_MODULE_NAME}/core-js/get-iterator`,
|
||||
`${moduleName}/core-js/get-iterator`,
|
||||
"default",
|
||||
"getIterator"
|
||||
),
|
||||
@ -76,9 +82,10 @@ export default function ({ types: t }) {
|
||||
if (path.node.operator !== "in") return;
|
||||
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.callExpression(
|
||||
state.addImport(
|
||||
`${RUNTIME_MODULE_NAME}/core-js/is-iterable`,
|
||||
`${moduleName}/core-js/is-iterable`,
|
||||
"default",
|
||||
"isIterable"
|
||||
),
|
||||
@ -112,8 +119,9 @@ export default function ({ types: t }) {
|
||||
if (call.arguments.length === 3 && t.isLiteral(call.arguments[1])) return;
|
||||
}
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(state.addImport(
|
||||
`${RUNTIME_MODULE_NAME}/core-js/${methods[prop.name]}`,
|
||||
`${moduleName}/core-js/${methods[prop.name]}`,
|
||||
"default",
|
||||
`${obj.name}$${prop.name}`
|
||||
));
|
||||
@ -129,9 +137,10 @@ export default function ({ types: t }) {
|
||||
if (!has(definitions.builtins, obj.name)) return;
|
||||
if (path.scope.getBindingIdentifier(obj.name)) return;
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.memberExpression(
|
||||
state.addImport(
|
||||
`${RUNTIME_MODULE_NAME}/core-js/${definitions.builtins[obj.name]}`,
|
||||
`${moduleName}/core-js/${definitions.builtins[obj.name]}`,
|
||||
"default",
|
||||
obj.name
|
||||
),
|
||||
|
||||
3
packages/babel-plugin-transform-runtime/src/options.json
Normal file
3
packages/babel-plugin-transform-runtime/src/options.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-runtime", "transform-regenerator"]
|
||||
}
|
||||
9
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/actual.js
vendored
Normal file
9
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/actual.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
import foo, * as bar from "someModule";
|
||||
|
||||
export const myWord = Symbol("abc");
|
||||
export function* giveWord () {
|
||||
yield myWord;
|
||||
}
|
||||
|
||||
foo;
|
||||
bar;
|
||||
24
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/expected.js
vendored
Normal file
24
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/expected.js
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
import _regeneratorRuntime from "foo/regenerator";
|
||||
import _Symbol from "foo/core-js/symbol";
|
||||
|
||||
var _marked = [giveWord].map(_regeneratorRuntime.mark);
|
||||
|
||||
import foo, * as bar from "someModule";
|
||||
|
||||
export const myWord = _Symbol("abc");
|
||||
export function giveWord() {
|
||||
return _regeneratorRuntime.wrap(function giveWord$(_context) {
|
||||
while (1) switch (_context.prev = _context.next) {
|
||||
case 0:
|
||||
_context.next = 2;
|
||||
return myWord;
|
||||
|
||||
case 2:
|
||||
case "end":
|
||||
return _context.stop();
|
||||
}
|
||||
}, _marked[0], this);
|
||||
}
|
||||
|
||||
foo;
|
||||
bar;
|
||||
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/options.json
vendored
Normal file
3
packages/babel-plugin-transform-runtime/test/fixtures/runtime/custom-runtime/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-runtime", { "moduleName": "foo" }], "transform-regenerator"]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user