Refactor the supports-color code to TypeScript and ensure the output supports both CommonJS (CJS) and ES Modules (ESM) with mixed exports. This will allow users to import
or require
the module without needing to use .default
.
- ✅ Auto-detection for
browser
andnode
platforms - ✅ Supports both CommonJS (CJS) and ES Modules (ESM)
- ✅ Mixed exports (No need to call
.default
to get the default value) - ✅ Minified distribution package
- ✅ Well-tested (100% code coverage)
- ✅ TypeScript source code for development
npm i @mnrendra/chalk-supports-color
Using CommonJS
:
const supportsColor = require('@mnrendra/chalk-supports-color')
const {
stdout,
stderr,
createSupportsColor
} = require('@mnrendra/chalk-supports-color')
console.log(supportsColor) // It will print:
// {
// stdout: {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// },
// stderr: {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
// }
console.log(supportsColor.stdout) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(supportsColor.stderr) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(stdout) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(stderr) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(createSupportsColor({ isTTY: true })) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(supportsColor.default) // It will print the same as `supportsColor`.
Using ES Modules
:
import supportsColor, {
stdout,
stderr,
createSupportsColor
} from '@mnrendra/chalk-supports-color'
console.log(supportsColor) // It will print:
// {
// stdout: {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// },
// stderr: {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
// }
console.log(supportsColor.stdout) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(supportsColor.stderr) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(stdout) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(stderr) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(createSupportsColor({ isTTY: true })) // It will print:
// {
// level: 2,
// hasBasic: true,
// has256: true,
// has16m: false
// }
console.log(supportsColor.default) // It will print the same as `supportsColor`.
For more details, refer to the original source code: https://github.com/chalk/supports-color.
import type {
ColorSupportLevel,
ColorSupport,
ColorInfo,
SupportsColor,
Options
} from '@mnrendra/chalk-supports-color'
Contributions are always welcome! Please open discussions here.
- sindresorhus for creating the original source code.
- Qix- for being the active maintainer of the original source code.
Refactored from the original source code since version v9.4.0, commit c214314.