Skip to content

Commit

Permalink
fix: workaround to resolve core-js path error bug (#5715) (#5719)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Nov 23, 2020
1 parent 509bba4 commit 7762d90
Showing 1 changed file with 33 additions and 7 deletions.
@@ -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);
}
},
},
Expand Down

0 comments on commit 7762d90

Please sign in to comment.