-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support named exports in the ESM mode
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
Showing
6 changed files
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
picocolors.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |