also import regenerator when the transform is required

This commit is contained in:
Henry Zhu 2016-12-06 16:01:21 -05:00
parent 8e2aa82991
commit 541ebb5e5f
13 changed files with 151 additions and 12 deletions

View File

@ -184,7 +184,12 @@ export default function buildPreset(context, opts = {}) {
}
}
transformations = [...transformations, ...whitelist].map((pluginName) => {
let allTransformations = [...transformations, ...whitelist];
let regenerator = allTransformations.indexOf("transform-regenerator") >= 0;
console.log(allTransformations, regenerator);
let plugins = allTransformations.map((pluginName) => {
return [require(`babel-plugin-${pluginName}`), { loose }];
});
@ -198,8 +203,8 @@ export default function buildPreset(context, opts = {}) {
return {
plugins: [
...modules,
...transformations,
useBuiltIns === true && [transformPolyfillRequirePlugin, { polyfills }]
...plugins,
useBuiltIns === true && [transformPolyfillRequirePlugin, { polyfills, regenerator }]
].filter(Boolean)
};
}

View File

@ -30,6 +30,25 @@ export default function ({ types: t }) {
isPolyfillSource(path.node.expression.arguments[0].value);
}
function createImport(polyfill, requireType) {
if (requireType === "import") {
return createImportDeclaration(polyfill);
} else {
return createRequireStatement(polyfill);
}
}
function createImports(polyfills, requireType, regenerator) {
let imports = polyfills
.filter((el, i, arr) => arr.indexOf(el) === i)
.map((polyfill) => createImport(polyfill, requireType));
return [
...imports,
regenerator && createImport("regenerator-runtime/runtime", requireType)
].filter(Boolean);
}
const isPolyfillImport = {
ImportDeclaration(path, state) {
if (path.node.specifiers.length === 0 &&
@ -40,10 +59,9 @@ export default function ({ types: t }) {
return;
}
let imports = state.opts.polyfills
.filter((el, i, arr) => arr.indexOf(el) === i)
.map((p) => createImportDeclaration(p));
path.replaceWithMultiple(imports);
path.replaceWithMultiple(
createImports(state.opts.polyfills, "import", state.opts.regenerator)
);
}
},
Program(path, state) {
@ -62,10 +80,9 @@ to the "transform-polyfill-require" plugin
return;
}
let requires = state.opts.polyfills
.filter((el, i, arr) => arr.indexOf(el) === i)
.map((p) => createRequireStatement(p));
bodyPath.replaceWithMultiple(requires);
bodyPath.replaceWithMultiple(
createImports(state.opts.polyfills, "require", state.opts.regenerator)
);
}
});
}

View File

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

View File

@ -0,0 +1,8 @@
{
"plugins": [
["../../../../lib/transformPolyfillRequirePlugin", {
"regenerator": false,
"polyfills": []
}]
]
}

View File

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

View File

@ -0,0 +1 @@
import "core-js/modules/regenerator-runtime/runtime";

View File

@ -0,0 +1,8 @@
{
"plugins": [
["../../../../lib/transformPolyfillRequirePlugin", {
"regenerator": true,
"polyfills": []
}]
]
}

View File

@ -28,5 +28,6 @@ import "core-js/modules/es7.object.entries";
import "core-js/modules/es7.object.get-own-property-descriptors";
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "core-js/modules/regenerator-runtime/runtime";
Math.pow(1, 2);
Math.pow(1, 2);

View File

@ -14,5 +14,6 @@ import "core-js/modules/es7.object.entries";
import "core-js/modules/es7.object.get-own-property-descriptors";
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "core-js/modules/regenerator-runtime/runtime";
Math.pow(1, 2);

View File

@ -0,0 +1,2 @@
import "babel-polyfill";
1 ** 2;

View File

