-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.config.ts
More file actions
101 lines (88 loc) · 2.51 KB
/
build.config.ts
File metadata and controls
101 lines (88 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import fs from 'node:fs/promises'
import { defineBuildConfig } from 'unbuild'
import colors from 'picocolors'
export default defineBuildConfig({
entries: ['src/index'],
externals: ['vite'],
clean: true,
declaration: true,
rollup: {
emitCJS: true,
inlineDependencies: true,
esbuild: {
target: 'node18',
},
output: {
generatedCode: {
reservedNamesAsProps: false,
},
},
},
hooks: {
async 'build:done'(ctx) {
const isBuild = ctx.options.stub === false
if (isBuild) {
await dedupeCertificateChunk()
await patchCJS()
}
},
},
})
// 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)
}
}
}