@akanjs/devkit 3.3.0-rc.36 → 3.3.0-rc.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/app/applicationBuildRunner.ts +7 -1
- package/app/dictionarySizes.ts +2 -5
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/storeBootstrap.ts +5 -1
- package/biome.base.json +1 -3
- package/codeAgent/skills/akan-page/SKILL.md +28 -7
- package/executors/clientRuntimeKeys.test.ts +72 -1
- package/executors/sysExecutor.ts +10 -8
- package/frontendBuild/allRoutesBuilder.ts +14 -7
- package/frontendBuild/clientEntryDiscovery.test.ts +193 -0
- package/frontendBuild/clientEntryDiscovery.ts +41 -8
- package/frontendBuild/devBarrelImports.test.ts +54 -0
- package/frontendBuild/devBarrelImports.ts +1 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +12 -4
- package/frontendBuild/dictionaryAssetBuilder.ts +20 -7
- package/frontendBuild/dictionaryConstants.ts +55 -1
- package/frontendBuild/dictionaryPacking.test.ts +58 -0
- package/frontendBuild/dictionaryPacking.ts +43 -0
- package/frontendBuild/dictionaryRoutes.test.ts +65 -0
- package/frontendBuild/dictionaryRoutes.ts +3 -1
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +18 -11
- package/frontendBuild/renderedEntries.ts +129 -0
- package/frontendBuild/routeHandoffs.test.ts +10 -0
- package/frontendBuild/routeHandoffs.ts +3 -1
- package/frontendBuild/routeRuntimePays.test.ts +7 -1
- package/lint/biomeBase.test.ts +23 -3
- package/package.json +3 -3
- package/quality/dictionaryKeys.test.ts +185 -2
- package/quality/dictionaryKeys.ts +82 -9
- package/quality/objectImportReport.ts +6 -3
- package/quality/translatorBindings.ts +187 -0
- package/transforms/barrelImportsPlugin.ts +6 -1
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +304 -0
- package/transforms/objectMemberImports.test.ts +289 -7
- package/transforms/objectMemberImports.ts +92 -58
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +72 -2
- package/transforms/objectModule.ts +31 -7
- package/workspace/scanInfo.ts +24 -0
|
@@ -283,4 +283,197 @@ describe("GraphClientEntryDiscovery", () => {
|
|
|
283
283
|
route.invalidate([file("lib/memo/index.ts")]);
|
|
284
284
|
expect(await route.discover([file("S.tsx")])).toEqual([file("lib/memo/Memo.Panel.tsx")]);
|
|
285
285
|
});
|
|
286
|
+
|
|
287
|
+
describe("read by export", () => {
|
|
288
|
+
const byExport = () =>
|
|
289
|
+
new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never, { byExport: true });
|
|
290
|
+
const client = (name: string) => `"use client";\nexport const ${name} = () => null;\n`;
|
|
291
|
+
const writeView = async (file: (name: string) => string) => {
|
|
292
|
+
for (const name of ["Remove", "Chart", "Editor", "Panel", "Badge"])
|
|
293
|
+
await Bun.write(file(`ui/${name}.tsx`), client(name));
|
|
294
|
+
await Bun.write(
|
|
295
|
+
file("Memo.View.tsx"),
|
|
296
|
+
[
|
|
297
|
+
'import { Badge } from "./ui/Badge";',
|
|
298
|
+
'import { Chart } from "./ui/Chart";',
|
|
299
|
+
'import { Remove } from "./ui/Remove";',
|
|
300
|
+
"const Title = () => <Badge />;",
|
|
301
|
+
"export const General = () => [<Title />, <Remove />];",
|
|
302
|
+
"export const Stats = () => <Chart />;",
|
|
303
|
+
'export const Rich = async () => (await import("./ui/Editor")).Editor;',
|
|
304
|
+
"",
|
|
305
|
+
].join("\n"),
|
|
306
|
+
);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
test("a route reaches the client files of the export its page names, not of the whole server file", async () => {
|
|
310
|
+
const file = await tempDir();
|
|
311
|
+
await writeView(file);
|
|
312
|
+
await Bun.write(file("general.tsx"), 'import { General } from "./Memo.View";\nexport default General;\n');
|
|
313
|
+
await Bun.write(file("stats.tsx"), 'import { Stats as S } from "./Memo.View";\nexport default () => <S />;\n');
|
|
314
|
+
await Bun.write(file("rich.tsx"), 'import * as View from "./Memo.View";\nexport default View.Rich;\n');
|
|
315
|
+
await Bun.write(file("all.tsx"), 'import * as View from "./Memo.View";\nexport default () => View;\n');
|
|
316
|
+
const discovery = byExport();
|
|
317
|
+
const whole = ["Badge", "Chart", "Editor", "Remove"].map((name) => file(`ui/${name}.tsx`));
|
|
318
|
+
|
|
319
|
+
expect(await discovery.discover([file("general.tsx")])).toEqual(whole);
|
|
320
|
+
expect(await discovery.discoverRendered([file("general.tsx")])).toEqual([
|
|
321
|
+
file("ui/Badge.tsx"),
|
|
322
|
+
file("ui/Remove.tsx"),
|
|
323
|
+
]);
|
|
324
|
+
expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
|
|
325
|
+
expect(await discovery.discoverRendered([file("rich.tsx")])).toEqual([file("ui/Editor.tsx")]);
|
|
326
|
+
expect(await discovery.discoverRendered([file("all.tsx")])).toEqual(whole);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("a second route asking the same export gets the same answer, and a saved file is read again", async () => {
|
|
330
|
+
const file = await tempDir();
|
|
331
|
+
await writeView(file);
|
|
332
|
+
await Bun.write(file("stats.tsx"), 'import { Stats } from "./Memo.View";\nexport default Stats;\n');
|
|
333
|
+
await Bun.write(file("board.tsx"), 'import { Stats } from "./Memo.View";\nexport default () => <Stats />;\n');
|
|
334
|
+
const discovery = byExport();
|
|
335
|
+
|
|
336
|
+
expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
|
|
337
|
+
expect(await discovery.discoverRendered([file("board.tsx")])).toEqual([file("ui/Chart.tsx")]);
|
|
338
|
+
|
|
339
|
+
await Bun.write(
|
|
340
|
+
file("Memo.View.tsx"),
|
|
341
|
+
'import { Panel } from "./ui/Panel";\nexport const Stats = () => <Panel />;\n',
|
|
342
|
+
);
|
|
343
|
+
discovery.invalidate([file("Memo.View.tsx")]);
|
|
344
|
+
expect(await discovery.discoverRendered([file("board.tsx")])).toEqual([file("ui/Panel.tsx")]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("without the setting, a route reaches what the whole server file imports", async () => {
|
|
348
|
+
const file = await tempDir();
|
|
349
|
+
await writeView(file);
|
|
350
|
+
await Bun.write(file("stats.tsx"), 'import { Stats } from "./Memo.View";\nexport default Stats;\n');
|
|
351
|
+
const discovery = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
|
|
352
|
+
|
|
353
|
+
expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual(
|
|
354
|
+
["Badge", "Chart", "Editor", "Remove"].map((name) => file(`ui/${name}.tsx`)),
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test("a client file registered by a top-level statement is reached by every route evaluating the module", async () => {
|
|
359
|
+
const file = await tempDir();
|
|
360
|
+
await writeView(file);
|
|
361
|
+
await Bun.write(file("registry.ts"), "export const parts: unknown[] = [];\n");
|
|
362
|
+
await Bun.write(
|
|
363
|
+
file("Memo.Zone.tsx"),
|
|
364
|
+
[
|
|
365
|
+
'import { parts } from "./registry";',
|
|
366
|
+
'import { Chart } from "./ui/Chart";',
|
|
367
|
+
'import { Panel } from "./ui/Panel";',
|
|
368
|
+
"parts.push(Panel);",
|
|
369
|
+
"export const Stats = () => <Chart />;",
|
|
370
|
+
"export const Plain = () => null;",
|
|
371
|
+
"",
|
|
372
|
+
].join("\n"),
|
|
373
|
+
);
|
|
374
|
+
await Bun.write(file("plain.tsx"), 'import { Plain } from "./Memo.Zone";\nexport default Plain;\n');
|
|
375
|
+
|
|
376
|
+
expect(await byExport().discoverRendered([file("plain.tsx")])).toEqual([file("ui/Panel.tsx")]);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("a name asked through `export *` is followed into the file that holds it, and an unknown name takes the whole file", async () => {
|
|
380
|
+
const file = await tempDir();
|
|
381
|
+
await writeView(file);
|
|
382
|
+
await Bun.write(file("index.ts"), 'export * from "./Memo.View";\nexport * from "./other";\n');
|
|
383
|
+
await Bun.write(file("other.tsx"), 'import { Panel } from "./ui/Panel";\nexport const Side = () => <Panel />;\n');
|
|
384
|
+
await Bun.write(
|
|
385
|
+
file("legacy.js"),
|
|
386
|
+
'const { Panel } = require("./ui/Panel");\nmodule.exports = { Old: Panel };\n',
|
|
387
|
+
);
|
|
388
|
+
await Bun.write(file("stats.tsx"), 'import { Stats } from "./index";\nexport default Stats;\n');
|
|
389
|
+
await Bun.write(file("old.tsx"), 'import { Old } from "./legacy";\nexport default Old;\n');
|
|
390
|
+
const discovery = byExport();
|
|
391
|
+
|
|
392
|
+
expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
|
|
393
|
+
expect(await discovery.discoverRendered([file("old.tsx")])).toEqual([file("ui/Panel.tsx")]);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test("an import swapped or dropped for the walk is swapped or dropped by export too", async () => {
|
|
397
|
+
const file = await tempDir();
|
|
398
|
+
await writeView(file);
|
|
399
|
+
await Bun.write(
|
|
400
|
+
file("Shell.tsx"),
|
|
401
|
+
'import { Panel } from "./ui/Panel";\nexport const General = () => <Panel />;\n',
|
|
402
|
+
);
|
|
403
|
+
await Bun.write(
|
|
404
|
+
file("page.tsx"),
|
|
405
|
+
'import { General } from "./Memo.View";\nimport { Chart } from "./ui/Chart";\nexport default () => [<General />, <Chart />];\n',
|
|
406
|
+
);
|
|
407
|
+
const discovery = byExport();
|
|
408
|
+
const importsAs = new Map([
|
|
409
|
+
[
|
|
410
|
+
file("page.tsx"),
|
|
411
|
+
new Map<string, string | null>([
|
|
412
|
+
["./Memo.View", "./Shell"],
|
|
413
|
+
["./ui/Chart", null],
|
|
414
|
+
]),
|
|
415
|
+
],
|
|
416
|
+
]);
|
|
417
|
+
|
|
418
|
+
expect(await discovery.discoverRendered([file("page.tsx")], { importsAs })).toEqual([file("ui/Panel.tsx")]);
|
|
419
|
+
expect(await discovery.discoverRendered([file("page.tsx")])).toEqual(
|
|
420
|
+
["Badge", "Chart", "Remove"].map((name) => file(`ui/${name}.tsx`)),
|
|
421
|
+
);
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
test("a page naming a part of the object a wrapper copies in reaches that part's file and none of the wrapper's own, until the copied object or its barrel moves it", async () => {
|
|
426
|
+
const dir = await realpath(await mkdtemp(path.join(os.tmpdir(), "akan-discovery-")));
|
|
427
|
+
dirs.push(dir);
|
|
428
|
+
const file = (name: string) => path.join(dir, name);
|
|
429
|
+
const client = (name: string) => `"use client";\nexport const ${name} = () => null;\n`;
|
|
430
|
+
const fieldIndex = (text: string) =>
|
|
431
|
+
`import { Text } from "./${text}";\nexport const Field = Object.assign(() => null, { Text });\n`;
|
|
432
|
+
const wrapper = [
|
|
433
|
+
'import { memo } from "react";',
|
|
434
|
+
'import { Field as BaseField } from "@demo/ui";',
|
|
435
|
+
'import { Editor } from "./Editor";',
|
|
436
|
+
"const Rich = memo(() => <Editor />);",
|
|
437
|
+
"export const Field = Object.assign(() => null, BaseField, { Rich });",
|
|
438
|
+
].join("\n");
|
|
439
|
+
const pageOf = (wrap: string, member: string) =>
|
|
440
|
+
`import { Field } from "./wrap/${wrap}";\nexport const Page = () => <Field.${member} />;\n`;
|
|
441
|
+
await Bun.write(file("ui/index.ts"), 'export * from "./Field";\n');
|
|
442
|
+
await Bun.write(file("ui/Field/index.tsx"), fieldIndex("Text"));
|
|
443
|
+
await Bun.write(file("ui/FieldNext/index.tsx"), fieldIndex("TextLast"));
|
|
444
|
+
for (const text of ["Field/Text", "Field/TextNext", "FieldNext/TextLast"])
|
|
445
|
+
await Bun.write(file(`ui/${text}.tsx`), client("Text"));
|
|
446
|
+
await Bun.write(file("wrap/Field.tsx"), wrapper);
|
|
447
|
+
await Bun.write(file("wrap/ClientField.tsx"), `"use client";\n${wrapper}`);
|
|
448
|
+
await Bun.write(file("wrap/Editor.tsx"), client("Editor"));
|
|
449
|
+
await Bun.write(file("Text.tsx"), pageOf("Field", "Text"));
|
|
450
|
+
await Bun.write(file("Rich.tsx"), pageOf("Field", "Rich"));
|
|
451
|
+
await Bun.write(file("ClientText.tsx"), pageOf("ClientField", "Text"));
|
|
452
|
+
await Bun.write(file("ClientRich.tsx"), pageOf("ClientField", "Rich"));
|
|
453
|
+
const entries: Record<string, string> = {
|
|
454
|
+
"@demo/ui": "ui/index.ts",
|
|
455
|
+
"@demo/ui/Field": "ui/Field/index.tsx",
|
|
456
|
+
"@demo/ui/FieldNext": "ui/FieldNext/index.tsx",
|
|
457
|
+
};
|
|
458
|
+
const resolvePackage = async (pkgName: string) => {
|
|
459
|
+
const entry = entries[pkgName];
|
|
460
|
+
return entry ? { pkgName, entryFile: file(entry), pkgDir: file("ui") } : null;
|
|
461
|
+
};
|
|
462
|
+
const route = new GraphClientEntryDiscovery({ barrelImports: ["@demo/ui"] }, resolvePackage as never, {
|
|
463
|
+
packageRoot: dir,
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/Field/Text.tsx")]);
|
|
467
|
+
expect(await route.discover([file("Rich.tsx")])).toEqual([file("ui/Field/Text.tsx"), file("wrap/Editor.tsx")]);
|
|
468
|
+
expect(await route.discover([file("ClientText.tsx")])).toEqual([file("ui/Field/Text.tsx")]);
|
|
469
|
+
expect(await route.discover([file("ClientRich.tsx")])).toEqual([file("wrap/ClientField.tsx")]);
|
|
470
|
+
|
|
471
|
+
await Bun.write(file("ui/Field/index.tsx"), fieldIndex("TextNext"));
|
|
472
|
+
route.invalidate([file("ui/Field/index.tsx")]);
|
|
473
|
+
expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/Field/TextNext.tsx")]);
|
|
474
|
+
|
|
475
|
+
await Bun.write(file("ui/index.ts"), 'export * from "./FieldNext";\n');
|
|
476
|
+
route.invalidate([file("ui/index.ts")]);
|
|
477
|
+
expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/FieldNext/TextLast.tsx")]);
|
|
478
|
+
});
|
|
286
479
|
});
|
|
@@ -4,8 +4,10 @@ import { SOURCE_EXTS } from "../akanApp/devHostPolicy";
|
|
|
4
4
|
import type { App } from "../commandDecorators";
|
|
5
5
|
import { BarrelAnalyzer, type BarrelExportMap } from "../transforms/barrelAnalyzer";
|
|
6
6
|
import { createTsconfigPackageResolver, rewriteBarrelImports } from "../transforms/barrelImportsPlugin";
|
|
7
|
+
import { ExportReach } from "../transforms/exportReach";
|
|
7
8
|
import { ObjectMemberImports } from "../transforms/objectMemberImports";
|
|
8
9
|
import type { AkanConfig, ClientEntryDiscovery, DiscoverOptions, ScannedImport } from "./clientBuildTypes";
|
|
10
|
+
import { RenderedEntries, type RenderedFile } from "./renderedEntries";
|
|
9
11
|
|
|
10
12
|
const USE_CLIENT_RE = /^\s*(?:\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/\s*|\/\/[^\n]*\n\s*)*["']use client["']/;
|
|
11
13
|
const EXPORT_STAR_RE = /^\s*export\s*\*/m;
|
|
@@ -16,10 +18,7 @@ const AKANJS_NODE_MODULE_RE = /[\\/]node_modules[\\/]akanjs[\\/]/;
|
|
|
16
18
|
const NON_SOURCE_EXT_RE = /\.(css|scss|sass|less|json|svg|png|jpe?g|webp|gif|avif|ico|woff2?|ttf|otf|mp3|mp4|wav)$/i;
|
|
17
19
|
type PackageResolver = Awaited<ReturnType<typeof createTsconfigPackageResolver>>;
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
isClientEntry: boolean;
|
|
21
|
-
imports: ScannedImport[];
|
|
22
|
-
}
|
|
21
|
+
type FileFacts = RenderedFile;
|
|
23
22
|
|
|
24
23
|
interface Lookup<T> {
|
|
25
24
|
epoch: number;
|
|
@@ -68,24 +67,37 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
68
67
|
//? Bumped by every invalidation: a walk that began before one computed from facts it may have read stale, so it
|
|
69
68
|
//? answers its caller but caches nothing (a registry check walks this instance beside the slow lane's route builds).
|
|
70
69
|
#epoch = 0;
|
|
70
|
+
#rendered: RenderedEntries | null;
|
|
71
71
|
|
|
72
72
|
constructor(
|
|
73
73
|
akanConfig: Pick<AkanConfig, "barrelImports">,
|
|
74
74
|
resolvePackage: PackageResolver,
|
|
75
|
-
{ packageRoot }: { packageRoot?: string } = {},
|
|
75
|
+
{ packageRoot, byExport = false }: { packageRoot?: string; byExport?: boolean } = {},
|
|
76
76
|
) {
|
|
77
77
|
this.#akanConfig = akanConfig;
|
|
78
78
|
this.#resolvePackage = resolvePackage;
|
|
79
|
+
this.#rendered = byExport
|
|
80
|
+
? new RenderedEntries({
|
|
81
|
+
fileOf: async (file) => (shouldSkipNodeModule(file) ? null : await this.#facts(file)),
|
|
82
|
+
resolve: async (spec, fromDir) =>
|
|
83
|
+
NON_SOURCE_EXT_RE.test(spec) ? null : await this.#resolveSpecifier(spec, fromDir),
|
|
84
|
+
epoch: () => this.#epoch,
|
|
85
|
+
})
|
|
86
|
+
: null;
|
|
79
87
|
this.#analyzer = new BarrelAnalyzer({ resolvePackage, ...(packageRoot ? { packageRoot } : {}) });
|
|
80
88
|
this.#objects = packageRoot
|
|
81
|
-
? new ObjectMemberImports({
|
|
89
|
+
? new ObjectMemberImports({
|
|
90
|
+
resolve: ObjectMemberImports.resolverOf(resolvePackage),
|
|
91
|
+
barrels: { names: akanConfig.barrelImports, analyzer: this.#analyzer },
|
|
92
|
+
})
|
|
82
93
|
: null;
|
|
83
94
|
}
|
|
84
95
|
|
|
85
96
|
//? An app narrows as its bundles do, so discovery reaches the member files they load.
|
|
86
|
-
static async create(app: App): Promise<GraphClientEntryDiscovery> {
|
|
97
|
+
static async create(app: App, { byExport = false } = {}): Promise<GraphClientEntryDiscovery> {
|
|
87
98
|
return new GraphClientEntryDiscovery(await app.getConfig(), await createTsconfigPackageResolver(app), {
|
|
88
99
|
packageRoot: app.workspace.workspaceRoot,
|
|
100
|
+
byExport,
|
|
89
101
|
});
|
|
90
102
|
}
|
|
91
103
|
|
|
@@ -101,6 +113,20 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
101
113
|
return Array.from(entries).sort();
|
|
102
114
|
}
|
|
103
115
|
|
|
116
|
+
//? Without `byExport`, and never beyond it, this is what `discover` answers.
|
|
117
|
+
async discoverRendered(
|
|
118
|
+
seeds: string[],
|
|
119
|
+
{ importsAs, serverModules }: DiscoverOptions & { serverModules?: string[] } = {},
|
|
120
|
+
): Promise<string[]> {
|
|
121
|
+
const within = await this.discover(seeds, { importsAs });
|
|
122
|
+
if (!this.#rendered) return within;
|
|
123
|
+
return await this.#rendered.of(seeds, {
|
|
124
|
+
serverModules: serverModules ?? (await this.serverModules(seeds, { importsAs })),
|
|
125
|
+
within,
|
|
126
|
+
importsAs,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
104
130
|
async serverModules(seeds: string[], { importsAs }: DiscoverOptions = {}): Promise<string[]> {
|
|
105
131
|
const seen = new Set<string>();
|
|
106
132
|
const queue = seeds.map((seed) => path.resolve(seed));
|
|
@@ -147,6 +173,7 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
147
173
|
// Reachable-entry results are transitive, so a changed child invalidates every ancestor.
|
|
148
174
|
this.#reachableEntriesCache.clear();
|
|
149
175
|
this.#swappedEntriesCaches = new WeakMap();
|
|
176
|
+
this.#rendered?.forget();
|
|
150
177
|
for (const [barrel, before] of this.#analyzer.forget(files))
|
|
151
178
|
if (!this.#reread.has(barrel)) this.#reread.set(barrel, before);
|
|
152
179
|
this.#forgetObjects(files);
|
|
@@ -221,7 +248,11 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
221
248
|
if (content === null) return null;
|
|
222
249
|
this.#readFiles.add(absPath);
|
|
223
250
|
if (USE_CLIENT_RE.test(content)) return { isClientEntry: !this.#exportsOnlyHooks(content), imports: [] };
|
|
224
|
-
|
|
251
|
+
const rewritten = await this.#rewrite(content, absPath);
|
|
252
|
+
const imports = this.#scanImports(rewritten);
|
|
253
|
+
return this.#rendered
|
|
254
|
+
? { isClientEntry: false, imports, reach: ExportReach.read(rewritten, absPath) }
|
|
255
|
+
: { isClientEntry: false, imports };
|
|
225
256
|
})();
|
|
226
257
|
this.#factsCache.set(absPath, cached);
|
|
227
258
|
}
|
|
@@ -255,6 +286,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
255
286
|
const absFile = path.resolve(file);
|
|
256
287
|
this.#objectReaders.set(absFile, (this.#objectReaders.get(absFile) ?? new Set()).add(absPath));
|
|
257
288
|
}
|
|
289
|
+
for (const barrel of narrowed?.barrels ?? [])
|
|
290
|
+
this.#barrelImporters.set(barrel, (this.#barrelImporters.get(barrel) ?? new Set()).add(absPath));
|
|
258
291
|
return narrowed?.contents ?? null;
|
|
259
292
|
} catch {
|
|
260
293
|
return null;
|
|
@@ -75,6 +75,60 @@ describe("DevBarrelImports", () => {
|
|
|
75
75
|
expect(await round().fingerprint(key, ["@server", "Zone.View"])).not.toBe(hash ?? "");
|
|
76
76
|
});
|
|
77
77
|
|
|
78
|
+
test("narrows a part of the object a wrapper copies in from a barrel, and stamps the wrapper with that object's files and the barrel's", async () => {
|
|
79
|
+
const root = await realpath(await makeTempRoot());
|
|
80
|
+
const file = (name: string) => path.join(root, name);
|
|
81
|
+
const fieldIndex = (text: string) =>
|
|
82
|
+
`import { Text } from "./${text}";\nexport const Field = Object.assign(() => null, { Text });\n`;
|
|
83
|
+
await write(file("ui/index.ts"), 'export * from "./Field";\n');
|
|
84
|
+
await write(file("ui/Field/index.tsx"), fieldIndex("Text"));
|
|
85
|
+
for (const text of ["Text", "TextNext"])
|
|
86
|
+
await write(file(`ui/Field/${text}.tsx`), '"use client";\nexport const Text = () => null;\n');
|
|
87
|
+
await write(
|
|
88
|
+
file("wrap/Field.tsx"),
|
|
89
|
+
[
|
|
90
|
+
'import { memo } from "react";',
|
|
91
|
+
'import { Field as BaseField } from "@demo/ui";',
|
|
92
|
+
"const Rich = memo(() => null);",
|
|
93
|
+
"export const Field = Object.assign(() => null, BaseField, { Rich });",
|
|
94
|
+
].join("\n"),
|
|
95
|
+
);
|
|
96
|
+
const importer = file("page/_index.tsx");
|
|
97
|
+
const page = 'import { Field } from "../wrap/Field";\nexport const Page = () => <Field.Text />;\n';
|
|
98
|
+
const key = `object:${file("wrap/Field.tsx")}#Field`;
|
|
99
|
+
const round = () =>
|
|
100
|
+
new DevBarrelImports(
|
|
101
|
+
["@demo/ui"],
|
|
102
|
+
async (pkgName) => {
|
|
103
|
+
const entry = { "@demo/ui": "ui/index.ts", "@demo/ui/Field": "ui/Field/index.tsx" }[pkgName];
|
|
104
|
+
return entry ? { pkgName, entryFile: file(entry), pkgDir: file("ui") } : null;
|
|
105
|
+
},
|
|
106
|
+
{ packageRoot: root },
|
|
107
|
+
);
|
|
108
|
+
const stamp = async () => (await round().stamps([key], (id) => path.relative(root, id)))[key];
|
|
109
|
+
|
|
110
|
+
const rewrite = await round().rewrite(page, importer);
|
|
111
|
+
const before = await stamp();
|
|
112
|
+
|
|
113
|
+
expect(rewrite?.contents).toBe(
|
|
114
|
+
'import { Text as Field$Text } from "../ui/Field/Text.tsx";\nexport const Page = () => <Field$Text />;\n',
|
|
115
|
+
);
|
|
116
|
+
expect(rewrite?.narrowed[key]?.names).toEqual(["@server", "Text"]);
|
|
117
|
+
expect(Object.keys(before?.read ?? {}).sort()).toEqual([
|
|
118
|
+
"ui",
|
|
119
|
+
"ui/Field",
|
|
120
|
+
"ui/Field/Text.tsx",
|
|
121
|
+
"ui/Field/index.tsx",
|
|
122
|
+
"ui/index.ts",
|
|
123
|
+
"wrap",
|
|
124
|
+
"wrap/Field.tsx",
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
await write(file("ui/Field/index.tsx"), fieldIndex("TextNext"));
|
|
128
|
+
expect((await stamp())?.digest).not.toBe(before?.digest ?? "");
|
|
129
|
+
expect(await round().fingerprint(key, ["@server", "Text"])).not.toBe(rewrite?.narrowed[key]?.hash ?? "");
|
|
130
|
+
});
|
|
131
|
+
|
|
78
132
|
test("hashes a part called through the object with whether it reads `this`, so a save that makes it read one moves both", async () => {
|
|
79
133
|
const root = await realpath(await makeTempRoot());
|
|
80
134
|
const file = (name: string) => path.join(root, name);
|
|
@@ -51,6 +51,7 @@ export class DevBarrelImports {
|
|
|
51
51
|
specifierOf: (file, importer) =>
|
|
52
52
|
DevBarrelImports.#relativeFile(CsrDevPaths.realpath(file), path.dirname(CsrDevPaths.realpath(importer))),
|
|
53
53
|
keepLines: true,
|
|
54
|
+
barrels: { names: this.#barrels, analyzer: this.#analyzer },
|
|
54
55
|
})
|
|
55
56
|
: null;
|
|
56
57
|
}
|
|
@@ -263,12 +263,20 @@ describe("DictionaryAssetBuilder", () => {
|
|
|
263
263
|
);
|
|
264
264
|
|
|
265
265
|
describe("for the routes it is handed", () => {
|
|
266
|
+
//? Texts brotli cannot fold away: with a few words per group a route's own files outweigh the whole file.
|
|
267
|
+
const long = (group: string) => ({
|
|
268
|
+
t: Array.from({ length: 80 }, (_, idx) => Bun.hash(`${group}${idx}`).toString(36)).join(" "),
|
|
269
|
+
});
|
|
266
270
|
const routedDictionary = {
|
|
267
271
|
en: {
|
|
268
|
-
base: { t: "Base", ok: { t: "OK" } },
|
|
269
|
-
memo: {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
+
base: { t: "Base", ok: { t: "OK" }, long: long("base") },
|
|
273
|
+
memo: {
|
|
274
|
+
t: "Memo",
|
|
275
|
+
long: long("memo"),
|
|
276
|
+
signal: { getMemo: { t: "Get Memo", desc: { t: "Reads a memo" } } },
|
|
277
|
+
},
|
|
278
|
+
user: { t: "User", long: long("user") },
|
|
279
|
+
util: { t: "Util", long: long("util") },
|
|
272
280
|
},
|
|
273
281
|
ko: { base: { t: "기본" }, memo: { t: "메모" } },
|
|
274
282
|
};
|
|
@@ -4,6 +4,7 @@ import type { AkanI18nConfig } from "akanjs/common";
|
|
|
4
4
|
import type { DictAsset } from "akanjs/server";
|
|
5
5
|
import type { App } from "../commandDecorators";
|
|
6
6
|
import { DictionaryPacking } from "./dictionaryPacking";
|
|
7
|
+
import { dictionaryBrotliBytesOf } from "./precompressArtifacts";
|
|
7
8
|
|
|
8
9
|
export interface DictionaryAssetBuildOptions {
|
|
9
10
|
/** Each locale's API descriptions go to a file of their own, loaded where shown. */
|
|
@@ -56,11 +57,17 @@ export class DictionaryAssetBuilder {
|
|
|
56
57
|
return { lang, ...(splitApi ? DictionaryAssetBuilder.splitApi(dictionary) : { page: dictionary, api: {} }) };
|
|
57
58
|
});
|
|
58
59
|
const groups = [...new Set(locales.flatMap(({ page }) => Object.keys(page)))].sort();
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const packed = routes?.size ? DictionaryAssetBuilder.#pack(routes, locales, i18n.defaultLocale, groups) : null;
|
|
61
|
+
if (packed?.wholeRoutes.length)
|
|
62
|
+
this.#app.verbose(
|
|
63
|
+
`[dictionary] ${packed.wholeRoutes.length} route(s) load the whole page dictionary: their own files would not be smaller. Routes: ${packed.wholeRoutes.join(", ")}`,
|
|
64
|
+
);
|
|
65
|
+
const pack =
|
|
66
|
+
packed && Object.keys(packed.partsByRoute).length
|
|
67
|
+
? packed
|
|
68
|
+
: everyGroupAlone
|
|
69
|
+
? { parts: groups.map((group) => [group]), partsByRoute: {} }
|
|
70
|
+
: null;
|
|
64
71
|
const assets = await Promise.all(locales.map((locale) => this.#writeLocale(locale, pack?.parts)));
|
|
65
72
|
const files = assets.flatMap(([, asset]) => [
|
|
66
73
|
asset,
|
|
@@ -91,8 +98,14 @@ export class DictionaryAssetBuilder {
|
|
|
91
98
|
defaultLocale: string,
|
|
92
99
|
groups: string[],
|
|
93
100
|
) {
|
|
94
|
-
const
|
|
95
|
-
|
|
101
|
+
const { lang = defaultLocale, page: sizing = {} } =
|
|
102
|
+
locales.find((locale) => locale.lang === defaultLocale) ?? locales[0] ?? {};
|
|
103
|
+
const servedBytesOf = (held: readonly string[]) => {
|
|
104
|
+
const texts = Object.fromEntries(held.flatMap((group) => (sizing[group] ? [[group, sizing[group]]] : [])));
|
|
105
|
+
return dictionaryBrotliBytesOf(DictionaryAssetBuilder.source(lang, texts));
|
|
106
|
+
};
|
|
107
|
+
const bytesOf = (group: string) => JSON.stringify(sizing[group] ?? {}).length;
|
|
108
|
+
return DictionaryPacking.packUnderWhole(routes, bytesOf, groups, servedBytesOf);
|
|
96
109
|
}
|
|
97
110
|
|
|
98
111
|
//? The same module the root layout's macro evaluates in this process, so the asset and the RSC worker's copy agree.
|
|
@@ -16,6 +16,8 @@ export class DictionaryConstants {
|
|
|
16
16
|
readonly #kindOf = new Map<string, GroupKind>();
|
|
17
17
|
readonly #typesOf = new Map<string, Set<string>>();
|
|
18
18
|
readonly #reach = new Map<string, string[]>();
|
|
19
|
+
readonly #statics = new Map<string, (string[] | null)[]>();
|
|
20
|
+
readonly #classCount = new Map<string, number>();
|
|
19
21
|
|
|
20
22
|
static async read(workspaceRoot: string, scopes: string[]): Promise<DictionaryConstants> {
|
|
21
23
|
const constants = new DictionaryConstants();
|
|
@@ -28,10 +30,11 @@ export class DictionaryConstants {
|
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
add(fileName: string, text: string) {
|
|
33
|
+
const sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
34
|
+
this.#addStatics(sourceFile);
|
|
31
35
|
const [, scalar, underscore, group] = fileName.match(constantFile) ?? [];
|
|
32
36
|
if (!group) return;
|
|
33
37
|
this.#kindOf.set(group, scalar ? "scalar" : underscore ? "service" : "model");
|
|
34
|
-
const sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
35
38
|
const types = this.#typesOf.get(group) ?? new Set<string>();
|
|
36
39
|
for (const constant of DictionaryConstants.#reader.read(sourceFile, null).constants) {
|
|
37
40
|
this.#classTo(constant.name, group);
|
|
@@ -45,6 +48,57 @@ export class DictionaryConstants {
|
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
|
|
51
|
+
#addStatics(sourceFile: ts.SourceFile) {
|
|
52
|
+
for (const statement of sourceFile.statements) {
|
|
53
|
+
if (!ts.isClassDeclaration(statement) || !statement.name) continue;
|
|
54
|
+
this.#classCount.set(statement.name.text, (this.#classCount.get(statement.name.text) ?? 0) + 1);
|
|
55
|
+
for (const member of statement.members) {
|
|
56
|
+
const name =
|
|
57
|
+
member.name && (ts.isIdentifier(member.name) || ts.isStringLiteral(member.name)) && member.name.text;
|
|
58
|
+
if (!name || !(ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static)) continue;
|
|
59
|
+
const key = `${statement.name.text}.${name}`;
|
|
60
|
+
const table = ts.isPropertyDeclaration(member) ? DictionaryConstants.#tableOf(member) : null;
|
|
61
|
+
this.#statics.set(key, [...(this.#statics.get(key) ?? []), table]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//? `as const` with no annotation types the static as its literals, so nothing but the written keys is assignable to it.
|
|
67
|
+
static #tableOf(member: ts.PropertyDeclaration): string[] | null {
|
|
68
|
+
let node = member.initializer;
|
|
69
|
+
while (node && (ts.isParenthesizedExpression(node) || ts.isSatisfiesExpression(node))) node = node.expression;
|
|
70
|
+
if (member.type || !node || !ts.isAsExpression(node) || !ts.isConstTypeReference(node.type)) return null;
|
|
71
|
+
let table = node.expression;
|
|
72
|
+
while (ts.isParenthesizedExpression(table)) table = table.expression;
|
|
73
|
+
const values = ts.isArrayLiteralExpression(table)
|
|
74
|
+
? [...table.elements]
|
|
75
|
+
: ts.isObjectLiteralExpression(table)
|
|
76
|
+
? table.properties.map((property) =>
|
|
77
|
+
ts.isPropertyAssignment(property) && !ts.isComputedPropertyName(property.name)
|
|
78
|
+
? property.initializer
|
|
79
|
+
: null,
|
|
80
|
+
)
|
|
81
|
+
: [null];
|
|
82
|
+
const keys: string[] = [];
|
|
83
|
+
for (const value of values) {
|
|
84
|
+
if (!value || !ts.isStringLiteralLike(value)) return null;
|
|
85
|
+
keys.push(value.text);
|
|
86
|
+
}
|
|
87
|
+
return keys;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//? An app and a lib may each declare a class of the name, and one that leaves the static out may inherit another.
|
|
91
|
+
staticKeysOf(className: string, member: string): string[] | null {
|
|
92
|
+
const tables = this.#statics.get(`${className}.${member}`);
|
|
93
|
+
if (!tables || tables.length !== this.#classCount.get(className)) return null;
|
|
94
|
+
const keys = new Set<string>();
|
|
95
|
+
for (const table of tables) {
|
|
96
|
+
if (!table) return null;
|
|
97
|
+
for (const key of table) keys.add(key);
|
|
98
|
+
}
|
|
99
|
+
return [...keys];
|
|
100
|
+
}
|
|
101
|
+
|
|
48
102
|
#classTo(name: string, group: string) {
|
|
49
103
|
this.#groupOfClass.set(name, (this.#groupOfClass.get(name) ?? new Set()).add(group));
|
|
50
104
|
}
|
|
@@ -69,6 +69,64 @@ describe("DictionaryPacking", () => {
|
|
|
69
69
|
expect(pack.partsByRoute).toEqual({ "/:lang/a": [0], "/:lang/b": [0] });
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
+
describe("held under the whole dictionary", () => {
|
|
73
|
+
const fileOverhead = 300;
|
|
74
|
+
const servedOf = (bytesOf: (group: string) => number) => (held: readonly string[]) =>
|
|
75
|
+
fileOverhead + held.reduce((sum, group) => sum + bytesOf(group), 0);
|
|
76
|
+
|
|
77
|
+
test("a route whose files would not be smaller than the whole loads the whole file and no longer shapes the cut", () => {
|
|
78
|
+
const modules = Array.from({ length: 8 }, (_, idx) => `module${idx}`);
|
|
79
|
+
const groups = ["base", ...modules];
|
|
80
|
+
const light = new Map(modules.map((module) => [`/:lang/${module}`, ["base", module]]));
|
|
81
|
+
const routes = new Map([...light, ["/:lang/everything", groups]]);
|
|
82
|
+
const bytesOf = (group: string) => (group === "base" ? 2000 : 5000);
|
|
83
|
+
const pack = DictionaryPacking.packUnderWhole(routes, bytesOf, groups, servedOf(bytesOf));
|
|
84
|
+
const alone = DictionaryPacking.pack(routes, bytesOf, groups);
|
|
85
|
+
|
|
86
|
+
expect(pack.wholeRoutes).toEqual(["/:lang/everything"]);
|
|
87
|
+
expect(pack.partsByRoute["/:lang/everything"]).toBeUndefined();
|
|
88
|
+
expect({ parts: pack.parts, partsByRoute: pack.partsByRoute }).toEqual(
|
|
89
|
+
DictionaryPacking.pack(light, bytesOf, groups),
|
|
90
|
+
);
|
|
91
|
+
for (const module of modules) {
|
|
92
|
+
const held = (files: number[] = [], parts: string[][]) => files.flatMap((idx) => parts[idx] ?? []).sort();
|
|
93
|
+
expect(held(pack.partsByRoute[`/:lang/${module}`], pack.parts)).toEqual(["base", module]);
|
|
94
|
+
expect(held(alone.partsByRoute[`/:lang/${module}`], alone.parts).length).toBeGreaterThan(2);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("no route left on files loads the whole or more, and all routes together load no more than before", () => {
|
|
99
|
+
const { groups, routes, bytesOf } = manyModules();
|
|
100
|
+
const served = servedOf(bytesOf);
|
|
101
|
+
const whole = served(groups);
|
|
102
|
+
const costOf = ({ parts, partsByRoute }: ReturnType<typeof DictionaryPacking.pack>) =>
|
|
103
|
+
[...routes.keys()].reduce((sum, routeId) => {
|
|
104
|
+
const files = partsByRoute[routeId];
|
|
105
|
+
const own = files?.reduce((bytes, idx) => bytes + served(parts[idx] ?? []), 0) ?? whole;
|
|
106
|
+
return sum + Math.min(own, whole);
|
|
107
|
+
}, 0);
|
|
108
|
+
const pack = DictionaryPacking.packUnderWhole(routes, bytesOf, groups, served);
|
|
109
|
+
|
|
110
|
+
expect(pack.wholeRoutes).toContain("/:lang/everything");
|
|
111
|
+
expect(Object.keys(pack.partsByRoute).length).toBe(routes.size - pack.wholeRoutes.length);
|
|
112
|
+
for (const files of Object.values(pack.partsByRoute))
|
|
113
|
+
expect(files.reduce((sum, idx) => sum + served(pack.parts[idx] ?? []), 0)).toBeLessThan(whole);
|
|
114
|
+
expect(costOf(pack)).toBeLessThanOrEqual(costOf(DictionaryPacking.pack(routes, bytesOf, groups)));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("every route loads the whole file where each shows every group", () => {
|
|
118
|
+
const routes = new Map([
|
|
119
|
+
["/:lang/a", ["base", "memo", "user"]],
|
|
120
|
+
["/:lang/b", ["base", "user", "memo"]],
|
|
121
|
+
]);
|
|
122
|
+
const bytesOf = (group: string) => (group === "base" ? 9000 : 300);
|
|
123
|
+
const pack = DictionaryPacking.packUnderWhole(routes, bytesOf, ["base", "memo", "user"], servedOf(bytesOf));
|
|
124
|
+
|
|
125
|
+
expect(pack.wholeRoutes).toEqual(["/:lang/a", "/:lang/b"]);
|
|
126
|
+
expect(pack.partsByRoute).toEqual({});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
72
130
|
test("a second route reuses the files the first one loaded where they share groups", () => {
|
|
73
131
|
const routes = new Map([
|
|
74
132
|
["/:lang/memo", ["base", "memo", "user"]],
|
|
@@ -48,6 +48,49 @@ export class DictionaryPacking {
|
|
|
48
48
|
return { parts, partsByRoute };
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
//? Several files compress worse than one, so a route showing nearly every text is cheaper on the whole dictionary.
|
|
52
|
+
static packUnderWhole(
|
|
53
|
+
routes: ReadonlyMap<string, readonly string[]>,
|
|
54
|
+
bytesOf: (group: string) => number,
|
|
55
|
+
groups: readonly string[],
|
|
56
|
+
servedBytesOf: (groups: readonly string[]) => number,
|
|
57
|
+
options: { bound?: number; requestBytes?: number } = {},
|
|
58
|
+
): DictionaryPack & { wholeRoutes: string[] } {
|
|
59
|
+
const whole = servedBytesOf([...new Set(groups)].sort());
|
|
60
|
+
const served = new Map<string, number>();
|
|
61
|
+
const servedOf = (part: readonly string[]) => {
|
|
62
|
+
const key = part.join("\n");
|
|
63
|
+
const known = served.get(key) ?? servedBytesOf(part);
|
|
64
|
+
served.set(key, known);
|
|
65
|
+
return known;
|
|
66
|
+
};
|
|
67
|
+
const shownBytesOf = (routeId: string) =>
|
|
68
|
+
[...new Set(routes.get(routeId))].reduce((sum, group) => sum + bytesOf(group), 0);
|
|
69
|
+
const partial = new Map(routes);
|
|
70
|
+
let best: { pack: DictionaryPack; over: string[]; cost: number } | null = null;
|
|
71
|
+
for (;;) {
|
|
72
|
+
const pack = DictionaryPacking.pack(partial, bytesOf, groups, options);
|
|
73
|
+
const costs = Object.entries(pack.partsByRoute).map(([routeId, files]) => ({
|
|
74
|
+
routeId,
|
|
75
|
+
cost: files.reduce((sum, idx) => sum + servedOf(pack.parts[idx] ?? []), 0),
|
|
76
|
+
}));
|
|
77
|
+
const over = costs.filter(({ cost }) => cost >= whole).map(({ routeId }) => routeId);
|
|
78
|
+
const cost =
|
|
79
|
+
costs.reduce((sum, route) => sum + Math.min(route.cost, whole), 0) + (routes.size - partial.size) * whole;
|
|
80
|
+
if (!best || cost < best.cost) best = { pack, over, cost };
|
|
81
|
+
if (!over.length) break;
|
|
82
|
+
//? Half at a time, the routes showing the most first: without them the rest may come out smaller after all.
|
|
83
|
+
over.sort((a, b) => shownBytesOf(b) - shownBytesOf(a) || (a < b ? -1 : 1));
|
|
84
|
+
for (const routeId of over.slice(0, Math.ceil(over.length / 2))) partial.delete(routeId);
|
|
85
|
+
}
|
|
86
|
+
const dropped = new Set(best.over);
|
|
87
|
+
const partsByRoute = Object.fromEntries(
|
|
88
|
+
Object.entries(best.pack.partsByRoute).filter(([routeId]) => !dropped.has(routeId)),
|
|
89
|
+
);
|
|
90
|
+
const wholeRoutes = [...routes.keys()].sort().filter((routeId) => !Object.hasOwn(partsByRoute, routeId));
|
|
91
|
+
return { parts: best.pack.parts, partsByRoute, wholeRoutes };
|
|
92
|
+
}
|
|
93
|
+
|
|
51
94
|
static #setsOf(parts: string[][], needs: string[][]) {
|
|
52
95
|
const partOf = new Map(parts.flatMap((part, idx) => part.map((group) => [group, idx] as const)));
|
|
53
96
|
return needs.map((need) => new Set(need.map((group) => partOf.get(group) as number)));
|