Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: workaround to resolve core-js path error bug #5719

Merged
merged 1 commit into from Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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