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,
|
createImport,
|
||||||
getImportSource,
|
getImportSource,
|
||||||
getRequireSource,
|
getRequireSource,
|
||||||
|
getModulePath,
|
||||||
} from "../../utils";
|
} from "../../utils";
|
||||||
import { logEntryPolyfills } from "../../debug";
|
import { logEntryPolyfills } from "../../debug";
|
||||||
|
|
||||||
@ -48,6 +49,20 @@ export default function(
|
|||||||
|
|
||||||
const available = new Set(getModulesListForTargetVersion(corejs.version));
|
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 = {
|
const isPolyfillImport = {
|
||||||
ImportDeclaration(path: NodePath) {
|
ImportDeclaration(path: NodePath) {
|
||||||
const source = getImportSource(path);
|
const source = getImportSource(path);
|
||||||
@ -56,24 +71,40 @@ export default function(
|
|||||||
console.warn(BABEL_POLYFILL_DEPRECATION);
|
console.warn(BABEL_POLYFILL_DEPRECATION);
|
||||||
} else {
|
} else {
|
||||||
const modules = isCoreJSSource(source);
|
const modules = isCoreJSSource(source);
|
||||||
if (modules) {
|
if (shouldReplace(source, modules)) {
|
||||||
this.replaceBySeparateModulesImport(path, modules);
|
this.replaceBySeparateModulesImport(path, modules);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Program(path: NodePath) {
|
Program: {
|
||||||
path.get("body").forEach(bodyPath => {
|
enter(path: NodePath) {
|
||||||
const source = getRequireSource(bodyPath);
|
path.get("body").forEach(bodyPath => {
|
||||||
if (!source) return;
|
const source = getRequireSource(bodyPath);
|
||||||
if (isBabelPolyfillSource(source)) {
|
if (!source) return;
|
||||||
console.warn(BABEL_POLYFILL_DEPRECATION);
|
if (isBabelPolyfillSource(source)) {
|
||||||
} else {
|
console.warn(BABEL_POLYFILL_DEPRECATION);
|
||||||
const modules = isCoreJSSource(source);
|
} else {
|
||||||
if (modules) {
|
const modules = isCoreJSSource(source);
|
||||||
this.replaceBySeparateModulesImport(bodyPath, modules);
|
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",
|
name: "corejs3-entry",
|
||||||
visitor: isPolyfillImport,
|
visitor: isPolyfillImport,
|
||||||
pre() {
|
pre() {
|
||||||
|
this.injectedPolyfills = new Set();
|
||||||
this.polyfillsSet = new Set();
|
this.polyfillsSet = new Set();
|
||||||
|
|
||||||
this.replaceBySeparateModulesImport = function(path, modules) {
|
this.replaceBySeparateModulesImport = function(path, modules) {
|
||||||
@ -91,19 +123,12 @@ export default function(
|
|||||||
path.remove();
|
path.remove();
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
post({ path }: { path: NodePath }) {
|
post() {
|
||||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
|
||||||
const reversed = Array.from(filtered).reverse();
|
|
||||||
|
|
||||||
for (const module of reversed) {
|
|
||||||
createImport(path, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
logEntryPolyfills(
|
logEntryPolyfills(
|
||||||
"core-js",
|
"core-js",
|
||||||
this.polyfillsSet.size > 0,
|
this.injectedPolyfills.size > 0,
|
||||||
filtered,
|
this.injectedPolyfills,
|
||||||
this.file.opts.filename,
|
this.file.opts.filename,
|
||||||
polyfillTargets,
|
polyfillTargets,
|
||||||
corejs3Polyfills,
|
corejs3Polyfills,
|
||||||
|
|||||||
@ -112,13 +112,30 @@ export default function(
|
|||||||
},
|
},
|
||||||
|
|
||||||
// require('core-js')
|
// require('core-js')
|
||||||
Program(path: NodePath) {
|
Program: {
|
||||||
path.get("body").forEach(bodyPath => {
|
enter(path: NodePath) {
|
||||||
if (isPolyfillSource(getRequireSource(bodyPath))) {
|
path.get("body").forEach(bodyPath => {
|
||||||
console.warn(NO_DIRECT_POLYFILL_IMPORT);
|
if (isPolyfillSource(getRequireSource(bodyPath))) {
|
||||||
bodyPath.remove();
|
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(...)
|
// import('something').then(...)
|
||||||
@ -214,6 +231,7 @@ export default function(
|
|||||||
return {
|
return {
|
||||||
name: "corejs3-usage",
|
name: "corejs3-usage",
|
||||||
pre() {
|
pre() {
|
||||||
|
this.injectedPolyfills = new Set();
|
||||||
this.polyfillsSet = new Set();
|
this.polyfillsSet = new Set();
|
||||||
|
|
||||||
this.addUnsupported = function(builtIn) {
|
this.addUnsupported = function(builtIn) {
|
||||||
@ -252,17 +270,10 @@ export default function(
|
|||||||
this.addUnsupported(InstancePropertyDependencies);
|
this.addUnsupported(InstancePropertyDependencies);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
post({ path }: { path: NodePath }) {
|
post() {
|
||||||
const filtered = intersection(polyfills, this.polyfillsSet, available);
|
|
||||||
const reversed = Array.from(filtered).reverse();
|
|
||||||
|
|
||||||
for (const module of reversed) {
|
|
||||||
createImport(path, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
logUsagePolyfills(
|
logUsagePolyfills(
|
||||||
filtered,
|
this.injectedPolyfills,
|
||||||
this.file.opts.filename,
|
this.file.opts.filename,
|
||||||
polyfillTargets,
|
polyfillTargets,
|
||||||
corejs3Polyfills,
|
corejs3Polyfills,
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
import "core-js/modules/es.object.from-entries";
|
import 'core-js/modules/es.object.from-entries';
|
||||||
import "core-js/modules/esnext.string.replace-all";
|
import 'core-js/modules/esnext.string.replace-all';
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
import "core-js/modules/es.symbol";
|
import 'core-js/modules/es.symbol';
|
||||||
import "core-js/modules/es.object.from-entries";
|
import 'core-js/modules/es.object.from-entries';
|
||||||
import "core-js/modules/esnext.string.replace-all";
|
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