Skip to content
Merged
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
89 changes: 75 additions & 14 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs/promises'
import { defineBuildConfig } from 'unbuild'
import colors from 'picocolors'

export default defineBuildConfig({
entries: ['src/index'],
Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
53 changes: 0 additions & 53 deletions scripts/patchCJS.ts

This file was deleted.

Loading