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:
Nicolò Ribaudo 2019-10-15 23:46:10 +02:00 committed by GitHub
parent 686186cabc
commit 272d85d0ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 270 additions and 42 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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';

View File

@ -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';

View File

@ -0,0 +1 @@
import "core-js";

View File

@ -0,0 +1,14 @@
{
"presets": [
[
"env",
{
"useBuiltIns": "entry",
"corejs": 2,
"modules": false,
"targets": { "chrome": 65 }
}
]
],
"plugins": ["./plugin.js"]
}

View File

@ -0,0 +1,6 @@
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";

View File

@ -0,0 +1,8 @@
module.exports = () => ({
visitor: {
ImportDeclaration(path) {
if (path.node.source.value === "core-js") return;
path.node.source.value = "MODIFIED";
},
},
});

View File

@ -0,0 +1 @@
new Map();

View File

@ -0,0 +1,6 @@
{
"presets": [
["env", { "useBuiltIns": "usage", "corejs": 2, "modules": false }]
],
"plugins": ["./plugin.js"]
}

View File

@ -0,0 +1,6 @@
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
new Map();

View File

@ -0,0 +1,7 @@
module.exports = () => ({
visitor: {
ImportDeclaration(path) {
path.node.source.value = "MODIFIED";
},
},
});

View File

@ -0,0 +1 @@
import "core-js";

View File

@ -0,0 +1,14 @@
{
"presets": [
[
"env",
{
"useBuiltIns": "entry",
"corejs": 3,
"modules": false,
"targets": { "chrome": 65 }
}
]
],
"plugins": ["./plugin.js"]
}

View 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";

View File

@ -0,0 +1,8 @@
module.exports = () => ({
visitor: {
ImportDeclaration(path) {
if (path.node.source.value === "core-js") return;
path.node.source.value = "MODIFIED";
},
},
});

View File

@ -0,0 +1 @@
new Map();

View File

@ -0,0 +1,6 @@
{
"presets": [
["env", { "useBuiltIns": "usage", "corejs": 3, "modules": false }]
],
"plugins": ["./plugin.js"]
}

View File

@ -0,0 +1,6 @@
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
import "MODIFIED";
new Map();

View File

@ -0,0 +1,7 @@
module.exports = () => ({
visitor: {
ImportDeclaration(path) {
path.node.source.value = "MODIFIED";
},
},
});