On this page

🌐 Kleur to util.styleText

这个教程将外部 kleur 包迁移到 Node.js 内置的 util.styleText API。它会把 kleur 风格的调用和 kleur/colors 命名风格的函数转换为原生 Node.js 风格的功能。

🌐 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

- 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'));
- 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));
- import { bgRed, white } from 'kleur/colors';
+ import { styleText } from 'node:util';
- console.log(bgRed(white('FAIL')));
+ console.log(styleText(['bgRed', 'white'], 'FAIL'));

🌐 Compatibility

  • 自动从 package.json 中移除 kleur 依赖
  • 支持默认、命名空间和 CommonJS kleur 导入
  • 支持从 kleur/colors 的命名导入和解构 require
  • 对 kleur/colors 中不受支持的 API(如 kleur.enabled 和 $)保持不变

🌐 Limitations

  • kleur.enabled、$ 需要从 kleur/colors 手动迁移,以及其他不是直接样式函数的 API