Skip to content

Commit

Permalink
fix: use chalk 4
Browse files Browse the repository at this point in the history
chalk 5 requires ESM, so it's a pain to use in this plugin.
chalk 4 is fine with CommonJS, so we can use that one and pin
the semver version to avoid chalk 5.

See also:

- [npm semver calculator](https://semver.npmjs.com/)
- [chalk 5 requires ESM](https://stackoverflow.com/questions/70309135/chalk-error-err-require-esm-require-of-es-module)
  • Loading branch information
jackdbd committed Jun 10, 2022
1 parent e0ac0be commit 48fb6f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"test:ci": "jest --config ./config/jest.cjs --rootDir ./ --ci --coverage"
},
"dependencies": {
"chalk": "^4.1.0",
"joi": "^17.0.0",
"workbox-build": "^6.5.3"
},
Expand Down
36 changes: 22 additions & 14 deletions src/lib.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { writeFile } = require('fs/promises')
const path = require('path')
// const chalk = require('chalk')
const chalk = require('chalk')
const { getManifest } = require('workbox-build/build/get-manifest')

const PREFIX = '[🔎 report precache manifest] '
Expand Down Expand Up @@ -29,16 +29,22 @@ const reportPrecacheManifest = async (config) => {

// color functions, just to add a level of indirection, so I can change colors
// in one place. This could also be useful if I decide to replace chalk.
// const cfCss = chalk.blue
// const cfJs = chalk.yellow
// const cfHtml = chalk.white
// const cfImages = chalk.green
// const cfFonts = chalk.red
const cfCss = (x) => x
const cfJs = (x) => x
const cfHtml = (x) => x
const cfImages = (x) => x
const cfFonts = (x) => x
const cfCss = chalk.blue
const cfJs = chalk.yellow
const cfHtml = chalk.white
const cfImages = chalk.green
const cfFonts = chalk.red
const cfOk = chalk.green
const cfWarn = chalk.yellow
const cfError = chalk.red
// const cfCss = (x) => x
// const cfJs = (x) => x
// const cfHtml = (x) => x
// const cfImages = (x) => x
// const cfFonts = (x) => x
// const cfOk = (x) => x
// const cfWarn = (x) => x
// const cfError = (x) => x

try {
// https://developer.chrome.com/docs/workbox/modules/workbox-build/#getmanifest-mode
Expand Down Expand Up @@ -121,13 +127,15 @@ const reportPrecacheManifest = async (config) => {
const filepath = path.join(eleventyOutputDirectory, reportName)
try {
await writeFile(filepath, JSON.stringify(entries, null, 4))
console.log(chalk.green(`${PREFIX}✅ wrote ${filepath}`))
console.log(cfOk(`${PREFIX}✅ wrote ${filepath}`))
} catch (err) {
console.warn(`${PREFIX}⚠️ could not write ${filepath}`)
console.warn(cfWarn(`${PREFIX}⚠️ could not write ${filepath}`))
}
} catch (err) {
console.error(
`${PREFIX}⚠️ could not retrieve precache manifest from ${eleventyOutputDirectory}`,
cfError(
`${PREFIX}⚠️ could not retrieve precache manifest from ${eleventyOutputDirectory}`
),
err
)
}
Expand Down

0 comments on commit 48fb6f5

Please sign in to comment.