Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: switch from picocolors to ansis #474

Merged
merged 1 commit into from
Feb 11, 2025

Conversation

webdiscus
Copy link
Contributor

@webdiscus webdiscus commented Feb 11, 2025

Hello @antfu,

I'm the author of ansis, a smaller and faster drop-in replacement for Chalk.

I know, Chalk@4 was already used, which was very large and had 2 dependencies.
So Chalk was replaced with Picocolors, here is the commit from Dec 28, 2021.

But these changes have "degraded" the code readability:

- import c from 'chalk'
+ import c from 'picocolors'

- console.log(c.yellow.inverse.bold`\n  Vite  `, name, '\n')
+ console.log(c.yellow(c.inverse(c.bold('\n  Vite  '))), name, '\n')
...

Nested calls leads to "Parenthesis Hell" and it works 2x slower than chained syntax for the example above.

With Ansis, we can return to the original "chalk-like" clean and more readable syntax, while still being as small as a Picocolors:

- import c from 'picocolors'
+ import c from 'ansis'

- console.log(c.yellow(c.inverse(c.bold('\n  Vite  '))), name, '\n')
+ console.log(c.yellow.inverse.bold`\n  Vite  `, name, '\n')
...

Unpacked package size

Ansis is almost as small as Picocolors:

The difference with picocolors is 0.4 kB, but Ansis has the functionality of Chalk.
Picocolors doesn't handle important edge cases, so it is the smallest.

Today, the two smallest and fastest libraries are picocolors and ansis.
Both are recommended by the e18e community.

Popularity

  • picocolors ~60m+ weekly downloads.
  • ansis ~1.3m weekly downloads with steady growth. This is enough to be considered popular, though yet not as much as Picocolors.

Who already uses Ansis

unjs/webpackbar, NestJS, Sequelize, Salesforce CLI, Oclif, Facebook/stylex, Grafana

Test

The cmd npm run test:build outputs:
image

Copy link

pkg-pr-new bot commented Feb 11, 2025

Open in Stackblitz

npm i https://pkg.pr.new/unjs/unplugin@474

commit: 2440b78

@antfu
Copy link
Member

antfu commented Feb 11, 2025

I am actually looking for an alternative of picocolors because it's CJS-only. Thanks for your suggestion! I would be happy to replace all my packages' picocolors usage to ansis as well, if you also want to help on that :)

@webdiscus
Copy link
Contributor Author

@antfu, yes of course, I want help you :)

@sxzz sxzz merged commit de0ea62 into unjs:main Feb 11, 2025
12 checks passed
@webdiscus webdiscus deleted the feat-switch-to-ansis branch February 11, 2025 10:56
@panoply
Copy link

panoply commented Feb 11, 2025

Just chiming in here and apologies for any unwanted noise but wanted to vouch for Ansis. It’s a brilliant addition and just an all round fantastic module for ansi.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants