-
-
Notifications
You must be signed in to change notification settings - Fork 245
/
build.ts
97 lines (85 loc) · 1.84 KB
/
build.ts
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
import { $ } from 'bun'
import { build, type Options } from 'tsup'
const tsupConfig: Options = {
entry: ['src/**/*.ts'],
splitting: false,
sourcemap: false,
clean: true,
bundle: false,
minify: false
// outExtension() {
// return {
// js: '.js'
// }
// }
} satisfies Options
await Promise.all([
// ? tsup esm
build({
outDir: 'dist',
format: 'esm',
target: 'node20',
cjsInterop: false,
...tsupConfig
}),
// ? tsup cjs
build({
outDir: 'dist/cjs',
format: 'cjs',
target: 'node20',
// dts: true,
...tsupConfig
})
])
// ? Fix mjs import
const glob = new Bun.Glob('./dist/**/*.mjs')
for await (const entry of glob.scan('.')) {
const content = await Bun.file(entry).text()
await Bun.write(
entry,
content
.replace(
// Named import
/(import|export)\s*\{([a-zA-Z0-9_,\s$]*)\}\s*from\s*['"]([a-zA-Z0-9./-]*[./][a-zA-Z0-9./-]*)['"]/g,
'$1{$2}from"$3.mjs"'
)
.replace(
// Default import
/(import|export) ([a-zA-Z0-9_$]+) from\s*['"]([a-zA-Z0-9./-]*[./][a-zA-Z0-9./-]*)['"]/g,
'$1 $2 from"$3.mjs"'
)
)
// await fs.writeFile(
// entry,
// (await fs.readFile(entry))
// .toString()
// .replaceAll(/require\("(.+)\.js"\);/g, 'require("$1.cjs");'),
// );
}
await $`tsc --project tsconfig.dts.json`
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist/bun',
minify: {
whitespace: true,
syntax: true,
identifiers: false
},
target: 'bun',
sourcemap: 'external',
external: [
'@sinclair/typebox',
'cookie',
'fast-decode-uri-component',
'memoirist'
]
})
await Promise.all([
$`cp dist/*.d.ts dist/cjs`,
$`cp dist/ws/*.d.ts dist/cjs/ws/`
])
await $`cp dist/index*.d.ts dist/bun`
// const fsMjs = Bun.file('dist/universal/fs.mjs')
// const fsMjsContent = await fsMjs.text()
// Bun.write(fsMjs, fsMjsContent.replace(`require("fs")`, `await import("fs")`))
process.exit()