-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Swap chalk for turbocolor #2339
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
Conversation
import { RollupError } from '../../src/rollup/types'; | ||
import relativeId from '../../src/utils/relativeId'; | ||
|
||
if (!chalk.supportsColor) chalk.enabled = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is built-into turbocolor, so we don't need it here.
Note: One can still force color support on or off using
tc.enabled: boolean
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Will need to play around with it to see if I encounter any unexpected issues when run in different environments but code-wise, this looks fine!
Also 👍 for the reduced size! |
More precisely, the rollup CLI shrank from 107KB to 67KB 🎉 (but of course it is not minified or zipped). So basically, chalk was a third of the code of our CLI 😱 It seems the colors are a little off, though. Basically, they are much brighter than before which reduces legibility especially of yellow, cyan and green status texts on a terminal with a white background. Do you think something can be done about this? (First: chalk, second: TurboColor) |
It seems the issue is that turbocolor only knows the bright versions of the colors which also means that turbocolor is not a 1-to-1 drop-in replacement for chalk here. Any chance you will add non-bright color support? |
Legibility is suffering from bright color use
@lukastaegert Thanks, good catch! I've updated the PR and published a fix jorgebucaran/colorette@a0cfbec
Default/non-bright colors is the default now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This Pull Request updates dependency [rollup](https://github.com/rollup/rollup) from `v0.63.3` to `v0.63.4` <details> <summary>Release Notes</summary> ### [`v0.63.4`](https://github.com/rollup/rollup/blob/master/CHANGELOG.md#​0634) [Compare Source](rollup/rollup@v0.63.3...v0.63.4) *2018-07-20* * Use turbocolor instead of chalk ([#​2339](`https://github.com/rollup/rollup/pull/2339`)) --- </details> --- This PR has been generated by [Renovate Bot](https://renovatebot.com).
Hey guys, thought you might be interested to know that "turbocolor" has a serious memory leak. I know, because when @jorgebucaran took the code that I wrote for a PR that I submitted to @doowb's ansi-colors, and then, in an attempt to make his code look different and perform better in benchmarks, he removed, among other things, the part of the code that prevented the memory leak from happening. I spent a great deal of time thinking about how to get that code working using something like 1/10th of the code used by chalk. Getting the color stack to clear correctly was a real pain. @jorgebucaran my recommendation is that you do the same. Spend time figuring out how to solve problems, not on how to make my code look like you wrote it. I've been doing this a while, and there are exceedingly rare situations in which I would feel compelled to speak up about something like this, and this is one of them. Apologies to the rollup team for the intrusion and the drama. I won't continue this discussion here, so we can keep spirits high and stay focused on solving issues. I would be happy to answer questions or provide more detail if you want to contact me directly. |
FWIW I'll be introducing a new logger sometime soon that leverages some of chalk's feature set. While it's a few KB larger, nothing else comes close in terms of platform support and features. I wasn't able to weigh in on this PR before it was merged, but I'll be opening up discussion for a reversion. I also don't have any skin in the game as to the alleged plagiarism, only a personal preference of lib. @jonschlinkert objectively speaking, it would be helpful if you could highlight the part of the code that prevents the memory leak, that which when removed results in a speed increase. That in of itself would warrant reverting. @lukastaegert I believe this is worth a second look. |
@jonschlinkert Hi! I didn't know you, until now. This is the first time we interact together. I'm sorry you feel that I plagiarized your code. I did not. I'd never claim as mine something that isn't. Looks like this is the PR you may be talking about. This is the first time I see it. Turbocolor (previously clorox/clor) has been around since May, 2015. The basic gist was here. I did know about ansi-colors (and kleur too) and thoughtfully credited the authors in my release notes for turbocolor@1.0.0.
I credit them for using @lukastaegert @shellscape I haven't noticed any memory problems, or problems with each individual color function's |
I would definitely be interested in learning more about the potential memory issues. Otherwise I still prefer a targeted library to the universal solution chalk offers as it is packed with features we will never use and this code really seems to resist tree-shaking and we only use some colors + bold/underline anyway. Actually the most performant solution with the least overhead would be to just inline functions that add the few bits of formatting we need. Beyond that, as this is not really performance critical and I would not want to look up ANSI codes each time we have a new idea, something targeted like turbocolor or the other libraries mentioned seems about right to me. |
BTW interesting that |
@lukastaegert I looked more into it and I didn't find anything wrong with the internal stack array or with anything else. In turbocolor we keep a stack for every style function and clear it by popping all the ansi codes when the style function is invoked. We use the popped items to assemble the output stylized string. I didn't want to come back empty-handed, so I did a bit of research, and found leakage, a package that helps detect memory leaks and added a test to check against leaks. |
@lukastaegert @shellscape Howdy! I am adding my comment here because @jonschlinkert has apparently blocked me and that prevents me from replying directly to him, and as a result, I can't post on #2412. Following is my reply to each point he is making on the PR.
The load time test is wrong. It always favors the last module that is required. Turbocolor loads under 0.7ms in my benchmarks too, but only because I am requiring it last. I didn't realize that until now, so I'm going to investigate how to properly test load time or just remove that test until I know how to accurately and fairly do it. The rest of the benchmark results (theirs and mine) show that turbocolor is faster in every test except in the nested test. Why? As of ansi-colors@3.0.0 they're failing to remove the console.log(red(`red ${green("green") not red`)) I could do the same in turbocolor to get those extra ops.
In turbocolor we can do this instead: const redBgCyanBold = s => tc.red.bgCyan.bold(s)
console.log(redBgCyanBold("Hello")) This is how I've always done it since clorox and chalk before that. The docs don't say you can assign a color chain to a variable, therefore I don't consider it to be a bug. Now the real question: is this a feature we want? It's not difficult to add, but it will affect performance. BTW this issue was filed and discussed in jorgebucaran/colorette#21.
The internal stack is always cleared, that's why assigning a color chain to a variable doesn't keep the stack. If it did, it would be a memory leak, though. So, this can't be classified as a memory leak.
This is outdated. @lukastaegert brought this to my attention in #2339 (comment) and it's been fixed for a while alright. See: jorgebucaran/colorette@a0cfbec
Turbocolor doesn't have every chalk feature, e.g., 256/Truecolor color support, a tagged function, etc. Turbocolor is a targeted library, as opposed to a universal, multi-featured library like chalk. Having said that, chalk would also fail turbocolor's unit tests! The end result is exactly the same though. 🎉 The basic feature set is there and turbocolor can be used as a decent "drop in" replacement for chalk that will work for most cases as shown by this PR.
This is 100% true! Turbocolor only exports normal colors escape codes at the moment. 😄 |
@jorgebucaran it seems that Rollup has been made the center of a sort of feud. That's not exactly a place where we want to be. I think you've made your case very eloquently and we certainly appreciate all of the data you've provided. It's obvious that you and @jonschlinkert are passionate about this particular space. We'll take all of the information into consideration for choosing which library ships. Please do understand that the decision we make will not be one that favors (or promotes) one library over another, nor one author over another. It will be a decision that maintainers determine is best for the project and its future needs. I would ask that you let this topic of discussion be for now and allow the maintainers to focus our energies on other aspects of the library, lest this feed the feud and turn into endless bike-shedding. If there are comparisons or challenges you'd like to make towards other libraries, please do so on their respective repositories. Unfortunately third-party repos aren't the right place for that. @lukastaegert I believe it would be prudent to lock this PR to contributors for a short time. |
Summary
This PR swaps chalk for turbocolor. It ought to give us a humble perf boost as turbocolor loads >20x faster and applies styles >18x faster than chalk. Terminal color detection is also built-in, so I was able to simplify the code in one place.
I let some comments in the diff to explain a few other things.
Let me know if this looks good to you and if you'd like to take my contribution.
turbocolor (1.1kB) / chalk (20.2kB)
Cheers! 👋
@Rich-Harris @lukastaegert @TrySound