本方案将外部 kleur 包迁移到 Node.js 内置的 util.styleText API。它会将 kleur 样式调用以及 kleur/colors 中的具名样式函数转换为 Node.js 原生的样式功能。
- 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'));
- 自动从 package.json 中移除
kleur依赖 - 支持默认导入、命名空间导入和 CommonJS 的
kleur导入 - 支持来自
kleur/colors的具名导入和解构 require - 不直接支持的 API(如
kleur.enabled和kleur/colors中的$)保持不变
kleur.enabled、kleur/colors中的$以及其他非直接样式函数的 API 需要手动迁移