From 272d85d0ad653ca61011a6a7af07c272035cad0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 15 Oct 2019 23:46:10 +0200 Subject: [PATCH] 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 --- .../src/polyfills/corejs3/entry-plugin.js | 69 ++++++++---- .../src/polyfills/corejs3/usage-plugin.js | 41 ++++--- .../output.mjs | 4 +- .../corejs3/entry-entries-modules/output.mjs | 6 +- .../issue-10142-corejs2-entry/input.mjs | 1 + .../issue-10142-corejs2-entry/options.json | 14 +++ .../issue-10142-corejs2-entry/output.mjs | 6 ++ .../issue-10142-corejs2-entry/plugin.js | 8 ++ .../issue-10142-corejs2-usage/input.mjs | 1 + .../issue-10142-corejs2-usage/options.json | 6 ++ .../issue-10142-corejs2-usage/output.mjs | 6 ++ .../issue-10142-corejs2-usage/plugin.js | 7 ++ .../issue-10142-corejs3-entry/input.mjs | 1 + .../issue-10142-corejs3-entry/options.json | 14 +++ .../issue-10142-corejs3-entry/output.mjs | 100 ++++++++++++++++++ .../issue-10142-corejs3-entry/plugin.js | 8 ++ .../issue-10142-corejs3-usage/input.mjs | 1 + .../issue-10142-corejs3-usage/options.json | 6 ++ .../issue-10142-corejs3-usage/output.mjs | 6 ++ .../issue-10142-corejs3-usage/plugin.js | 7 ++ 20 files changed, 270 insertions(+), 42 deletions(-) create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs create mode 100644 packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js diff --git a/packages/babel-preset-env/src/polyfills/corejs3/entry-plugin.js b/packages/babel-preset-env/src/polyfills/corejs3/entry-plugin.js index 1d8d3df0a2..e30b284d8a 100644 --- a/packages/babel-preset-env/src/polyfills/corejs3/entry-plugin.js +++ b/packages/babel-preset-env/src/polyfills/corejs3/entry-plugin.js @@ -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, diff --git a/packages/babel-preset-env/src/polyfills/corejs3/usage-plugin.js b/packages/babel-preset-env/src/polyfills/corejs3/usage-plugin.js index 2b06088aa6..687a07c436 100644 --- a/packages/babel-preset-env/src/polyfills/corejs3/usage-plugin.js +++ b/packages/babel-preset-env/src/polyfills/corejs3/usage-plugin.js @@ -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, diff --git a/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules-chrome-71/output.mjs b/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules-chrome-71/output.mjs index 44a025d6a4..55d6e9dbd5 100644 --- a/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules-chrome-71/output.mjs +++ b/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules-chrome-71/output.mjs @@ -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'; diff --git a/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules/output.mjs b/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules/output.mjs index 16b33e2fd0..e761a4ac12 100644 --- a/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules/output.mjs +++ b/packages/babel-preset-env/test/fixtures/corejs3/entry-entries-modules/output.mjs @@ -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'; diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs new file mode 100644 index 0000000000..bd6332b8ba --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/input.mjs @@ -0,0 +1 @@ +import "core-js"; \ No newline at end of file diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json new file mode 100644 index 0000000000..786316f22e --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/options.json @@ -0,0 +1,14 @@ +{ + "presets": [ + [ + "env", + { + "useBuiltIns": "entry", + "corejs": 2, + "modules": false, + "targets": { "chrome": 65 } + } + ] + ], + "plugins": ["./plugin.js"] +} diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs new file mode 100644 index 0000000000..05407cabf9 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/output.mjs @@ -0,0 +1,6 @@ +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js new file mode 100644 index 0000000000..931322f3b7 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-entry/plugin.js @@ -0,0 +1,8 @@ +module.exports = () => ({ + visitor: { + ImportDeclaration(path) { + if (path.node.source.value === "core-js") return; + path.node.source.value = "MODIFIED"; + }, + }, +}); diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs new file mode 100644 index 0000000000..b24e09f0c2 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/input.mjs @@ -0,0 +1 @@ +new Map(); \ No newline at end of file diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json new file mode 100644 index 0000000000..d7ce4a4a72 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/options.json @@ -0,0 +1,6 @@ +{ + "presets": [ + ["env", { "useBuiltIns": "usage", "corejs": 2, "modules": false }] + ], + "plugins": ["./plugin.js"] +} diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs new file mode 100644 index 0000000000..2e074eea8b --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/output.mjs @@ -0,0 +1,6 @@ +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +new Map(); diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js new file mode 100644 index 0000000000..ddfe181cf6 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs2-usage/plugin.js @@ -0,0 +1,7 @@ +module.exports = () => ({ + visitor: { + ImportDeclaration(path) { + path.node.source.value = "MODIFIED"; + }, + }, +}); diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs new file mode 100644 index 0000000000..bd6332b8ba --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/input.mjs @@ -0,0 +1 @@ +import "core-js"; \ No newline at end of file diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json new file mode 100644 index 0000000000..79d8456fac --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/options.json @@ -0,0 +1,14 @@ +{ + "presets": [ + [ + "env", + { + "useBuiltIns": "entry", + "corejs": 3, + "modules": false, + "targets": { "chrome": 65 } + } + ] + ], + "plugins": ["./plugin.js"] +} diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs new file mode 100644 index 0000000000..da903d0b05 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/output.mjs @@ -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"; diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js new file mode 100644 index 0000000000..931322f3b7 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-entry/plugin.js @@ -0,0 +1,8 @@ +module.exports = () => ({ + visitor: { + ImportDeclaration(path) { + if (path.node.source.value === "core-js") return; + path.node.source.value = "MODIFIED"; + }, + }, +}); diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs new file mode 100644 index 0000000000..b24e09f0c2 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/input.mjs @@ -0,0 +1 @@ +new Map(); \ No newline at end of file diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json new file mode 100644 index 0000000000..3d488206b5 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/options.json @@ -0,0 +1,6 @@ +{ + "presets": [ + ["env", { "useBuiltIns": "usage", "corejs": 3, "modules": false }] + ], + "plugins": ["./plugin.js"] +} diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs new file mode 100644 index 0000000000..2e074eea8b --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/output.mjs @@ -0,0 +1,6 @@ +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +import "MODIFIED"; +new Map(); diff --git a/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js new file mode 100644 index 0000000000..ddfe181cf6 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/sanity/issue-10142-corejs3-usage/plugin.js @@ -0,0 +1,7 @@ +module.exports = () => ({ + visitor: { + ImportDeclaration(path) { + path.node.source.value = "MODIFIED"; + }, + }, +});