-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathbuild-rolldown.ts
231 lines (206 loc) · 5.29 KB
/
build-rolldown.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Produces production builds and stitches together d.ts files.
To specify the package to build, simply pass its name and the desired build
formats to output (defaults to `buildOptions.formats` specified in that package,
or "esm"):
```
# name supports fuzzy match. will build all packages with name containing "core-base":
pnpm build core-base
# specify the format to output
pnpm build core --formats mjs
```
*/
import { spawnSync } from 'node:child_process'
import { existsSync, promises as fs } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { parseArgs } from 'node:util'
import { brotliCompressSync, gzipSync } from 'node:zlib'
import pc from 'picocolors'
import { rolldown } from 'rolldown'
import { buildTypings } from './build-types'
import { createConfigsForPackage } from './rolldown'
import {
targets as allTargets,
checkSizeDistFiles,
displaySize,
fuzzyMatchTarget,
readJson
} from './utils'
import type { OutputOptions } from 'rolldown'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const commit = spawnSync('git', ['rev-parse', '--short=7', 'HEAD'])
.stdout.toString()
.trim()
const { values, positionals: targets } = parseArgs({
allowPositionals: true,
options: {
formats: {
type: 'string',
short: 'f'
},
devOnly: {
type: 'boolean',
short: 'd'
},
prodOnly: {
type: 'boolean',
short: 'p'
},
withTypes: {
type: 'boolean',
short: 't'
},
sourceMap: {
type: 'boolean',
short: 's'
},
release: {
type: 'boolean'
},
all: {
type: 'boolean',
short: 'a'
},
size: {
type: 'boolean'
}
}
})
const {
formats: rawFormats,
all: buildAllMatching,
devOnly,
prodOnly,
withTypes: buildTypes,
sourceMap,
release: isRelease,
size
} = values
const formats = rawFormats?.split(',')
const sizeDir = path.resolve(__dirname, '../temp/size')
async function main() {
await run()
async function run() {
if (size) {
await fs.mkdir(sizeDir, { recursive: true })
}
const resolvedTargets = targets.length
? await fuzzyMatchTarget(targets, buildAllMatching)
: await allTargets()
await buildAll(resolvedTargets)
if (size) {
await checkAllSizes(resolvedTargets)
}
if (buildTypes) {
await buildTypings(resolvedTargets)
}
}
async function buildAll(targets: string[]) {
const start = performance.now()
let count = 0
for (const target of targets) {
const all = []
const configs = await createConfigsForTarget(target)
if (configs) {
all.push(
Promise.all(
configs.map(c =>
rolldown(c).then(bundle => {
return bundle.write(c.output as OutputOptions).then(() => {
return path.join(
'packages',
target,
'dist',
// @ts-expect-error
path.basename(c.output.file)
)
})
})
)
).then(files => {
files.forEach(f => {
count++
console.log(pc.gray('built: ') + pc.green(f))
})
})
)
}
await Promise.all(all)
}
console.log(
`\n${count} files built in ${(performance.now() - start).toFixed(2)}ms.`
)
}
async function createConfigsForTarget(target: string) {
const pkgDir = path.resolve(__dirname, `../packages/${target}`)
const pkg = await readJson(`${pkgDir}/package.json`)
// only build published packages for release
if (isRelease && pkg.private) {
return
}
// if building a specific format, do not remove dist.
if (!formats && existsSync(`${pkgDir}/dist`)) {
await fs.rm(`${pkgDir}/dist`, { recursive: true })
}
return createConfigsForPackage({
target,
commit,
formats,
prodOnly,
sourceMap
})
}
async function checkAllSizes(targets: string[]) {
if (devOnly) {
return
}
console.log()
for (const target of targets) {
await checkSize(target)
}
console.log()
}
async function checkSize(target: string) {
const pkgDir = path.resolve(`packages/${target}`)
const files = await checkSizeDistFiles(pkgDir)
for (const file of files) {
await checkFileSize(`${pkgDir}/dist/${file}`)
}
}
async function checkFileSize(filePath: string) {
if (!existsSync(filePath)) {
return
}
const file = await fs.readFile(filePath)
const filename = path.basename(filePath)
const gzipped = gzipSync(file)
const brotli = brotliCompressSync(file)
console.log(
`📦 ${pc.green(
pc.bold(path.basename(filePath))
)} - min: ${displaySize(file.length)} / gzip: ${displaySize(gzipped.length)} / brotli: ${displaySize(brotli.length)}`
)
if (size) {
const sizeContents = JSON.stringify(
{
file: filename,
size: file.length,
gzip: gzipped.length,
brotli: brotli.length
},
null,
2
)
await fs.writeFile(
path.resolve(sizeDir, `${filename}.json`),
sizeContents,
'utf-8'
)
}
}
}
main().catch(err => {
console.error(err)
process.exit(1)
})