Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named exports in the ESM mode #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/benchmarks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
name: Benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
- run: npm install
- name: Ensure color support detection
run: node tests/environments.js
Expand Down
14 changes: 9 additions & 5 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ jobs:
strategy:
matrix:
node-version:
- 20
- 18
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build
- run: node tests/test.js
- run: node tests/esm.mjs
- run: node tests/types.mjs
legacy:
name: Node v${{ matrix.node-version }} (Legacy)
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
- 6
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
picocolors.mjs
tests/types.mjs
28 changes: 28 additions & 0 deletions build-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { readFile, writeFile } from "node:fs/promises"

let script = await readFile("picocolors.js", "utf8")

script = script.replace('require != null && require("tty")', '(await import("tty"))')

let lines = script.split(/\r?\n/)
lines.splice(-3, 3)
script = lines.join("\n")

script += `
let colors = createColors()
colors.createColors = createColors
let {
reset, bold, dim, italic, underline, inverse, hidden, strikethrough,
black, red, green, yellow, blue, magenta, cyan, white, gray,
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
} = colors

export {
isColorSupported, reset, bold, dim, italic, underline, inverse, hidden, strikethrough,
black, red, green, yellow, blue, magenta, cyan, white, gray,
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite, createColors
}
export default colors
`;

await writeFile("picocolors.mjs", script)
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
"name": "picocolors",
"version": "1.0.0",
"main": "./picocolors.js",
"module": "./picocolors.mjs",
"types": "./picocolors.d.ts",
"browser": {
"./picocolors.js": "./picocolors.browser.js"
},
"exports": {
".": {
"types": "./picocolors.d.ts",
"require": "./picocolors.js",
"import": "./picocolors.mjs"
}
},
"sideEffects": false,
"description": "The tiniest and the fastest library for terminal output formatting with ANSI colors",
"scripts": {
"build": "node build-esm.mjs && tsc --module esnext tests/types.mts",
"test": "node tests/test.js"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be more logical to put the compiling of the types tests during the test script? As it is a way to tests that types are correct.

Also maybe adding --noEmit option to not produce any compiled files of the test, no ?

},
"files": [
"picocolors.*",
"types.ts"
"*.ts"
],
"keywords": [
"terminal",
Expand All @@ -26,15 +35,16 @@
"repository": "alexeyraspopov/picocolors",
"license": "ISC",
"devDependencies": {
"ansi-colors": "^4.1.1",
"ansi-colors": "^4.1.3",
"benchmark": "^2.1.4",
"chalk": "^4.1.2",
"clean-publish": "^3.0.3",
"cli-color": "^2.0.0",
"colorette": "^2.0.12",
"kleur": "^4.1.4",
"nanocolors": "^0.2.12",
"prettier": "^2.4.1"
"clean-publish": "^3.4.5",
"cli-color": "^2.0.3",
"colorette": "^2.0.20",
"kleur": "^4.1.5",
"nanocolors": "^0.2.13",
"prettier": "^2.8.8",
"typescript": "^5.3.3"
},
"prettier": {
"printWidth": 100,
Expand Down
39 changes: 36 additions & 3 deletions picocolors.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { Colors } from "./types"
import { Colors, Formatter } from "./types"

declare const picocolors: Colors & { createColors: (enabled?: boolean) => Colors }
export { Colors }

export = picocolors
declare type Generator = (enabled?: boolean) => Colors

declare const picocolors: Colors & { createColors: Generator }

export default picocolors

export let isColorSupported: boolean
export let reset: Formatter
export let bold: Formatter
export let dim: Formatter
export let italic: Formatter
export let underline: Formatter
export let inverse: Formatter
export let hidden: Formatter
export let strikethrough: Formatter
export let black: Formatter
export let red: Formatter
export let green: Formatter
export let yellow: Formatter
export let blue: Formatter
export let magenta: Formatter
export let cyan: Formatter
export let white: Formatter
export let gray: Formatter
export let bgBlack: Formatter
export let bgRed: Formatter
export let bgGreen: Formatter
export let bgYellow: Formatter
export let bgBlue: Formatter
export let bgMagenta: Formatter
export let bgCyan: Formatter
export let bgWhite: Formatter

export let createColors: Generator
27 changes: 27 additions & 0 deletions tests/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pc from "../picocolors.mjs"
import { bold, createColors } from "../picocolors.mjs"
import assert from "node:assert"

test("import all", () => {
assert.equal(typeof pc, "object")
assert.equal(typeof pc.bold, "function")
assert.equal(typeof pc.createColors, "function")
})

test("import bold", () => {
assert.equal(typeof bold, "function")
})

test("import createColors", () => {
assert.equal(typeof createColors, "function")
})

function test(name, fn) {
try {
fn()
console.log(pc.green("✓ " + name))
} catch (error) {
console.log(pc.red("✗ " + name))
throw error
}
}
25 changes: 25 additions & 0 deletions tests/types.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pc from "../picocolors.mjs"
import { bold, createColors, Colors } from "../picocolors.mjs"

test("default export", () => {
let _result: string = pc.bold("test")
})

test("named exports", () => {
let _result: string = bold("test")
})

test("factory function", () => {
let _result1: Colors = pc.createColors()
let _result2: Colors = createColors()
})

function test(name: string, fn: () => void) : void {
try {
fn()
console.log(pc.green("✓ " + name))
} catch (error) {
console.log(pc.red("✗ " + name))
throw error
}
}