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

refactor!: replace ansi-colors with smaller and faster ansis #5274

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

webdiscus
Copy link

@webdiscus webdiscus commented Dec 17, 2024

PR Checklist

Overview

This PR replaces the outdated ansi-colors (last update 3 years ago) package with smaller and faster ansis.

Benefits of ansis

  • Supports both ESM and CommonJS
  • Supports Bun and Deno runtimes
  • Supports TypeScript
  • Ansis is compatible with ansi-colors API, plus supports named import: import { red, yellow } from 'ansis'
  • Installed package size only 10 kB (ansi-colors 26 kB), see Compare the size of most popular packages
  • 7-10x faster than ansi-colors, see Benchmarks
  • Using named import your code will be clean and more readable:
    - console.error(ansi.red(`Error: ${result.error.message}`));
    + console.error(red`Error: ${result.error.message}`); // <= shorter and more readable
  • Supports ANSI 256 colors and TrueColor with Fallback: TrueColor → 256 colors → 16 colors → no colors
  • Supports environment variables NO_COLOR FORCE_COLOR and flags --no-color --color
  • Both Ansis and Picocolors (does't support named import) are recommended by the ES Tooling community as smallest and fastest packages.

Packages that already use ansis

Test

This PR does not require unit/integration tests.

But you can do the manual test.

Install the fork

git clone https://github.com/webdiscus/mocha.git
cd mocha
git checkout switch-to-ansis
npm i
npm run build

Test changed files

Cmd:

node ./bin/mocha.js -h

In console output will be displayed color text:
Other Options

Cmd:

node ./bin/mocha.js -h --no-color

In console output will be displayed b&w text:
Other Options

Cmd:

node ./bin/mocha.js -R

In console output will be displayed red text:
Error Not enough arguments following R

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Hey thanks for sending this in @webdiscus!

In the future, could you please file issues before sending nontrivial PRs? Per our contributing guidelines we prefer discussing changes before implementing. It can be hard to know whether an approach is the right one to spend development time on, and it's awkward telling someone to redo lots of work.

This PR is a good example of that: it's not clear to me why you'd want ansis (300k weekly downloads) over picocolors (31m weekly downloads).

I'll note that per https://github.com/webdiscus/ansis?tab=readme-ov-file#-unpacked-size, picocolors is both a smaller size and faster when applying single styles. But both of those are such negligibly small differences that I doubt they matter for Mocha's usage.

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft January 2, 2025 05:15
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.

2 participants