prevent duplicate imports
This commit is contained in:
parent
69e9138637
commit
5d32ca6bb3
@ -1,13 +1,13 @@
|
||||
const polyfillSource = "babel-polyfill";
|
||||
|
||||
export default function ({ types: t }) {
|
||||
function addImport(polyfill) {
|
||||
function createImportDeclaration(polyfill) {
|
||||
let declar = t.importDeclaration([], t.stringLiteral(`core-js/modules/${polyfill}`));
|
||||
declar._blockHoist = 3;
|
||||
return declar;
|
||||
}
|
||||
|
||||
function addRequire(polyfill) {
|
||||
function createRequireStatement(polyfill) {
|
||||
return t.expressionStatement(
|
||||
t.callExpression(
|
||||
t.identifier("require"),
|
||||
@ -38,7 +38,9 @@ export default function ({ types: t }) {
|
||||
return;
|
||||
}
|
||||
|
||||
let imports = state.opts.polyfills.map((p) => addImport(p));
|
||||
let imports = state.opts.polyfills
|
||||
.filter((el, i, arr) => arr.indexOf(el) === i)
|
||||
.map((p) => createImportDeclaration(p));
|
||||
path.replaceWithMultiple(imports);
|
||||
}
|
||||
},
|
||||
@ -58,7 +60,9 @@ to the "transform-polyfill-require" plugin
|
||||
return;
|
||||
}
|
||||
|
||||
let requires = state.opts.polyfills.map((p) => addRequire(p));
|
||||
let requires = state.opts.polyfills
|
||||
.filter((el, i, arr) => arr.indexOf(el) === i)
|
||||
.map((p) => createRequireStatement(p));
|
||||
bodyPath.replaceWithMultiple(requires);
|
||||
}
|
||||
});
|
||||
|
||||
1
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/actual.js
vendored
Normal file
1
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "babel-polyfill";
|
||||
2
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/expected.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import "core-js/modules/es6.typed.data-view";
|
||||
import "core-js/modules/es6.reflect.apply";
|
||||
11
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/options.json
vendored
Normal file
11
experimental/babel-preset-env/test/fixtures/plugin-options/filters-duplicates/options.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"plugins": [
|
||||
["../../../../lib/transformPolyfillRequirePlugin", {
|
||||
"polyfills": [
|
||||
"es6.typed.data-view",
|
||||
"es6.typed.data-view",
|
||||
"es6.reflect.apply"
|
||||
]
|
||||
}]
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user