diff --git a/build.config.ts b/build.config.ts index 3256888..db91d08 100644 --- a/build.config.ts +++ b/build.config.ts @@ -1,5 +1,6 @@ import fs from 'node:fs/promises' import { defineBuildConfig } from 'unbuild' +import colors from 'picocolors' export default defineBuildConfig({ entries: ['src/index'], @@ -20,21 +21,81 @@ export default defineBuildConfig({ }, hooks: { async 'build:done'(ctx) { - // Remove duplicated `dist/chunks/certificate.{cjs,mjs}` chunks - // as both are only dynamically imported by the entrypoints. The - // dynamic import can use the `.mjs` chunk only instead. - await fs.rm('dist/chunks/certificate.cjs') - const indexCjs = await fs.readFile('dist/index.cjs', 'utf8') - const editedIndexCjs = indexCjs.replace( - 'chunks/certificate.cjs', - 'chunks/certificate.mjs', - ) - if (indexCjs === editedIndexCjs) { - throw new Error( - 'Failed to find `dist/chunks/certificate.cjs` in `dist/index.cjs`', - ) + const isBuild = ctx.options.stub === false + if (isBuild) { + await dedupeCertificateChunk() + await patchCJS() } - await fs.writeFile('dist/index.cjs', editedIndexCjs) }, }, }) + +// Remove duplicated `dist/chunks/certificate.{cjs,mjs}` chunks +// as both are only dynamically imported by the entrypoints. The +// dynamic import can use the `.mjs` chunk only instead. +async function dedupeCertificateChunk() { + await fs.rm('dist/chunks/certificate.cjs') + const indexCjs = await fs.readFile('dist/index.cjs', 'utf8') + const editedIndexCjs = indexCjs.replace( + 'chunks/certificate.cjs', + 'chunks/certificate.mjs', + ) + if (indexCjs === editedIndexCjs) { + throw new Error( + 'Failed to find `dist/chunks/certificate.cjs` in `dist/index.cjs`', + ) + } + await fs.writeFile('dist/index.cjs', editedIndexCjs) +} + +/** + +It converts + +```ts +exports["default"] = viteBasicSslPlugin; +``` + +to + +```ts +module.exports = viteBasicSslPlugin; +module.exports["default"] = viteBasicSslPlugin; +``` +*/ +async function patchCJS() { + const indexPath = 'dist/index.cjs' + let code = await fs.readFile(indexPath, 'utf-8') + + const matchMixed = code.match(/\nexports\["default"\] = (\w+);/) + if (matchMixed) { + const name = matchMixed[1] + + const lines = code.trimEnd().split('\n') + + // search from the end to prepend `modules.` to `export[xxx]` + for (let i = lines.length - 1; i > 0; i--) { + if (lines[i].startsWith('exports')) lines[i] = 'module.' + lines[i] + else { + // at the beginning of exports, export the default function + lines[i] += `\nmodule.exports = ${name};` + break + } + } + + await fs.writeFile(indexPath, lines.join('\n')) + + console.log(colors.bold(`${indexPath} CJS patched`)) + } else { + const matchDefault = code.match(/\nmodule.exports = (\w+);/) + + if (matchDefault) { + code += `module.exports["default"] = ${matchDefault[1]};\n` + await fs.writeFile(indexPath, code) + console.log(colors.bold(`${indexPath} CJS patched`)) + } else { + console.error(colors.red(`${indexPath} CJS patch failed`)) + process.exit(1) + } + } +} diff --git a/package.json b/package.json index e93277c..318251e 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ }, "scripts": { "dev": "unbuild --stub", - "build": "unbuild && tsx scripts/patchCJS.ts", + "build": "unbuild", "test": "vitest run", "format": "prettier --write --cache .", "prepublishOnly": "npm run build", diff --git a/scripts/patchCJS.ts b/scripts/patchCJS.ts deleted file mode 100644 index 6c916b4..0000000 --- a/scripts/patchCJS.ts +++ /dev/null @@ -1,53 +0,0 @@ -/** - -It converts - -```ts -exports["default"] = viteBasicSslPlugin; -``` - -to - -```ts -module.exports = viteBasicSslPlugin; -module.exports["default"] = viteBasicSslPlugin; -``` -*/ - -import { readFileSync, writeFileSync } from 'fs' -import colors from 'picocolors' - -const indexPath = 'dist/index.cjs' -let code = readFileSync(indexPath, 'utf-8') - -const matchMixed = code.match(/\nexports\["default"\] = (\w+);/) -if (matchMixed) { - const name = matchMixed[1] - - const lines = code.trimEnd().split('\n') - - // search from the end to prepend `modules.` to `export[xxx]` - for (let i = lines.length - 1; i > 0; i--) { - if (lines[i].startsWith('exports')) lines[i] = 'module.' + lines[i] - else { - // at the beginning of exports, export the default function - lines[i] += `\nmodule.exports = ${name};` - break - } - } - - writeFileSync(indexPath, lines.join('\n')) - - console.log(colors.bold(`${indexPath} CJS patched`)) -} else { - const matchDefault = code.match(/\nmodule.exports = (\w+);/) - - if (matchDefault) { - code += `module.exports["default"] = ${matchDefault[1]};\n` - writeFileSync(indexPath, code) - console.log(colors.bold(`${indexPath} CJS patched`)) - } else { - console.error(colors.red(`${indexPath} CJS patch failed`)) - process.exit(1) - } -}