@mnrendra/chalk
v1.0.0
Published
Refactored chalk code to support both CommonJS (CJS) and ES Modules (ESM) with mixed exports.
Downloads
82
Maintainers
Readme
@mnrendra/chalk
Refactored chalk code to support both CommonJS (CJS) and ES Modules (ESM) with mixed exports. This allows users to import
or require
the module without needing to access the .default
property.
Benefits
- ✅ Auto-detection for
browser
andnode
platforms - ✅ Supports both CommonJS (CJS) and ES Modules (ESM)
- ✅ Mixed exports (no need to access
.default
for default value) - ✅ Minified distribution package
- ✅ Well-tested (100% code coverage)
- ✅ TypeScript source code for easier development
Reference
Refactored from the original source code since version v5.3.0, commit 4a10354. This module will be kept updated with the original source code; contributions are welcome. 🙏
Install
npm i @mnrendra/chalk
Usage
Using CommonJS
:
const main = require('@mnrendra/chalk')
const {
Chalk,
chalk,
chalkStderr,
// modifier
reset,
bold,
dim,
italic,
underline,
inverse,
hidden,
strikethrough,
overline,
// foreground
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
grey,
blackBright,
redBright,
greenBright,
yellowBright,
blueBright,
magentaBright,
cyanBright,
whiteBright,
// background
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
bgGray,
bgGrey,
bgBlackBright,
bgRedBright,
bgGreenBright,
bgYellowBright,
bgBlueBright,
bgMagentaBright,
bgCyanBright,
bgWhiteBright,
// visible
visible,
// models
hex,
rgb,
ansi256,
bgHex,
bgRgb,
bgAnsi256,
// style names
modifiers,
modifierNames,
foregroundColors,
foregroundColorNames,
backgroundColors,
backgroundColorNames,
colors,
colorNames,
// supports color
supportsColor,
supportsColorStderr
} = require('@mnrendra/chalk')
// from default export
console.log(main.bgRed.blue.strikethrough('abc'))
console.log(main.default.bgRed.blue.strikethrough('abc'))
// from named exports
console.log(chalk.bgRed.blue.strikethrough('abc'))
console.log(bgRed.blue.strikethrough('abc'))
console.log(new Chalk({ level: 3 }).bgRed.blue.strikethrough('abc'))
console.log(chalkStderr.bgRed.blue.strikethrough('abc'))
console.log(`${[...modifiers, ...colors]}` === `${[...modifierNames, ...foregroundColorNames, ...backgroundColorNames]}`)
Using ES Modules
:
import main, {
Chalk,
chalk,
chalkStderr,
// modifier
reset,
bold,
dim,
italic,
underline,
inverse,
hidden,
strikethrough,
overline,
// foreground
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
grey,
blackBright,
redBright,
greenBright,
yellowBright,
blueBright,
magentaBright,
cyanBright,
whiteBright,
// background
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
bgGray,
bgGrey,
bgBlackBright,
bgRedBright,
bgGreenBright,
bgYellowBright,
bgBlueBright,
bgMagentaBright,
bgCyanBright,
bgWhiteBright,
// visible
visible,
// models
hex,
rgb,
ansi256,
bgHex,
bgRgb,
bgAnsi256,
// style names
modifiers,
modifierNames,
foregroundColors,
foregroundColorNames,
backgroundColors,
backgroundColorNames,
colors,
colorNames,
// supports color
supportsColor,
supportsColorStderr
} from '@mnrendra/chalk'
// from default export
console.log(main.bgRed.blue.strikethrough('abc'))
// from named exports
console.log(chalk.bgRed.blue.strikethrough('abc'))
console.log(bgRed.blue.strikethrough('abc'))
console.log(new Chalk({ level: 3 }).bgRed.blue.strikethrough('abc'))
console.log(chalkStderr.bgRed.blue.strikethrough('abc'))
console.log(`${[...modifiers, ...colors]}` === `${[...modifierNames, ...foregroundColorNames, ...backgroundColorNames]}`)
For more details, refer to the original source code: https://github.com/chalk/chalk.
Types
import type {
ColorSupportLevel,
ColorSupport,
ColorInfo,
SupportsColor,
Options
} from '@mnrendra/chalk'
Contribute
Contributions are always welcome! Please open discussions here.
Special Thanks 🙇
- sindresorhus for creating the original source code.
- Qix- for being the active maintainer of the original source code.
- and all contributors who contributed to the original source code.