Skip to content

Commit

Permalink
Support named exports in the ESM mode
Browse files Browse the repository at this point in the history
Allow importing single exported named functions:

    import { bold, green } from "picocolors"

Expose an ESM module with named exports: `picocolors.mjs`. Generate its
content from `picololors.js` by `build.mjs`. Test that the exports can
be imported by `test/esm.mjs`.

Also upgrade development dependencies. Keep the same major versions
of `chalk` and `clean-publish` not to break the legacy Node.js tests.

Also include tests with Node.js 20 and move versions older than 18
to the legacy group. Upgrade GitHub actions for modern Node.js.
  • Loading branch information
prantlf committed Feb 25, 2024
1 parent b626148 commit 8c8095e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
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
13 changes: 8 additions & 5 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ 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
legacy:
name: Node v${{ matrix.node-version }} (Legacy)
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
- 6
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
picocolors.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)
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
"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",
"test": "node tests/test.js"
},
"files": [
Expand All @@ -26,15 +35,15 @@
"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"
},
"prettier": {
"printWidth": 100,
Expand Down
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
}
}

0 comments on commit 8c8095e

Please sign in to comment.