Skip to content

Commit d2d3296

Browse files
yyx990803zhangzhonghe
authored andcommitted
build: reuse const enum data between concurrent rollup builds
1 parent 7b9a3da commit d2d3296

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const pkg = require(resolve(`package.json`))
3232
const packageOptions = pkg.buildOptions || {}
3333
const name = packageOptions.filename || path.basename(packageDir)
3434

35-
const [enumPlugin, enumDefines] = await constEnum()
35+
const [enumPlugin, enumDefines] = constEnum()
3636

3737
const outputConfigs = {
3838
'esm-bundler': {

scripts/build.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ nr build core --formats cjs
1717
*/
1818

1919
import fs from 'node:fs/promises'
20-
import { existsSync, readFileSync } from 'node:fs'
20+
import { existsSync, readFileSync, rmSync } from 'node:fs'
2121
import path from 'node:path'
2222
import minimist from 'minimist'
2323
import { gzipSync } from 'node:zlib'
@@ -27,6 +27,7 @@ import execa from 'execa'
2727
import { cpus } from 'node:os'
2828
import { createRequire } from 'node:module'
2929
import { targets as allTargets, fuzzyMatchTarget } from './utils.js'
30+
import { scanEnums } from './const-enum.js'
3031

3132
const require = createRequire(import.meta.url)
3233
const args = minimist(process.argv.slice(2))
@@ -42,12 +43,17 @@ const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
4243
run()
4344

4445
async function run() {
45-
if (!targets.length) {
46-
await buildAll(allTargets)
47-
checkAllSizes(allTargets)
48-
} else {
49-
await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
50-
checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
46+
const removeCache = scanEnums()
47+
try {
48+
if (!targets.length) {
49+
await buildAll(allTargets)
50+
checkAllSizes(allTargets)
51+
} else {
52+
await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
53+
checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
54+
}
55+
} finally {
56+
removeCache()
5157
}
5258
}
5359

scripts/const-enum.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,37 @@
1515
*/
1616

1717
import execa from 'execa'
18-
import { readFileSync } from 'node:fs'
18+
import {
19+
existsSync,
20+
readFileSync,
21+
rmSync,
22+
writeFile,
23+
writeFileSync
24+
} from 'node:fs'
1925
import { parse } from '@babel/parser'
2026
import path from 'node:path'
2127
import MagicString from 'magic-string'
2228

29+
const ENUM_CACHE_PATH = 'temp/enum.json'
30+
2331
function evaluate(exp) {
2432
return new Function(`return ${exp}`)()
2533
}
2634

27-
/**
28-
* @returns {Promise<[import('rollup').Plugin, Record<string, string>]>}
29-
*/
30-
export async function constEnum() {
35+
// this is called in the build script entry once
36+
// so the data can be shared across concurrent Rollup processes
37+
export function scanEnums() {
3138
/**
32-
* @type {{ ranges: Record<string, [number, number][]>, defines: Record<string, string> }}
39+
* @type {{ ranges: Record<string, [number, number][]>, defines: Record<string, string>, ids: string[] }}
3340
*/
3441
const enumData = {
3542
ranges: {},
36-
defines: {}
43+
defines: {},
44+
ids: []
3745
}
3846

39-
const knowEnums = new Set()
40-
4147
// 1. grep for files with exported const enum
42-
const { stdout } = await execa('git', ['grep', `export const enum`])
48+
const { stdout } = execa.sync('git', ['grep', `export const enum`])
4349
const files = [...new Set(stdout.split('\n').map(line => line.split(':')[0]))]
4450

4551
// 2. parse matched files to collect enum info
@@ -70,7 +76,9 @@ export async function constEnum() {
7076
for (let i = 0; i < decl.members.length; i++) {
7177
const e = decl.members[i]
7278
const id = decl.id.name
73-
knowEnums.add(id)
79+
if (!enumData.ids.includes(id)) {
80+
enumData.ids.push(id)
81+
}
7482
const key = e.id.type === 'Identifier' ? e.id.name : e.id.value
7583
const fullKey = `${id}.${key}`
7684
const init = e.initializer
@@ -149,9 +157,29 @@ export async function constEnum() {
149157
}
150158
}
151159

160+
// 3. save cache
161+
writeFileSync(ENUM_CACHE_PATH, JSON.stringify(enumData))
162+
163+
return () => {
164+
rmSync(ENUM_CACHE_PATH, { force: true })
165+
}
166+
}
167+
168+
/**
169+
* @returns {[import('rollup').Plugin, Record<string, string>]}
170+
*/
171+
export function constEnum() {
172+
if (!existsSync(ENUM_CACHE_PATH)) {
173+
throw new Error('enum cache needs to be initialized before creating plugin')
174+
}
175+
/**
176+
* @type {{ ranges: Record<string, [number, number][]>, defines: Record<string, string>, ids: string[] }}
177+
*/
178+
const enumData = JSON.parse(readFileSync(ENUM_CACHE_PATH, 'utf-8'))
179+
152180
// construct a regex for matching re-exports of known const enums
153181
const reExportsRE = new RegExp(
154-
`export {[^}]*?\\b(${[...knowEnums].join('|')})\\b[^]*?}`
182+
`export {[^}]*?\\b(${enumData.ids.join('|')})\\b[^]*?}`
155183
)
156184

157185
// 3. during transform:
@@ -190,7 +218,7 @@ export async function constEnum() {
190218
if (
191219
spec.type === 'ExportSpecifier' &&
192220
spec.exportKind !== 'type' &&
193-
knowEnums.has(spec.local.name)
221+
enumData.ids.includes(spec.local.name)
194222
) {
195223
const next = node.specifiers[i + 1]
196224
if (next) {

0 commit comments

Comments
 (0)