diff --git a/packages/umi-build-dev/src/plugins/afwebpack-config/lockCoreJSVersionPlugin.js b/packages/umi-build-dev/src/plugins/afwebpack-config/lockCoreJSVersionPlugin.js index 65a9563910da..bf5514ad81ff 100644 --- a/packages/umi-build-dev/src/plugins/afwebpack-config/lockCoreJSVersionPlugin.js +++ b/packages/umi-build-dev/src/plugins/afwebpack-config/lockCoreJSVersionPlugin.js @@ -1,21 +1,47 @@ import { dirname } from 'path'; import { winPath } from 'umi-utils'; +import * as types from '@babel/types'; const coreJSPath = dirname(require.resolve('core-js/package.json')); +function isUmiCoreJSPolyfill(oFilename, identifier) { + const filename = winPath(oFilename); + + return ( + (filename.endsWith('.umi/polyfills.js') || filename.endsWith('.umi-production/polyfills.js')) && + identifier.startsWith('core-js/') + ); +} + +function getReplacedCoreJSPath(path) { + return path.replace('core-js/', `${coreJSPath}/`); +} + export default function() { return { visitor: { + // handle import statements ImportDeclaration(path, state) { - const filename = winPath(state.filename); + const callPathNode = path.node; + + if ( + types.isLiteral(callPathNode.source) && + isUmiCoreJSPolyfill(state.filename, callPathNode.source.value) + ) { + callPathNode.source.value = getReplacedCoreJSPath(callPathNode.source.value); + } + }, + // handle require statements + CallExpression(path, state) { + const callPathNode = path.node; + if ( - filename.endsWith('.umi/polyfills.js') || - filename.endsWith('.umi-production/polyfills.js') + types.isIdentifier(callPathNode.callee) && + callPathNode.callee.name === 'require' && + types.isStringLiteral(callPathNode.arguments[0]) && + isUmiCoreJSPolyfill(state.filename, callPathNode.arguments[0].value) ) { - const { node } = path; - if (node.source.value.startsWith('core-js/')) { - node.source.value = node.source.value.replace('core-js/', `${coreJSPath}/`); - } + callPathNode.arguments[0].value = getReplacedCoreJSPath(callPathNode.arguments[0].value); } }, },