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

[babel 8] Use ESM-based chalk@5 #15792

Merged
merged 8 commits into from
Aug 25, 2023
Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jul 17, 2023

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I updated yarn-plugin-conditions to allow defining ESM dependencies, so that we can start updating other deps for Babel 8.

Would this be ok for you other maintainers? It means that you can only run with BABEL_8_BREAKING=true if you are developing in the monorepo in ESM mode (it's enough to run make use-esm once).

The commits marked as [15816] are from #15816, please review that PR first.

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 24, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55223/

liuxingbaoyu
liuxingbaoyu previously approved these changes Aug 25, 2023
Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

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

To be honest, because of these special handling of chalk@5, I would rather stay with the old version unless there is a security problem.🤦‍♂️

Comment on lines 1 to 14
export function setup() {
// chalk, which we bundle in @babel/standalone, relies on `window.navigator`
// for the browser build:
// https://github.com/chalk/chalk/blob/f399cd0ff69841e88cca89d43a49f1cc9ba2efd5/source/vendor/supports-color/browser.js#L4
// We only bundle Chalk 5 in local dev and in Babel 8, so we avoid this
// "polyfill" when releasing Babel 7 to make sure that we do not accidentally
// bundle Chalk 5.

if (process.env.BABEL_8_BREAKING) {
globalThis.navigator = {};
} else if (!process.env.IS_PUBLISH) {
global.navigator = {};
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we replace it with navigator || {} in rollup?
I remember someone seems to be using @babel/standalone in node, maybe it's not worth breaking them.
If you agree I can try to do this. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes please!

@nicolo-ribaudo
Copy link
Member Author

To be honest, because of these special handling of chalk@5, I would rather stay with the old version unless there is a security problem.🤦‍♂️

The complexity is only there because we are using both chalk 2 and chalk 5 at the same time, if it was only chalk 5 it would be as complex as any other version. I would rather not have Babel 7 holding back Babel 8 😬

The main advantages of using Chalk 5 are that:

  • when using ESM, yarn-plugin-conditions works much better and thus it can properly load the TS definitions. Additionally, it exports the Instance type that it needs: this makes it possible for us to avoid this ugly import form:
    import _chalk from "chalk";
    const chalk = _chalk as unknown as typeof import("chalk-BABEL_8_BREAKING-true");
    type Chalk =
    typeof import("chalk-BABEL_8_BREAKING-true").Instance extends new () => infer R
    ? R
    : never;
  • it's much smaller than chalk 4 (75% smaller)

@liuxingbaoyu
Copy link
Member

it's much smaller than chalk 4 (75% smaller)

Amazing! Now i like it! :)

@nicolo-ribaudo
Copy link
Member Author

Good :)

Do you want to do #15792 (comment) here on in a separate PR?

@liuxingbaoyu
Copy link
Member

It didn't look complicated, so I pushed it directly in this PR. :)

@liuxingbaoyu
Copy link
Member

I ran into a little strange thing, even if I just build babel7, this code is still there.🤔
2{ RGC%TBH0OSS6__R_%Z4

@nicolo-ribaudo
Copy link
Member Author

Unless you specify STRIP_BABEL_8_BREAKNG, the bundle will include both Babel 7 and Babel 8 code. Thus, I would also prefer to avoid the BABEL_8_BREAKING guard in the rollup config ans always replace navigator.

@nicolo-ribaudo
Copy link
Member Author

@liuxingbaoyu I pushed a commit to use a Babel plugin instead. Rollup's replace plugin had some problems, because:

  • by default, it does not replace things in member expressions
  • it was also replacing navigator in a JSON file one of our dependencies was importing

@liuxingbaoyu
Copy link
Member

it was also replacing navigator in a JSON file one of our dependencies was importing

Yes, I found this problem too.
Thanks!

@liuxingbaoyu
Copy link
Member

chalk/chalk#598 (comment)
I think there is still a problem.
Currently its color detection is completely broken in non-browsers for this reason.🤦‍♂️

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Aug 25, 2023

Well, Node.js will get the non-highlighted version but the result will still be ok. @babel/standalone is mostly meant to be used in browser, and while I agree that we should not break people using it in Node.js we should prioritize the in-browser experience.

This is already the current behavior for Babel 7: search for var stdoutColor = requireBrowser().stdout; and function requireBrowser() { in https://unpkg.com/@babel/[email protected]/babel.js, and you will see that it always return false. In Babel 8 / Chalk 5, it would at least return true in browsers that support colors.

Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

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

Sorry, I made a mistake. 😅
I thought code-frame and highlight would be affected too, now it looks fine.

@nicolo-ribaudo nicolo-ribaudo merged commit e43d8e7 into babel:main Aug 25, 2023
47 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the up-chalk-5 branch August 25, 2023 13:26
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Nov 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Dependency ⬆️ PR: Internal (next major) 🏠 A type of pull request used for our changelog categories for next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants