|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | 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' |
19 | 25 | import { parse } from '@babel/parser' |
20 | 26 | import path from 'node:path' |
21 | 27 | import MagicString from 'magic-string' |
22 | 28 |
|
| 29 | +const ENUM_CACHE_PATH = 'temp/enum.json' |
| 30 | + |
23 | 31 | function evaluate(exp) { |
24 | 32 | return new Function(`return ${exp}`)() |
25 | 33 | } |
26 | 34 |
|
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() { |
31 | 38 | /** |
32 | | - * @type {{ ranges: Record<string, [number, number][]>, defines: Record<string, string> }} |
| 39 | + * @type {{ ranges: Record<string, [number, number][]>, defines: Record<string, string>, ids: string[] }} |
33 | 40 | */ |
34 | 41 | const enumData = { |
35 | 42 | ranges: {}, |
36 | | - defines: {} |
| 43 | + defines: {}, |
| 44 | + ids: [] |
37 | 45 | } |
38 | 46 |
|
39 | | - const knowEnums = new Set() |
40 | | - |
41 | 47 | // 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`]) |
43 | 49 | const files = [...new Set(stdout.split('\n').map(line => line.split(':')[0]))] |
44 | 50 |
|
45 | 51 | // 2. parse matched files to collect enum info |
@@ -70,7 +76,9 @@ export async function constEnum() { |
70 | 76 | for (let i = 0; i < decl.members.length; i++) { |
71 | 77 | const e = decl.members[i] |
72 | 78 | const id = decl.id.name |
73 | | - knowEnums.add(id) |
| 79 | + if (!enumData.ids.includes(id)) { |
| 80 | + enumData.ids.push(id) |
| 81 | + } |
74 | 82 | const key = e.id.type === 'Identifier' ? e.id.name : e.id.value |
75 | 83 | const fullKey = `${id}.${key}` |
76 | 84 | const init = e.initializer |
@@ -149,9 +157,29 @@ export async function constEnum() { |
149 | 157 | } |
150 | 158 | } |
151 | 159 |
|
| 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 | + |
152 | 180 | // construct a regex for matching re-exports of known const enums |
153 | 181 | const reExportsRE = new RegExp( |
154 | | - `export {[^}]*?\\b(${[...knowEnums].join('|')})\\b[^]*?}` |
| 182 | + `export {[^}]*?\\b(${enumData.ids.join('|')})\\b[^]*?}` |
155 | 183 | ) |
156 | 184 |
|
157 | 185 | // 3. during transform: |
@@ -190,7 +218,7 @@ export async function constEnum() { |
190 | 218 | if ( |
191 | 219 | spec.type === 'ExportSpecifier' && |
192 | 220 | spec.exportKind !== 'type' && |
193 | | - knowEnums.has(spec.local.name) |
| 221 | + enumData.ids.includes(spec.local.name) |
194 | 222 | ) { |
195 | 223 | const next = node.specifiers[i + 1] |
196 | 224 | if (next) { |
|
0 commit comments