add some tests
This commit is contained in:
parent
3a7a1b9221
commit
d06270498b
@ -2,14 +2,8 @@
|
||||
// or if more than one is found
|
||||
|
||||
const polyfillSource = "babel-polyfill";
|
||||
let numPolyfillImports = 0;
|
||||
|
||||
export default function ({ types: t }) {
|
||||
function checkNumPolyfillImports() {
|
||||
numPolyfillImports++;
|
||||
return numPolyfillImports > 1;
|
||||
}
|
||||
|
||||
function addImport(polyfill) {
|
||||
let declar = t.importDeclaration([], t.stringLiteral(`core-js/modules/${polyfill}`));
|
||||
declar._blockHoist = 3;
|
||||
@ -41,7 +35,8 @@ export default function ({ types: t }) {
|
||||
ImportDeclaration(path, state) {
|
||||
if (path.node.specifiers.length === 0 &&
|
||||
path.node.source.value === polyfillSource) {
|
||||
if (checkNumPolyfillImports()) {
|
||||
this.numPolyfillImports++;
|
||||
if (this.numPolyfillImports > 1) {
|
||||
path.remove();
|
||||
return;
|
||||
}
|
||||
@ -53,19 +48,19 @@ export default function ({ types: t }) {
|
||||
Program(path, state) {
|
||||
if (!state.opts.polyfills) {
|
||||
throw path.buildCodeFrameError(`
|
||||
"polyfills" option not correctly passed
|
||||
to the transform-polyfill-require plugin
|
||||
in babel-preset-env
|
||||
There was an issue in "babel-preset-env" such that
|
||||
the "polyfills" option was not correctly passed
|
||||
to the "transform-polyfill-require" plugin
|
||||
`);
|
||||
}
|
||||
path.get("body").forEach((bodyPath) => {
|
||||
if (isRequire(bodyPath, polyfillSource)) {
|
||||
if (checkNumPolyfillImports()) {
|
||||
this.numPolyfillImports++;
|
||||
if (this.numPolyfillImports > 1) {
|
||||
path.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let requires = state.opts.polyfills.map((p) => addRequire(p));
|
||||
bodyPath.replaceWithMultiple(requires);
|
||||
}
|
||||
@ -75,6 +70,9 @@ in babel-preset-env
|
||||
|
||||
return {
|
||||
name: "transform-polyfill-require",
|
||||
visitor: isPolyfillImport
|
||||
visitor: isPolyfillImport,
|
||||
pre() {
|
||||
this.numPolyfillImports = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
1 ** 2;
|
||||
@ -1 +0,0 @@
|
||||
1 ** 2;
|
||||
2
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-import/actual.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-import/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import "babel-polyfill";
|
||||
1 ** 2;
|
||||
8
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-import/expected.js
vendored
Normal file
8
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-import/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
import "core-js/modules/es6.typed.data-view";
|
||||
import "core-js/modules/es6.reflect.apply";
|
||||
import "core-js/modules/es6.reflect.own-keys";
|
||||
import "core-js/modules/es6.symbol.iterator";
|
||||
import "core-js/modules/es6.symbol.species";
|
||||
import "core-js/modules/es6.array.from";
|
||||
|
||||
1 ** 2;
|
||||
@ -2,7 +2,7 @@
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 54
|
||||
"chrome": 55
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
@ -0,0 +1,3 @@
|
||||
import "babel-polyfill";
|
||||
import "babel-polyfill";
|
||||
1 ** 2;
|
||||
@ -0,0 +1,8 @@
|
||||
import "core-js/modules/es6.typed.data-view";
|
||||
import "core-js/modules/es6.reflect.apply";
|
||||
import "core-js/modules/es6.reflect.own-keys";
|
||||
import "core-js/modules/es6.symbol.iterator";
|
||||
import "core-js/modules/es6.symbol.species";
|
||||
import "core-js/modules/es6.array.from";
|
||||
|
||||
1 ** 2;
|
||||
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 55
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
3
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/actual.js
vendored
Normal file
3
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
require("babel-polyfill");
|
||||
|
||||
1 ** 2;
|
||||
13
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/expected.js
vendored
Normal file
13
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/expected.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
require("core-js/modules/es6.typed.data-view");
|
||||
|
||||
require("core-js/modules/es6.reflect.apply");
|
||||
|
||||
require("core-js/modules/es6.reflect.own-keys");
|
||||
|
||||
require("core-js/modules/es6.symbol.iterator");
|
||||
|
||||
require("core-js/modules/es6.symbol.species");
|
||||
|
||||
require("core-js/modules/es6.array.from");
|
||||
|
||||
1 ** 2;
|
||||
11
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/options.json
vendored
Normal file
11
experimental/babel-preset-env/test/fixtures/preset-options/use-builtins-require/options.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 55
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user