Package Exports
- nanocolors
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nanocolors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Nano Colors
A tiny and fast Node.js library for formatting terminal text with ANSI colors.
- It is 2 times faster than
chalk. Both loading and calls. - No dependencies. It takes 5 times less space in
node_modulesthanchalk. - Actively maintained. Used in many big projects like PostCSS or Browserslist.
- Auto-detects color support. You can also toggle color mode manually.
- Tree-shakable. We use a dual ESM/CJS package.
- Supports Deno and universal Node.js/browser projects.
import { green, bold } from 'nanocolors'
console.log(
green(`Task ${bold('1')} was finished`)
)
API was heavily inspired
by @jorgebucaran’s
colorette with few cool hacks
copied from @lukeed’s
kleur.
Benchmarks
Function calling time:
$ ./test/benchmark.js
chalk 11,608,010 ops/sec
cli-color 752,419 ops/sec
ansi-colors 3,601,857 ops/sec
kleur 15,185,239 ops/sec
kleur/colors 21,113,231 ops/sec
colorette 19,712,884 ops/sec
nanocolors 21,176,376 ops/secLibrary loading time:
$ ./test/loading.js
chalk 3.465 ms
cli-color 21.849 ms
ansi-colors 1.101 ms
kleur 1.628 ms
kleur/colors 0.508 ms
colorette 2.610 ms
nanocolors 0.486 msThe space in node_modules including sub-dependencies:
$ ./test/size.js
chalk 192 KB
cli-color 5120 KB
ansi-colors 40 KB
kleur 44 KB
colorette 32 KB
nanocolors 36 KBTest configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.8.
Replacing chalk
Replace import and use named exports:
- import chalk from 'chalk' + import { red, bold } from 'nanocolors'Unprefix calls:
- chalk.red(text) + red(text)Replace chains to nested calls:
- chalk.red.bold(text) + red(bold(text))
API
Individual Colors
Nano Colors exports functions:
| Colors | Background Colors | Modifiers |
|---|---|---|
black |
bgBlack |
dim |
red |
bgRed |
bold |
green |
bgGreen |
hidden |
yellow |
bgYellow |
italic |
blue |
bgBlue |
underline |
magenta |
bgMagenta |
|
cyan |
bgCyan |
reset |
white |
bgWhite |
|
gray |
Functions are not chainable. You need to wrap it inside each other:
import { black, bgYellow } from 'nanocolors'
console.log(bgYellow(black(' WARN ')))Functions will use colors only if Nano Colors auto-detect that current environment supports colors.
You can get support level in isColorSupported:
import { isColorSupported } from 'nanocolors'
if (isColorSupported) {
console.log('With colors')
}Conditional Support
You can manually switch colors on/off and override color support auto-detection:
import { createColors } from 'nanocolors'
const { red } = createColors(options.enableColors)On undefined argument, createColors will use value
from color support auto-detection.
Deno
Nano Colors has build-in Deno support.
import { red } from 'https://deno.land/x/nanocolors/mod.ts'