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 chalk with smaller and faster ansis #2845

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

webdiscus
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

This PR replaces the outdated chalk@4 (64 kB install size, 2 dependencies) package with smaller and faster ansis.

Benefits of ansis

  • ansis is compatible with chalk API, plus supports named import: import { red, bold, bgRgb } from 'ansis'
  • Installed package size only 10KB (no dependencies), see Compare the size of most popular packages
  • faster than chalk, see Benchmarks
  • using named import your code will be clean and more readable:
    - `See ${chalk.red('--help')} for a list of available commands.\n`,
    + `See ${red`--help`} for a list of available commands.\n`, // <= shorter and more readable

Packages that already use ansis

Alternatives

Today are actual two fastest and smallest libraries:: picocolors and ansis.
Both are recommended by the ES Tooling community .

picocolors vs ansis, see the Compare features

  • install size
    • picocolors - 6.3 kB
    • ansis - 10.3 kB
  • performance
    • using singe color, picocolors is fastest
    • using 2 and more color/styles, ansis is fastest
  • truecolor
    • picocolors doesn't supports the truecolor
    • ansis has rgb() and hex() functions, e.g. bgRgb(60, 190, 100).bold.rgb(0, 0, 0)(' Info ');
      ansis supports fallback to supported color space:
      TrueColor —> 256 colors —> 16 colors —> no colors (black & white)
  • chained syntax
    • picocolors doesn't supports chained syntax: picocolors.bgCyan(picocolors.bold(' SWC '))
    • ansis supports chained syntax: bgCyan.bold(' SWC ') <= more readable and shorter

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/ansis-nest-cli.git
cd ansis-nest-cli
git checkout switch-to-ansis
npm i
npm run build

Test changed files

  • commands/generate.command.ts

Cmd:

npm start

In console output will be displayed color text:
Generate a Graphol resolver declaration

Test changed files

  • commands/command.loader.ts
  • lib/ui/prefixes.ts (RGB prefix)

Cmd:

npm start hello

In console output will be displayed color text:
Invalid command

Test changed files

  • actions/info.action.ts

Cmd:

npm start i

In console output will be displayed color text:
~DevelProjectsGitHubForksansis-nest-cli (master x)• D npm start i

Test changed files

  • actions/new.action.ts
  • lib/ui/messages.ts

Cmd:

npm start new

In console output will be displayed color text:
CREATE apptsconfig build json (97 bytes)

@@ -63,7 +63,7 @@ export class TypeCheckerHost {
const reportWatchStatusCallback = (diagnostic: ts.Diagnostic) => {
if (diagnostic.messageText !== TSC_NO_ERRORS_MESSAGE) {
if ((diagnostic.messageText as string)?.includes('Found')) {
console.log(TSC_LOG_ERROR_PREFIX, chalk.red(diagnostic.messageText));
console.log(TSC_LOG_ERROR_PREFIX, red(diagnostic.messageText.toString()));
Copy link
Author

Choose a reason for hiding this comment

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

Ansis method accepts the string or number argument, but diagnostic.messageText is string | ts.DiagnosticMessageChain, therefore we need it to convert into string

console.info(
'OS Version :',
chalk.blue(osName(platform(), release()), release()),
blue(osName(platform(), release()) + release()),
Copy link
Author

Choose a reason for hiding this comment

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

Ansis method accepts only one argument as a string, therefore we need to concatenate two stings into one

Copy link
Member

@micalevisk micalevisk left a comment

Choose a reason for hiding this comment

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

LGTM

image

@kamilmysliwiec kamilmysliwiec changed the base branch from master to next December 17, 2024 09:19
@kamilmysliwiec
Copy link
Member

Thanks for your contribution!

I just changed the base branch to next and a few minor conflicts popped up. Would you like to resolve them?

@webdiscus
Copy link
Author

Thanks for your contribution!

I just changed the base branch to next and a few minor conflicts popped up. Would you like to resolve them?

yes of cause, I will resolve conflicts

@webdiscus
Copy link
Author

@kamilmysliwiec in the next branch the dependencies are broken, I can't install packages:

git checkout next
npm i

Error:

npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: @swc/[email protected]
npm error Found: [email protected]
npm error node_modules/chokidar
npm error   chokidar@"4.0.1" from the root project
npm error   peerOptional chokidar@"^4.0.0" from @angular-devkit/[email protected]
npm error   node_modules/@angular-devkit/core
npm error     @angular-devkit/core@"19.0.1" from the root project
npm error     @angular-devkit/core@"19.0.1" from @angular-devkit/[email protected]
npm error     node_modules/@angular-devkit/schematics
npm error       @angular-devkit/schematics@"19.0.1" from the root project
npm error       1 more (@angular-devkit/schematics-cli)
npm error     1 more (@angular-devkit/schematics-cli)
npm error
npm error Could not resolve dependency:
npm error peerOptional chokidar@"^3.5.1" from @swc/[email protected]
npm error node_modules/@swc/cli
npm error   dev @swc/cli@"0.5.1" from the root project
npm error
npm error Conflicting peer dependency: [email protected]
npm error node_modules/chokidar
npm error   peerOptional chokidar@"^3.5.1" from @swc/[email protected]
npm error   node_modules/@swc/cli
npm error     dev @swc/cli@"0.5.1" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.

Using npm i --force works, but this is unsave way.

@kamilmysliwiec
Copy link
Member

You can safely ignore that error and run npm i --legacy-peer-deps

@webdiscus
Copy link
Author

webdiscus commented Dec 17, 2024

@kamilmysliwiec I have applied my changes directly to the next branch and force pushed here. So my branch has no conflicts with the next branch.

But the checker (build-and-test) can't install it because of npm error.

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

Successfully merging this pull request may close these issues.

3 participants