🌐 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