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(father-build): babel runtime resolve logic #484

Merged
merged 5 commits into from May 25, 2022

Conversation

PeachScript
Copy link
Member

Use project @babel/runtime first

ref: ant-design/pro-components#5273

@PeachScript PeachScript requested a review from zombieJ May 24, 2022 12:48
@codecov
Copy link

codecov bot commented May 24, 2022

Codecov Report

Merging #484 (5d616a4) into master (2be23ea) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #484   +/-   ##
=======================================
  Coverage   75.31%   75.31%           
=======================================
  Files          18       18           
  Lines         559      559           
  Branches      199      199           
=======================================
  Hits          421      421           
  Misses        137      137           
  Partials        1        1           
Impacted Files Coverage Δ
packages/father-build/src/babel.ts 71.42% <ø> (ø)
packages/father-build/src/getBabelConfig.ts 93.75% <ø> (ø)
packages/father-build/src/getRollupConfig.ts 98.01% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2be23ea...5d616a4. Read the comment docs.

@@ -35,6 +36,14 @@ function transformImportLess2Css() {
}
}

function getBabelRuntimeVersion(cwd: string) {
try {
return require(require.resolve('@babel/runtime/package.json', { paths: [cwd] })).version;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to check what depencensies["@babel/runtime"] says. If someone, for example, specifies ^7.0.0 they could have 7.18.0 in their node_modules, however the generated code should be compatible with 7.0.0 because their users might have a version older than 7.18.0.

This is what's causing the current problems, and I don't think that this PR would fix them.

@@ -81,7 +82,8 @@ export default function(opts: IGetBabelConfigOpts) {
...(runtimeHelpers
? [[require.resolve('@babel/plugin-transform-runtime'), {
useESModules: isBrowser && (type === 'esm'),
version: require('@babel/runtime/package.json').version,
// use @babel/runtime version from project dependencies
version: require(`${opts.cwd}/package.json`).dependencies['@babel/runtime'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using try-catch with a fallback to undefined:

function getBabelRuntimeVersion(cwd) {
  try {
    return require(`${cwd}/package.json`).dependencies['@babel/runtime']
  } catch {}
}

This is because that require call might fail, for example if the user as an uncommon setup or if for some reason they put @babel/runtime in a different package.json.

By returning undefined in the error case, Babel will automatically output code that is compatible with every version starting from 7.0.0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was removed on purpose, because there has a validation logic before this and I forgot just now, at:

assert.ok(existsSync(pkgPath), `@babel/runtime dependency is required to use runtimeHelpers`);
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
assert.ok(
(pkg.dependencies || {})['@babel/runtime'],
`@babel/runtime dependency is required to use runtimeHelpers`
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your detailed comment ❤️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, that's good!

Thank you very much for your detailed comment ❤️

I'm happy to help 😄

@PeachScript PeachScript merged commit 6a13525 into master May 25, 2022
@delete-merged-branch delete-merged-branch bot deleted the hotfix/babel-runtime-resolve branch May 25, 2022 07:00
afc163 added a commit to ant-design/antd-tools that referenced this pull request Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants