Inject core-js@3 imports in Program:exit instead of on post() (#10146)
* Inject corejs3 imports in Program:exit instead of post This allows them to be requeued and transformed by other plugins. * Also entry * Rebase * Update packages/babel-preset-env/src/polyfills/corejs3/entry-plugin.js [skip ci] Co-Authored-By: Brian Ng <bng412@gmail.com>
This commit is contained in:
parent
686186cabc
commit
272d85d0ad
@ -10,6 +10,7 @@ import {
|
||||
createImport,
|
||||
getImportSource,
|
||||
getRequireSource,
|
||||
getModulePath,
|
||||
} from "../../utils";
|
||||
import { logEntryPolyfills } from "../../debug";
|
||||
|
||||
@ -48,6 +49,20 @@ export default function(
|
||||
|
||||
const available = new Set(getModulesListForTargetVersion(corejs.version));
|
||||
|
||||
function shouldReplace(source, modules) {
|
||||
if (!modules) return false;
|
||||
if (
|
||||
// Don't replace an import with itself to avoid an infinite loop
|
||||
modules.length === 1 &&
|
||||
polyfills.has(modules[0]) &&
|
||||
available.has(modules[0]) &&
|
||||
getModulePath(modules[0]) === source
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const isPolyfillImport = {
|
||||
ImportDeclaration(path: NodePath) {
|
||||
const source = getImportSource(path);
|
||||
@ -56,24 +71,40 @@ export default function(
|
||||
console.warn(BABEL_POLYFILL_DEPRECATION);
|
||||
} else {
|
||||
const modules = isCoreJSSource(source);
|
||||
if (modules) {
|
||||
if (shouldReplace(source, modules)) {
|
||||
this.replaceBySeparateModulesImport(path, modules);
|
||||
}
|
||||
}
|
||||
},
|
||||
Program(path: NodePath) {
|
||||
path.get("body").forEach(bodyPath => {
|
||||
const source = getRequireSource(bodyPath);
|
||||
if (!source) return;
|
||||
if (isBabelPolyfillSource(source)) {
|
||||
console.warn(BABEL_POLYFILL_DEPRECATION);
|
||||
} else {
|
||||
const modules = isCoreJSSource(source);
|
||||
if (modules) {
|
||||
this.replaceBySeparateModulesImport(bodyPath, modules);
|
||||
Program: {
|
||||
enter(path: NodePath) {
|
||||
path.get("body").forEach(bodyPath => {
|
||||
const source = getRequireSource(bodyPath);
|
||||
if (!source) return;
|
||||
if (isBabelPolyfillSource(source)) {
|
||||
console.warn(BABEL_POLYFILL_DEPRECATION);
|
||||
} else {
|
||||
const modules = isCoreJSSource(source);
|
||||
if (shouldReplace(source, modules)) {
|
||||
this.replaceBySeparateModulesImport(bodyPath, modules);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
exit(path: NodePath) {
|
||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
||||
const reversed = Array.from(filtered).reverse();
|
||||
|
||||
for (const module of reversed) {
|
||||
// Program:exit could be called multiple times.
|
||||
// Avoid injecting the polyfills twice.
|
||||
if (!this.injectedPolyfills.has(module)) {
|
||||
createImport(path, module);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
filtered.forEach(module => this.injectedPolyfills.add(module));
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@ -81,6 +112,7 @@ export default function(
|
||||
name: "corejs3-entry",
|
||||
visitor: isPolyfillImport,
|
||||
pre() {
|
||||
this.injectedPolyfills = new Set();
|
||||
this.polyfillsSet = new Set();
|
||||
|
||||
this.replaceBySeparateModulesImport = function(path, modules) {
|
||||
@ -91,19 +123,12 @@ export default function(
|
||||
path.remove();
|
||||
};
|
||||
},
|
||||
post({ path }: { path: NodePath }) {
|
||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
||||
const reversed = Array.from(filtered).reverse();
|
||||
|
||||
for (const module of reversed) {
|
||||
createImport(path, module);
|
||||
}
|
||||
|
||||
post() {
|
||||
if (debug) {
|
||||
logEntryPolyfills(
|
||||
"core-js",
|
||||
this.polyfillsSet.size > 0,
|
||||
filtered,
|
||||
this.injectedPolyfills.size > 0,
|
||||
this.injectedPolyfills,
|
||||
this.file.opts.filename,
|
||||
polyfillTargets,
|
||||
corejs3Polyfills,
|
||||
|
||||
@ -112,13 +112,30 @@ export default function(
|
||||
},
|
||||
|
||||
// require('core-js')
|
||||
Program(path: NodePath) {
|
||||
path.get("body").forEach(bodyPath => {
|
||||
if (isPolyfillSource(getRequireSource(bodyPath))) {
|
||||
console.warn(NO_DIRECT_POLYFILL_IMPORT);
|
||||
bodyPath.remove();
|
||||
Program: {
|
||||
enter(path: NodePath) {
|
||||
path.get("body").forEach(bodyPath => {
|
||||
if (isPolyfillSource(getRequireSource(bodyPath))) {
|
||||
console.warn(NO_DIRECT_POLYFILL_IMPORT);
|
||||
bodyPath.remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
exit(path: NodePath) {
|
||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
||||
const reversed = Array.from(filtered).reverse();
|
||||
|
||||
for (const module of reversed) {
|
||||
// Program:exit could be called multiple times.
|
||||
// Avoid injecting the polyfills twice.
|
||||
if (!this.injectedPolyfills.has(module)) {
|
||||
createImport(path, module);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
filtered.forEach(module => this.injectedPolyfills.add(module));
|
||||
},
|
||||
},
|
||||
|
||||
// import('something').then(...)
|
||||
@ -214,6 +231,7 @@ export default function(
|
||||
return {
|
||||
name: "corejs3-usage",
|
||||
pre() {
|
||||
this.injectedPolyfills = new Set();
|
||||
this.polyfillsSet = new Set();
|
||||
|
||||
this.addUnsupported = function(builtIn) {
|
||||
@ -252,17 +270,10 @@ export default function(
|
||||
this.addUnsupported(InstancePropertyDependencies);
|
||||
};
|
||||
},
|
||||
post({ path }: { path: NodePath }) {
|
||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
||||
const reversed = Array.from(filtered).reverse();
|
||||
|
||||
for (const module of reversed) {
|
||||
createImport(path, module);
|
||||
}
|
||||
|
||||
post() {
|
||||
if (debug) {
|
||||
logUsagePolyfills(
|
||||
filtered,
|
||||
this.injectedPolyfills,
|
||||
this.file.opts.filename,
|
||||
polyfillTargets,
|
||||
corejs3Polyfills,
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
import "core-js/modules/es.object.from-entries";
|
||||
import "core-js/modules/esnext.string.replace-all";
|
||||
import 'core-js/modules/es.object.from-entries';
|
||||
import 'core-js/modules/esnext.string.replace-all';
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
import "core-js/modules/es.symbol";
|
||||
import "core-js/modules/es.object.from-entries";
|
||||
import "core-js/modules/esnext.string.replace-all";
|
||||
import 'core-js/modules/es.symbol';
|
||||
import 'core-js/modules/es.object.from-entries';
|
||||
import 'core-js/modules/esnext.string.replace-all';
|
||||
|
||||
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "core-js";
|
||||
14
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json
vendored
Normal file
14
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
"useBuiltIns": "entry",
|
||||
"corejs": 2,
|
||||
"modules": false,
|
||||
"targets": { "chrome": 65 }
|
||||
}
|
||||
]
|
||||
],
|
||||
"plugins": ["./plugin.js"]
|
||||
}
|
||||
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs
vendored
Normal file
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
8
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js
vendored
Normal file
8
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = () => ({
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
if (path.node.source.value === "core-js") return;
|
||||
path.node.source.value = "MODIFIED";
|
||||
},
|
||||
},
|
||||
});
|
||||
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
new Map();
|
||||
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json
vendored
Normal file
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"presets": [
|
||||
["env", { "useBuiltIns": "usage", "corejs": 2, "modules": false }]
|
||||
],
|
||||
"plugins": ["./plugin.js"]
|
||||
}
|
||||
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs
vendored
Normal file
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
new Map();
|
||||
7
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js
vendored
Normal file
7
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = () => ({
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
path.node.source.value = "MODIFIED";
|
||||
},
|
||||
},
|
||||
});
|
||||
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "core-js";
|
||||
14
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json
vendored
Normal file
14
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
"useBuiltIns": "entry",
|
||||
"corejs": 3,
|
||||
"modules": false,
|
||||
"targets": { "chrome": 65 }
|
||||
}
|
||||
]
|
||||
],
|
||||
"plugins": ["./plugin.js"]
|
||||
}
|
||||
100
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs
vendored
Normal file
100
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
8
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js
vendored
Normal file
8
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = () => ({
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
if (path.node.source.value === "core-js") return;
|
||||
path.node.source.value = "MODIFIED";
|
||||
},
|
||||
},
|
||||
});
|
||||
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
new Map();
|
||||
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json
vendored
Normal file
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"presets": [
|
||||
["env", { "useBuiltIns": "usage", "corejs": 3, "modules": false }]
|
||||
],
|
||||
"plugins": ["./plugin.js"]
|
||||
}
|
||||
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs
vendored
Normal file
6
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
import "MODIFIED";
|
||||
new Map();
|
||||
7
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js
vendored
Normal file
7
packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = () => ({
|
||||
visitor: {
|
||||
ImportDeclaration(path) {
|
||||
path.node.source.value = "MODIFIED";
|
||||
},
|
||||
},
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user