@ -0,0 +1,86 @@
import "core-js/modules/es6.typed.data-view";
import "core-js/modules/es6.typed.int8-array";
import "core-js/modules/es6.typed.uint8-array";
import "core-js/modules/es6.typed.uint8-clamped-array";
import "core-js/modules/es6.typed.int16-array";
import "core-js/modules/es6.typed.uint16-array";
import "core-js/modules/es6.typed.int32-array";
import "core-js/modules/es6.typed.uint32-array";
import "core-js/modules/es6.typed.float32-array";
import "core-js/modules/es6.typed.float64-array";
import "core-js/modules/es6.map";
import "core-js/modules/es6.set";
import "core-js/modules/es6.weak-map";
import "core-js/modules/es6.weak-set";
import "core-js/modules/es6.reflect.apply";
import "core-js/modules/es6.reflect.construct";
import "core-js/modules/es6.reflect.define-property";
import "core-js/modules/es6.reflect.delete-property";
import "core-js/modules/es6.reflect.get";
import "core-js/modules/es6.reflect.get-own-property-descriptor";
import "core-js/modules/es6.reflect.get-prototype-of";
import "core-js/modules/es6.reflect.has";
import "core-js/modules/es6.reflect.is-extensible";
import "core-js/modules/es6.reflect.own-keys";
import "core-js/modules/es6.reflect.prevent-extensions";
import "core-js/modules/es6.reflect.set";
import "core-js/modules/es6.reflect.set-prototype-of";
import "core-js/modules/es6.promise";
import "core-js/modules/es6.symbol";
import "core-js/modules/es6.object.assign";
import "core-js/modules/es6.object.is";
import "core-js/modules/es6.object.get-own-property-symbols";
import "core-js/modules/es6.object.set-prototype-of";
import "core-js/modules/es6.function.name";
import "core-js/modules/es6.string.raw";
import "core-js/modules/es6.string.from-code-point";
import "core-js/modules/es6.string.code-point-at";
import "core-js/modules/es6.string.repeat";
import "core-js/modules/es6.string.starts-with";
import "core-js/modules/es6.string.ends-with";
import "core-js/modules/es6.string.includes";
import "core-js/modules/es6.regexp.flags";
import "core-js/modules/es6.regexp.match";
import "core-js/modules/es6.regexp.replace";
import "core-js/modules/es6.regexp.split";
import "core-js/modules/es6.regexp.search";
import "core-js/modules/es6.array.from";
import "core-js/modules/es6.array.of";
import "core-js/modules/es6.array.copy-within";
import "core-js/modules/es6.array.find";
import "core-js/modules/es6.array.find-index";
import "core-js/modules/es6.array.fill";
import "core-js/modules/es6.array.iterator";
import "core-js/modules/es6.number.is-finite";
import "core-js/modules/es6.number.is-integer";
import "core-js/modules/es6.number.is-safe-integer";
import "core-js/modules/es6.number.is-nan";
import "core-js/modules/es6.number.epsilon";
import "core-js/modules/es6.number.min-safe-integer";
import "core-js/modules/es6.number.max-safe-integer";
import "core-js/modules/es6.math.acosh";
import "core-js/modules/es6.math.asinh";
import "core-js/modules/es6.math.atanh";
import "core-js/modules/es6.math.cbrt";
import "core-js/modules/es6.math.clz32";
import "core-js/modules/es6.math.cosh";
import "core-js/modules/es6.math.expm1";
import "core-js/modules/es6.math.fround";
import "core-js/modules/es6.math.hypot";
import "core-js/modules/es6.math.imul";
import "core-js/modules/es6.math.log1p";
import "core-js/modules/es6.math.log10";
import "core-js/modules/es6.math.log2";
import "core-js/modules/es6.math.sign";
import "core-js/modules/es6.math.sinh";
import "core-js/modules/es6.math.tanh";
import "core-js/modules/es6.math.trunc";
import "core-js/modules/es7.array.includes.js";
import "core-js/modules/es7.object.values";
import "core-js/modules/es7.object.entries";
import "core-js/modules/es7.object.get-own-property-descriptors";
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "core-js/modules/regenerator-runtime/runtime";
Math.pow(1, 2);

View File

@ -0,0 +1,8 @@
{
"presets": [
["../../../../lib", {
"modules": false,
"useBuiltIns": true
}]
]
}