From 4e3613f96fb5bfaca487c948a10b371ec758bb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 23 Feb 2021 03:34:39 -0500 Subject: [PATCH] fix(babel-preset-app): avoid rely on preset-env utils (#8883) --- packages/babel-preset-app/package.json | 1 + packages/babel-preset-app/src/polyfills-plugin.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/babel-preset-app/package.json b/packages/babel-preset-app/package.json index d54cb2b0e7..f417b4f129 100644 --- a/packages/babel-preset-app/package.json +++ b/packages/babel-preset-app/package.json @@ -13,6 +13,7 @@ "@babel/compat-data": "^7.12.13", "@babel/core": "^7.12.17", "@babel/helper-compilation-targets": "^7.12.17", + "@babel/helper-module-imports": "^7.12.13", "@babel/plugin-proposal-class-properties": "^7.12.13", "@babel/plugin-proposal-decorators": "^7.12.13", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.13", diff --git a/packages/babel-preset-app/src/polyfills-plugin.js b/packages/babel-preset-app/src/polyfills-plugin.js index 2d0b32b476..811454232f 100644 --- a/packages/babel-preset-app/src/polyfills-plugin.js +++ b/packages/babel-preset-app/src/polyfills-plugin.js @@ -1,4 +1,18 @@ // Add polyfill imports to the first file encountered. +const { addSideEffect } = require('@babel/helper-module-imports') + +const modulePathMap = { + 'regenerator-runtime': 'regenerator-runtime/runtime.js' +} + +function getModulePath (mod) { + return modulePathMap[mod] || 'core-js/modules/' + mod + '.js' +} + +function createImport (path, mod) { + return addSideEffect(path, getModulePath(mod)) +} + module.exports = ({ types }) => { let entryFile return { @@ -12,7 +26,6 @@ module.exports = ({ types }) => { } const { polyfills } = state.opts - const { createImport } = require('@babel/preset-env/lib/utils') // Imports are injected in reverse order polyfills.slice().reverse().forEach((p) => {