Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pages/userland-migrations/kleur-to-util-styletext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Kleur to util.styleText

This recipe migrates from the external `kleur` package to Node.js built-in `util.styleText` API. It transforms `kleur` style calls and `kleur/colors` named style functions to the native Node.js styling functionality.

## Examples

```diff
- import kleur from 'kleur';
+ import { styleText } from 'node:util';
- console.log(kleur.red('Error'));
+ console.log(styleText('red', 'Error'));
- console.log(kleur.bold().red('Failure'));
+ console.log(styleText(['bold', 'red'], 'Failure'));
```

```diff
- import { green, dim } from 'kleur/colors';
+ import { styleText } from 'node:util';
- console.log(green('OK') + ' ' + dim(name));
+ console.log(styleText('green', 'OK') + ' ' + styleText('dim', name));
```

```diff
- import { bgRed, white } from 'kleur/colors';
+ import { styleText } from 'node:util';
- console.log(bgRed(white('FAIL')));
+ console.log(styleText(['bgRed', 'white'], 'FAIL'));
```

## Compatibility

- Removes the `kleur` dependency from package.json automatically
- Supports default, namespace, and CommonJS `kleur` imports
- Supports named imports and destructured requires from `kleur/colors`
- Leaves unsupported APIs such as `kleur.enabled` and `$` from `kleur/colors` unchanged

## Limitations

- Manual migration is required for `kleur.enabled`, `$` from `kleur/colors`, and other APIs that are not direct style functions

<!-- sync_to_learn: true -->
4 changes: 4 additions & 0 deletions site.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@
"link": "/learn/userland-migrations/correct-ts-specifiers",
"label": "Correct TypeScript Specifiers"
},
{
"link": "/learn/userland-migrations/kleur-to-util-styletext",
"label": "Kleur to util.styleText"
},
{
"link": "/learn/userland-migrations/mocha-to-node-test-runner",
"label": "Mocha to Node.js Test Runner"
Expand Down
Loading