@akanjs/devkit 3.3.0-rc.6 → 3.3.0-rc.61
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/DEV_RUNTIME_KNOBS.md +1 -1
- package/README.ko.md +8 -2
- package/README.md +8 -3
- package/akanApp/akanApp.host.test.ts +4 -2
- package/akanApp/akanApp.host.ts +11 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +280 -19
- package/akanConfig/akanConfig.ts +96 -270
- package/akanConfig/akanWorkers.test.ts +73 -0
- package/akanConfig/akanWorkers.ts +61 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +216 -0
- package/akanConfig/index.ts +2 -0
- package/akanConfig/nativeFileSettings.ts +33 -0
- package/akanConfig/nativeKeys.ts +52 -0
- package/akanConfig/types.ts +2 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +51 -1
- package/app/applicationBuildReporter.ts +6 -1
- package/app/applicationBuildRunner.test.ts +358 -5
- package/app/applicationBuildRunner.ts +204 -30
- package/app/dictionarySizes.test.ts +40 -0
- package/app/dictionarySizes.ts +95 -0
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/implicitRootLayout.test.ts +19 -0
- package/artifact/implicitRootLayout.ts +16 -2
- package/artifact/implicitRootLayoutRender.test.tsx +4 -1
- package/artifact/storeBootstrap.test.ts +28 -0
- package/artifact/storeBootstrap.ts +64 -8
- package/biome.base.json +7 -5
- package/codeAgent/agent/CodeAgent.ts +36 -16
- package/codeAgent/agent/CodeAgentAllowed.ts +35 -0
- package/codeAgent/agent/CodeAgentCommandLine.ts +372 -0
- package/codeAgent/agent/CodeAgentGate.ts +23 -4
- package/codeAgent/agent/CodeAgentRpcHost.ts +1 -1
- package/codeAgent/agent/codeAgent.test.ts +62 -0
- package/codeAgent/agent/codeAgentCommandLine.test.ts +212 -0
- package/codeAgent/agent/codeAgentContract.test.ts +117 -5
- package/codeAgent/skills/akan-agent/SKILL.md +8 -1
- package/codeAgent/skills/akan-code/SKILL.md +38 -3
- package/codeAgent/skills/akan-debug/SKILL.md +1 -1
- package/codeAgent/skills/akan-deps/SKILL.md +3 -2
- package/codeAgent/skills/akan-err/SKILL.md +13 -8
- package/codeAgent/skills/akan-mcp/SKILL.md +1 -1
- package/codeAgent/skills/akan-module/SKILL.md +2 -2
- package/codeAgent/skills/akan-page/SKILL.md +52 -28
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-runtime/SKILL.md +21 -1
- package/codeAgent/skills/akan-service/SKILL.md +10 -11
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +16 -21
- package/commandDecorators/command.ts +28 -12
- package/commandDecorators/commandBuilder.ts +8 -1
- package/commandDecorators/commandDecorators.test.ts +70 -3
- package/commandDecorators/helpFormatter.ts +14 -12
- package/context/akanContext.ts +2 -0
- package/csrE2e/devHmrLatency.e2e.test.ts +1 -1
- package/docs/apiReference.test.ts +19 -0
- package/docs/apiReference.ts +28 -5
- package/executors/appExecutor.ts +26 -12
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1690 -0
- package/executors/executor.ts +14 -6
- package/executors/executors.test.ts +140 -1
- package/executors/sysExecutor.ts +112 -8
- package/executors/workspaceExecutor.ts +7 -4
- package/frontendBuild/allRoutesBuilder.ts +99 -7
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +686 -13
- package/frontendBuild/clientBuildTypes.ts +26 -0
- package/frontendBuild/clientEntriesBundler.ts +49 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
- package/frontendBuild/clientEntryDiscovery.ts +136 -14
- package/frontendBuild/clientEntryGroups.test.ts +77 -0
- package/frontendBuild/clientEntryGroups.ts +61 -0
- package/frontendBuild/csrDevArtifactWriter.ts +2 -2
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.test.ts +1 -1
- package/frontendBuild/csrDevPatcher.ts +33 -7
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/devBarrelImports.test.ts +154 -0
- package/frontendBuild/devBarrelImports.ts +83 -23
- package/frontendBuild/devRegistryBundler.ts +4 -2
- package/frontendBuild/devRouteRuntime.test.ts +492 -0
- package/frontendBuild/devRouteRuntime.ts +167 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +408 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +185 -17
- package/frontendBuild/dictionaryConstants.ts +136 -0
- package/frontendBuild/dictionaryOwnParts.ts +88 -0
- package/frontendBuild/dictionaryPackState.ts +131 -0
- package/frontendBuild/dictionaryPacking.test.ts +207 -0
- package/frontendBuild/dictionaryPacking.ts +160 -0
- package/frontendBuild/dictionaryRoutes.test.ts +251 -0
- package/frontendBuild/dictionaryRoutes.ts +227 -0
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +78 -2
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +34 -8
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/renderedEntries.ts +144 -0
- package/frontendBuild/routeClientBuilder.ts +407 -43
- package/frontendBuild/routeHandoffs.test.ts +155 -0
- package/frontendBuild/routeHandoffs.ts +117 -0
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +280 -0
- package/frontendBuild/routeRuntimeCheck.ts +248 -0
- package/frontendBuild/routeRuntimePays.test.ts +143 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/ssrBaseArtifactBuilder.ts +24 -34
- package/frontendBuild/ssrDevBundler.test.ts +150 -1
- package/frontendBuild/ssrDevBundler.ts +66 -11
- package/frontendBuild/vendorFileImports.test.ts +254 -0
- package/frontendBuild/vendorFileImports.ts +163 -0
- package/frontendBuild/vendorNarrowing.ts +28 -0
- package/frontendBuild/vendorSpecifiers.ts +2 -0
- package/incrementalBuilder/buildBatch.proc.ts +9 -9
- package/incrementalBuilder/buildBatchProtocol.ts +1 -1
- package/incrementalBuilder/builderBoot.ts +5 -5
- package/incrementalBuilder/builderGraphPayloads.ts +4 -4
- package/incrementalBuilder/builderLanes.ts +2 -2
- package/incrementalBuilder/builderWatch.ts +8 -8
- package/incrementalBuilder/csrRegistry.ts +4 -4
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +79 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +27 -8
- package/incrementalBuilder/ssrRegistry.ts +6 -6
- package/index.ts +1 -1
- package/integration/renderAdmissionSoak.test.ts +99 -0
- package/integration/renderAdmissionSoak.ts +609 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/bad.tsx +11 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/bad.tsx +34 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/good.tsx +2 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/bad.tsx +10 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/bad.tsx +22 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/good.tsx +2 -0
- package/lint/biomeBase.test.ts +50 -3
- package/lint/biomeBase.ts +1 -1
- package/lint/biomeCheck.test.ts +162 -0
- package/lint/biomeCheck.ts +131 -0
- package/lint/linter.test.ts +16 -11
- package/lint/no-arbitrary-color.grit +5 -0
- package/lint/no-conditional-st-hook.grit +5 -1
- package/lint/no-daisyui-legacy-class.grit +8 -0
- package/lint/no-document-cookie.grit +3 -0
- package/lint/no-inline-color.grit +10 -2
- package/lint/no-oversized-list-limit.grit +2 -0
- package/lint/no-raw-palette-class.grit +7 -0
- package/lint/no-unpublished-form-setter.grit +2 -0
- package/lint/no-web-only-api-outside-webkit.grit +5 -0
- package/lint/no-web-storage.grit +3 -0
- package/lint/withheldEnvFile.ts +17 -0
- package/mobile/desktopBin.ts +2 -2
- package/mobile/desktopServerStage.test.ts +18 -3
- package/mobile/desktopServerStage.ts +7 -2
- package/mobile/nativeApp.test.ts +7 -5
- package/mobile/nativeApp.ts +2 -2
- package/mobile/nativeConfig.test.ts +96 -1
- package/mobile/nativeConfig.ts +46 -12
- package/mobile/nativePublicEnv.ts +1 -1
- package/mobile/serverBundleSecrets.ts +43 -0
- package/package.json +20 -171
- package/packageExports.test.ts +38 -24
- package/quality/dictionaryKeys.test.ts +356 -0
- package/quality/dictionaryKeys.ts +488 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +105 -0
- package/quality/qualityScanner.ts +1 -1
- package/quality/routeRuntimeReport.test.ts +91 -0
- package/quality/routeRuntimeReport.ts +121 -0
- package/quality/runtimeConstantFacts.ts +71 -0
- package/quality/runtimeConstantGraph.ts +123 -0
- package/quality/runtimeConstantReads.ts +116 -0
- package/quality/runtimeImports.test.ts +190 -0
- package/quality/runtimeImports.ts +233 -0
- package/quality/runtimeKeyNames.ts +46 -0
- package/quality/runtimeLexicalScope.ts +132 -0
- package/quality/runtimeMarkerReach.test.ts +84 -0
- package/quality/runtimeMarkerReach.ts +255 -0
- package/quality/runtimeModuleFacts.ts +290 -0
- package/quality/runtimeNameTable.test.ts +172 -0
- package/quality/runtimeNameTable.ts +477 -0
- package/quality/runtimeScopeReport.test.ts +123 -0
- package/quality/runtimeScopeReport.ts +167 -0
- package/quality/runtimeSliceKey.ts +269 -0
- package/quality/runtimeSyntax.ts +133 -0
- package/quality/runtimeTable.fixture.ts +189 -0
- package/quality/runtimeUse.test.ts +737 -0
- package/quality/runtimeUse.ts +696 -0
- package/quality/runtimeUseReadable.test.ts +393 -0
- package/quality/ssrScanner.test.ts +13 -0
- package/quality/ssrScanner.ts +1 -1
- package/quality/translatorBindings.ts +187 -0
- package/test/secretEnvFixture.ts +134 -0
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +26 -4
- package/transforms/barrelImportsPlugin.ts +121 -32
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +308 -0
- package/transforms/frameworkObjects.fixture.tsx +58 -0
- package/transforms/frameworkObjects.test.ts +54 -0
- package/transforms/modelReference.test.ts +196 -0
- package/transforms/modelReference.ts +39 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +633 -0
- package/transforms/objectMemberImports.ts +571 -0
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +307 -0
- package/transforms/objectModule.ts +482 -0
- package/transforms/objectModuleAnalyzer.ts +104 -0
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
- package/transforms/reactIconImports.test.ts +231 -0
- package/transforms/reactIconImports.ts +325 -0
- package/transforms/routeConstants.ts +122 -0
- package/transforms/routeRuntime.test.ts +835 -0
- package/transforms/routeRuntime.ts +703 -0
- package/transforms/routeRuntimeEdits.ts +92 -0
- package/transforms/routeRuntimeGraph.ts +66 -0
- package/transforms/routeRuntimePlan.ts +244 -0
- package/transforms/secretEnvDeclaration.test.ts +208 -0
- package/transforms/secretEnvDeclaration.ts +213 -0
- package/transforms/secretEnvFile.test.ts +117 -0
- package/transforms/secretEnvFile.ts +113 -0
- package/transforms/secretEnvName.test.ts +54 -0
- package/transforms/secretEnvName.ts +29 -0
- package/transforms/secretEnvSource.test.ts +148 -0
- package/transforms/secretEnvSource.ts +166 -0
- package/transforms/serverEnvEvaluationError.ts +11 -0
- package/transforms/serverEnvPlugin.test.ts +2 -1
- package/transforms/serverEnvPlugin.ts +13 -0
- package/transforms/serverEnvProfile.test.ts +129 -2
- package/transforms/serverEnvProfile.ts +126 -40
- package/transforms/serverEnvSecrets.test.ts +292 -0
- package/transforms/signalDescriptors.test.ts +280 -0
- package/transforms/signalDescriptors.ts +227 -0
- package/transforms/thisFreeExports.ts +87 -0
- package/transforms/transforms.test.ts +180 -3
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/tunnel/createTunnel.test.ts +29 -8
- package/tunnel/createTunnel.ts +22 -11
- package/tunnel/ssh2Module.test.ts +37 -0
- package/tunnel/ssh2Module.ts +32 -0
- package/tunnel/sshTunnel.ts +4 -2
- package/typecheck/projectTypecheck.test.ts +32 -1
- package/typecheck/projectTypecheck.ts +3 -3
- package/typecheck/typeChecker.test.ts +49 -0
- package/typecheck/typeChecker.ts +9 -7
- package/typecheck/withheldDiagnostics.ts +41 -0
- package/util/fileEditor.ts +1 -0
- package/util/selectModel.ts +1 -0
- package/workflow/executor.ts +6 -2
- package/workflow/index.ts +1 -0
- package/workflow/plan.ts +36 -7
- package/workflow/types.ts +1 -1
- package/workflow/uiTarget.ts +50 -0
- package/workspace/envSeed.test.ts +30 -1
- package/workspace/envSeed.ts +30 -1
- package/workspace/getRelatedCnsts.ts +7 -0
- package/workspace/mainCheckout.test.ts +55 -0
- package/workspace/mainCheckout.ts +38 -0
- package/workspace/scanInfo.test.ts +39 -0
- package/workspace/scanInfo.ts +55 -6
- package/workspace/slicePlanner.ts +1 -1
- package/workspace/workspaceLayout.test.ts +5 -1
- package/workspace/workspaceLayout.ts +3 -0
- package/CHANGELOG.md +0 -2717
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { readdir } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import { StoreBootstrap } from "../artifact/storeBootstrap";
|
|
6
|
+
import { RuntimeConstantGraph } from "../quality/runtimeConstantGraph";
|
|
7
|
+
import { RuntimeImports } from "../quality/runtimeImports";
|
|
8
|
+
import { RuntimeNameTable } from "../quality/runtimeNameTable";
|
|
9
|
+
import { RuntimeScopeReport } from "../quality/runtimeScopeReport";
|
|
10
|
+
import { RuntimeUse, type RuntimeUseVerdict } from "../quality/runtimeUse";
|
|
11
|
+
import { ModelReference } from "./modelReference";
|
|
12
|
+
import { type ConstantRewrite, RouteConstants } from "./routeConstants";
|
|
13
|
+
import { RouteRuntimeEdits, type RouteRuntimeTextEdit } from "./routeRuntimeEdits";
|
|
14
|
+
import { type RouteRuntimeBundle, RouteRuntimeGraph } from "./routeRuntimeGraph";
|
|
15
|
+
import { type RouteRuntimeKeep, RouteRuntimePlan, type RouteRuntimeWhy } from "./routeRuntimePlan";
|
|
16
|
+
|
|
17
|
+
export interface RouteRuntimeResult {
|
|
18
|
+
source: string;
|
|
19
|
+
perModule: boolean;
|
|
20
|
+
//? Workspace paths without an extension.
|
|
21
|
+
pieces: string[];
|
|
22
|
+
wholeConstants?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface Planned {
|
|
26
|
+
graph: RouteRuntimeGraph;
|
|
27
|
+
//? Every file a store file imports, itself included: it loads no store piece, which would import that store back.
|
|
28
|
+
storeSide: Set<string>;
|
|
29
|
+
//? The files a page loading pieces reaches. Every other file the bundle holds keeps the whole runtime as written.
|
|
30
|
+
routed: Set<string>;
|
|
31
|
+
//? Null where no route was told: every page then registers only the models it names.
|
|
32
|
+
narrow: NarrowPieces | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//? What a page loading no whole runtime reaches. The whole runtime takes these from their pieces, and answers the rest.
|
|
36
|
+
interface NarrowPieces {
|
|
37
|
+
//? Workspace paths without an extension.
|
|
38
|
+
pieces: Set<string>;
|
|
39
|
+
constantFiles: Set<string>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const macroAttribute = /with\s*\{\s*type\s*:\s*["']macro["']\s*\}/;
|
|
43
|
+
const storeFile = /^((?:apps|libs)\/[^/]+)\/lib\/_?[^/]+\/([^/]+)\.store\.ts$/;
|
|
44
|
+
const wholeFile = /^((?:apps|libs)\/[^/]+)\/lib\/(useClient|st)\.ts$/;
|
|
45
|
+
//? A model's reference file is not one: it is an entry the plan narrows like a page's own client file.
|
|
46
|
+
const runtimeFile =
|
|
47
|
+
/^(?:apps|libs)\/[^/]+\/lib\/(?:(?:useClient|st|usePage)\.ts|__client\/(?![^/]+\.ref\.ts$)[^/]+\.ts)$/;
|
|
48
|
+
const storePiece = /^((?:apps|libs)\/[^/]+)\/lib\/__client\/([^/_][^/]*)\.st$/;
|
|
49
|
+
const constantPieceFile = /^(?:apps|libs)\/[^/]+\/lib\/__client\/([^/_][^/]*)\.cnst\.ts$/;
|
|
50
|
+
const constantsFile = /^((?:apps|libs)\/[^/]+)\/lib\/cnst\.ts$/;
|
|
51
|
+
const registration = /^export const (\w+) = ConstantRegistry\.build(?:Model|Scalar)\(.*$/gm;
|
|
52
|
+
const pieceImport = /\bfrom\s+"((?:\.|@(?:apps|libs)\/)[^"]+)"/g;
|
|
53
|
+
const assembled = { useClient: "_useClient", st: "_st" } as const;
|
|
54
|
+
const byCodeUnit = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
|
|
55
|
+
|
|
56
|
+
const exportOf = (piece: string) => {
|
|
57
|
+
const name = piece.slice(piece.lastIndexOf("/") + 1);
|
|
58
|
+
if (name === assembled.st) return "st";
|
|
59
|
+
if (name === assembled.useClient) return "sig";
|
|
60
|
+
if (name === "cnst") return "*";
|
|
61
|
+
if (name.endsWith(".cnst")) return name.slice(0, -".cnst".length);
|
|
62
|
+
return name.endsWith(".st") ? name.slice(0, -".st".length) : "loaded";
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const saidOf = (why: RouteRuntimeWhy | undefined) =>
|
|
66
|
+
why ? `${why.file}${why.line ? `:${why.line}` : ""} ${why.message}` : "a file it imports is left as written";
|
|
67
|
+
|
|
68
|
+
const parsed = (fileName: string, source: string): ts.SourceFile | null => {
|
|
69
|
+
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
70
|
+
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, kind);
|
|
71
|
+
return (sourceFile as unknown as { parseDiagnostics: unknown[] }).parseDiagnostics.length ? null : sourceFile;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
//* What a client module imports where pages load per module: the page runtime's handles plus the pieces of the
|
|
75
|
+
//* modules it names, or the app's whole runtime assembled from the same pieces. Each piece is imported by a name the
|
|
76
|
+
//* module passes to `loadedAfter`, never for its side effect alone, so the piece's chunk loads before the module's.
|
|
77
|
+
export class RouteRuntime {
|
|
78
|
+
readonly #workspaceRoot: string;
|
|
79
|
+
readonly #table: RuntimeNameTable;
|
|
80
|
+
readonly #pieces: ReadonlySet<string>;
|
|
81
|
+
readonly #read: (absPath: string) => string | null;
|
|
82
|
+
readonly #constants: RouteConstants;
|
|
83
|
+
readonly #storeNeeds = new Map<string, string[] | null>();
|
|
84
|
+
readonly #ownNeeds = new Map<string, string[] | null>();
|
|
85
|
+
readonly #sideNeeds = new Map<string, string[] | null>();
|
|
86
|
+
readonly #routes = new Map<string, { source: string; result: RouteRuntimeResult | null }>();
|
|
87
|
+
readonly #wholeReaders = new Map<string, RouteRuntimeWhy | null>();
|
|
88
|
+
readonly #why = {
|
|
89
|
+
whole: new Map<string, RouteRuntimeWhy>(),
|
|
90
|
+
needs: new Map<string, RouteRuntimeWhy>(),
|
|
91
|
+
side: new Map<string, RouteRuntimeWhy>(),
|
|
92
|
+
store: new Map<string, RouteRuntimeWhy>(),
|
|
93
|
+
};
|
|
94
|
+
#planned: Planned | null = null;
|
|
95
|
+
#decided: RouteRuntimePlan | null = null;
|
|
96
|
+
|
|
97
|
+
static async read(workspaceRoot: string, appScope: string): Promise<RouteRuntime | null> {
|
|
98
|
+
const table = await RuntimeNameTable.read(workspaceRoot, appScope);
|
|
99
|
+
if (!table) return null;
|
|
100
|
+
const pieces = await Promise.all(
|
|
101
|
+
table.layers.map(async ({ scope }) => {
|
|
102
|
+
const files = await readdir(path.join(workspaceRoot, scope, "lib/__client")).catch(() => []);
|
|
103
|
+
return files.filter((file) => file.endsWith(".ts")).map((file) => `${scope}/lib/__client/${file.slice(0, -3)}`);
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
const runtime = new RouteRuntime(workspaceRoot, table, new Set(pieces.flat()));
|
|
107
|
+
return runtime.#wholePieces.every((piece) => runtime.#pieces.has(piece)) ? runtime : null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
constructor(
|
|
111
|
+
workspaceRoot: string,
|
|
112
|
+
table: RuntimeNameTable,
|
|
113
|
+
pieces: ReadonlySet<string>,
|
|
114
|
+
read: (absPath: string) => string | null = RouteRuntime.#readFile,
|
|
115
|
+
) {
|
|
116
|
+
this.#workspaceRoot = workspaceRoot;
|
|
117
|
+
this.#table = table;
|
|
118
|
+
this.#pieces = pieces;
|
|
119
|
+
this.#read = read;
|
|
120
|
+
const graph = new RuntimeConstantGraph(table, (file) => read(path.join(workspaceRoot, file)));
|
|
121
|
+
this.#constants = new RouteConstants(graph, pieces);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static #unsynced(scope: string, module: string) {
|
|
125
|
+
const synced = /^(?:apps|libs)\//.test(scope) ? `akan sync ${path.basename(scope)}` : "akan sync";
|
|
126
|
+
return `names ${module}, which has no generated piece: run \`${synced}\``;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static #readFile(absPath: string) {
|
|
130
|
+
try {
|
|
131
|
+
return readFileSync(absPath, "utf8");
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get table() {
|
|
138
|
+
return this.#table;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get #wholePieces() {
|
|
142
|
+
return Object.values(assembled).map((file) => `${this.#table.scope}/lib/__client/${file}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//? Absolute paths: a framework file imports these too, and neither `akanjs` nor a consumer's node_modules maps `@apps/*`.
|
|
146
|
+
#absoluteOf(piece: string) {
|
|
147
|
+
return `${path.join(this.#workspaceRoot, piece).split(path.sep).join("/")}.ts`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#after(pieces: string[], specifierOf: (piece: string) => string): string[] {
|
|
151
|
+
if (!pieces.length) return [];
|
|
152
|
+
const names = pieces.map((_, index) => `__akanLoaded${index}`);
|
|
153
|
+
return [
|
|
154
|
+
'import { loadedAfter as __akanLoadedAfter } from "akanjs/base";',
|
|
155
|
+
...pieces.map((piece, index) => {
|
|
156
|
+
const name = exportOf(piece);
|
|
157
|
+
const binding = name === "*" ? `* as ${names[index]}` : `{ ${name} as ${names[index]} }`;
|
|
158
|
+
return `import ${binding} from ${JSON.stringify(specifierOf(piece))};`;
|
|
159
|
+
}),
|
|
160
|
+
`__akanLoadedAfter(${names.join(", ")});`,
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
//? A piece registers only the models a page names; the whole runtime also registers every one a registry walker lists.
|
|
165
|
+
get wholeImports(): string[] {
|
|
166
|
+
return this.#after([`${this.#table.scope}/lib/cnst`, ...this.#wholePieces], (piece) => this.#absoluteOf(piece));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get wholeSource() {
|
|
170
|
+
return `${this.wholeImports.join("\n")}\n`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fileNameOf(absPath: string): string | null {
|
|
174
|
+
const relative = path.relative(this.#workspaceRoot, absPath).split(path.sep).join("/");
|
|
175
|
+
return /^(?:apps|libs)\//.test(relative) ? relative : null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//? A scope's `lib/useClient.ts` holds every descriptor in one macro call: answered from the pieces, none ships twice.
|
|
179
|
+
redirectOf(absPath: string): string | null {
|
|
180
|
+
if (this.#planned) return null;
|
|
181
|
+
const [, scope, file] = this.fileNameOf(absPath)?.match(wholeFile) ?? [];
|
|
182
|
+
if (!scope || !file) return null;
|
|
183
|
+
const piece = assembled[file as keyof typeof assembled];
|
|
184
|
+
if (!this.#pieces.has(`${scope}/lib/__client/${piece}`)) return null;
|
|
185
|
+
const own = `export * from "./__client/${piece}";\n`;
|
|
186
|
+
//? A lib's store root built after an app store's piece puts the lib's actions back over the app's, so the app's root follows it.
|
|
187
|
+
return file === "st" && scope !== this.#table.scope
|
|
188
|
+
? `${own}import ${JSON.stringify(this.#absoluteOf(`${this.#table.scope}/lib/__client/${piece}`))};\n`
|
|
189
|
+
: own;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//* Over a bundle whose every entry loads the whole runtime: the entries that keep it, whose files stay as written.
|
|
193
|
+
plan({ importsByModule, entries, entriesByRoute }: RouteRuntimeBundle): Set<string> {
|
|
194
|
+
const graph = new RouteRuntimeGraph(importsByModule, (absPath) => runtimeFile.test(this.fileNameOf(absPath) ?? ""));
|
|
195
|
+
const stores = graph.modules.filter((module) => storeFile.test(this.fileNameOf(module) ?? ""));
|
|
196
|
+
const storeSide = graph.closureOf(stores);
|
|
197
|
+
const planned: Planned = { graph, storeSide, routed: new Set(), narrow: null };
|
|
198
|
+
this.#planned = planned;
|
|
199
|
+
for (const memo of [this.#storeNeeds, this.#ownNeeds, this.#sideNeeds, this.#routes, this.#wholeReaders])
|
|
200
|
+
memo.clear();
|
|
201
|
+
for (const memo of Object.values(this.#why)) memo.clear();
|
|
202
|
+
const decided = new RouteRuntimePlan({
|
|
203
|
+
graph,
|
|
204
|
+
entries: new Set(entries),
|
|
205
|
+
routes: entriesByRoute ?? null,
|
|
206
|
+
readsWhole: (file) => this.#readsWhole(file, planned),
|
|
207
|
+
storeSide: (root) => this.#storeSideWhy(root, planned),
|
|
208
|
+
bringsPieces: (file) =>
|
|
209
|
+
!!this.#routeOf(file)?.pieces.some((piece) => !piece.endsWith("/_base.fetch") && !piece.endsWith(".cnst")),
|
|
210
|
+
nameOf: (file) => this.#nameOf(file),
|
|
211
|
+
});
|
|
212
|
+
this.#decided = decided;
|
|
213
|
+
return this.#settled(planned, decided);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
keep(entries: Iterable<string>, why: RouteRuntimeKeep): Set<string> {
|
|
217
|
+
if (!this.#planned || !this.#decided) return new Set();
|
|
218
|
+
this.#decided.keep(entries, why);
|
|
219
|
+
return this.#settled(this.#planned, this.#decided);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
keepAll(): Set<string> {
|
|
223
|
+
return this.keep(this.#decided?.roots ?? [], { rule: "reference" });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#settled(planned: Planned, decided: RouteRuntimePlan): Set<string> {
|
|
227
|
+
const { graph } = planned;
|
|
228
|
+
const keeps = new Set(decided.keeps.keys());
|
|
229
|
+
const routed = graph.closureOf(decided.roots.filter((root) => !keeps.has(root)));
|
|
230
|
+
//? A set yields what is added while it is walked: a store reached through its piece brings the files it imports.
|
|
231
|
+
for (const file of routed)
|
|
232
|
+
for (const piece of this.#routeOf(file)?.pieces ?? []) {
|
|
233
|
+
const [, , module] = piece.match(storePiece) ?? [];
|
|
234
|
+
if (module) graph.closureOf(this.#storeFilesOf(module, graph), routed);
|
|
235
|
+
}
|
|
236
|
+
planned.routed = routed;
|
|
237
|
+
planned.narrow = decided.told ? this.#narrowOf(keeps, [...decided.routes.values()], graph) : null;
|
|
238
|
+
return keeps;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
//? A model a page registers itself pays only on a page loading no whole runtime. Anywhere else its piece answers
|
|
242
|
+
//? from `lib/cnst.ts`, which keeps every constant in the one chunk the whole runtime has them in.
|
|
243
|
+
#narrowOf(keeps: Set<string>, routes: (readonly string[])[], graph: RouteRuntimeGraph): NarrowPieces {
|
|
244
|
+
const narrow: NarrowPieces = { pieces: new Set(), constantFiles: new Set() };
|
|
245
|
+
const files = new Set<string>();
|
|
246
|
+
const visit = (file: string) => {
|
|
247
|
+
const name = this.fileNameOf(file) ?? "";
|
|
248
|
+
if (!runtimeFile.test(name) && !constantsFile.test(name)) files.add(file);
|
|
249
|
+
};
|
|
250
|
+
const reach = (first: string) => {
|
|
251
|
+
const queue = [first];
|
|
252
|
+
for (const piece of queue) {
|
|
253
|
+
if (narrow.pieces.has(piece) || !this.#pieces.has(piece)) continue;
|
|
254
|
+
narrow.pieces.add(piece);
|
|
255
|
+
for (const target of this.#pieceImportsOf(piece))
|
|
256
|
+
if (this.#pieces.has(target)) queue.push(target);
|
|
257
|
+
else visit(path.join(this.#workspaceRoot, `${target}.ts`));
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
for (const route of routes) if (!route.some((entry) => keeps.has(entry))) route.forEach(visit);
|
|
261
|
+
//? The entry of a page loading pieces imports the framework's own descriptors before any file of the page.
|
|
262
|
+
if (files.size) reach(`${this.#table.scope}/lib/${StoreBootstrap.baseDescriptors}`);
|
|
263
|
+
for (const file of files) {
|
|
264
|
+
for (const imported of graph.importsOf(file)) visit(imported);
|
|
265
|
+
for (const lazy of graph.lazyImportsOf(file)) if (!keeps.has(lazy)) visit(lazy);
|
|
266
|
+
if (RuntimeScopeReport.isConstantFile(this.fileNameOf(file) ?? "")) narrow.constantFiles.add(file);
|
|
267
|
+
for (const piece of this.#routeOf(file)?.pieces ?? []) reach(piece);
|
|
268
|
+
}
|
|
269
|
+
return narrow;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#pieceImportsOf(piece: string): string[] {
|
|
273
|
+
const scope = piece.split("/").slice(0, 2).join("/");
|
|
274
|
+
const source = this.#read(this.#absoluteOf(piece)) ?? "";
|
|
275
|
+
return [...source.matchAll(pieceImport)].map(([, specifier = ""]) =>
|
|
276
|
+
specifier.startsWith(".") ? path.posix.join(`${scope}/lib/__client`, specifier) : specifier.slice(1),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
get decided(): RouteRuntimePlan | null {
|
|
281
|
+
return this.#decided;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
//? A scope's `lib/useClient.ts` holds every module's descriptors in one macro call, and so would hold again what a
|
|
285
|
+
//? piece on the same page holds: the absolute paths of the pieces it takes them from instead, or null to stay as written.
|
|
286
|
+
takenFromPieces(absPath: string): string[] | null {
|
|
287
|
+
const [, scope, file] = this.fileNameOf(absPath)?.match(wholeFile) ?? [];
|
|
288
|
+
const narrow = this.#planned?.narrow;
|
|
289
|
+
if (!scope || file !== "useClient" || !narrow) return null;
|
|
290
|
+
const pieces = [...narrow.pieces]
|
|
291
|
+
.filter((piece) => piece.startsWith(`${scope}/lib/__client/`) && piece.endsWith(".fetch"))
|
|
292
|
+
.sort(byCodeUnit);
|
|
293
|
+
return pieces.length ? pieces.map((piece) => this.#absoluteOf(piece)) : null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
//? The files the last plan found to need every module, which a page built to load pieces must not reach.
|
|
297
|
+
get wholeReaders(): Set<string> {
|
|
298
|
+
return new Set([...this.#wholeReaders].filter(([, why]) => why).map(([file]) => file));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
//? The files transformed since the last plan that keep every constant: each `akan quality runtime` names, and each
|
|
302
|
+
//? registration piece no page loading pieces reaches.
|
|
303
|
+
get wholeConstantReaders(): Set<string> {
|
|
304
|
+
return new Set([...this.#routes].filter(([, { result }]) => result?.wholeConstants).map(([file]) => file));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
#storeFilesOf(module: string, graph: RouteRuntimeGraph): string[] {
|
|
308
|
+
return this.#table.layers
|
|
309
|
+
.flatMap(({ scope }) =>
|
|
310
|
+
[module, `_${module}`].map((dir) => path.join(this.#workspaceRoot, scope, "lib", dir, `${module}.store.ts`)),
|
|
311
|
+
)
|
|
312
|
+
.filter((file) => graph.has(file));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#nameOf(absPath: string) {
|
|
316
|
+
if (StoreBootstrap.modelRuntimeFilter.test(absPath)) return "akanjs/ui/modelRuntime.ts";
|
|
317
|
+
const inPackage = absPath.match(/[\\/](?:node_modules|pkgs)[\\/](akanjs[\\/].+)$/)?.[1];
|
|
318
|
+
return this.fileNameOf(absPath) ?? inPackage?.split(path.sep).join("/") ?? absPath;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
#storeSideWhy(root: string, planned: Planned): string | null {
|
|
322
|
+
if (!planned.storeSide.has(root)) return null;
|
|
323
|
+
const needs = this.#sideNeedsOf(root, planned);
|
|
324
|
+
if (needs?.length === 0) return null;
|
|
325
|
+
const names = needs?.map((need) => need.slice(need.lastIndexOf("/") + 1, -".st".length)).join(", ");
|
|
326
|
+
return needs
|
|
327
|
+
? `a store file imports it and its code names the store of ${names}, which no importer can load first for an entry`
|
|
328
|
+
: `a store file imports it and the stores its code names cannot be told (${saidOf(this.#why.side.get(root))})`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#readsWhole(absPath: string, planned: Planned): RouteRuntimeWhy | null {
|
|
332
|
+
const known = this.#wholeReaders.get(absPath);
|
|
333
|
+
if (known !== undefined) return known;
|
|
334
|
+
const reads = this.#readsWholeNow(absPath, planned);
|
|
335
|
+
this.#wholeReaders.set(absPath, reads);
|
|
336
|
+
return reads;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#readsWholeNow(absPath: string, planned: Planned): RouteRuntimeWhy | null {
|
|
340
|
+
const file = this.#nameOf(absPath);
|
|
341
|
+
if (StoreBootstrap.modelRuntimeFilter.test(absPath))
|
|
342
|
+
return { file, message: "is what a framework component reading models by name imports" };
|
|
343
|
+
const routed = this.#routeOf(absPath);
|
|
344
|
+
if (routed)
|
|
345
|
+
return routed.perModule
|
|
346
|
+
? null
|
|
347
|
+
: (this.#why.whole.get(absPath) ?? { file, message: "names a module only the whole runtime answers" });
|
|
348
|
+
//? A file left as written (a macro host, a generated barrel, one outside the scopes) loads only what it imports.
|
|
349
|
+
const whole = planned.graph.importsOf(absPath).find((imported) => wholeFile.test(this.fileNameOf(imported) ?? ""));
|
|
350
|
+
if (whole)
|
|
351
|
+
return { file, message: `is left as written and imports ${this.#nameOf(whole)}, the runtime of every module` };
|
|
352
|
+
if (planned.storeSide.has(absPath)) return null;
|
|
353
|
+
const side = planned.graph
|
|
354
|
+
.importsOf(absPath)
|
|
355
|
+
.find((imported) => planned.storeSide.has(imported) && this.#sideNeedsOf(imported, planned)?.length !== 0);
|
|
356
|
+
return side
|
|
357
|
+
? {
|
|
358
|
+
file,
|
|
359
|
+
message: `is left as written and imports ${this.#nameOf(side)}, whose code names stores this file cannot load first`,
|
|
360
|
+
}
|
|
361
|
+
: null;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
#routeOf(absPath: string): RouteRuntimeResult | null {
|
|
365
|
+
const source = this.fileNameOf(absPath) ? this.#read(absPath) : null;
|
|
366
|
+
return source === null ? null : this.#route(source, absPath);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
//? A constant file is reached through a registration piece the planning bundle never holds, so the plan tells it apart.
|
|
370
|
+
transform(source: string, absPath: string): RouteRuntimeResult | null {
|
|
371
|
+
const key = path.resolve(absPath);
|
|
372
|
+
const fileName = this.fileNameOf(key) ?? "";
|
|
373
|
+
const narrow = this.#planned?.narrow;
|
|
374
|
+
const [, model] = fileName.match(constantPieceFile) ?? [];
|
|
375
|
+
if (narrow && model) {
|
|
376
|
+
if (narrow.pieces.has(fileName.slice(0, -".ts".length))) return null;
|
|
377
|
+
//? A lib's `lib/cnst.ts` alone would split the app's: the bundler groups modules by the entries reaching them.
|
|
378
|
+
const appConstants = fileName.startsWith(`${this.#table.scope}/`)
|
|
379
|
+
? '"../cnst"'
|
|
380
|
+
: JSON.stringify(this.#absoluteOf(`${this.#table.scope}/lib/cnst`));
|
|
381
|
+
//? Every answering piece hands out the same `loaded`: a piece importing several takes one name from that chunk.
|
|
382
|
+
const result = {
|
|
383
|
+
source: `export { ${model} } from "../cnst";\nexport * as loaded from ${appConstants};\n`,
|
|
384
|
+
perModule: true,
|
|
385
|
+
pieces: [],
|
|
386
|
+
wholeConstants: true,
|
|
387
|
+
};
|
|
388
|
+
this.#routes.set(key, { source, result });
|
|
389
|
+
return result;
|
|
390
|
+
}
|
|
391
|
+
const [, scope] = fileName.match(constantsFile) ?? [];
|
|
392
|
+
if (narrow && scope) {
|
|
393
|
+
const answered = this.#answeredFrom(source, narrow, scope);
|
|
394
|
+
if (answered || scope === this.#table.scope) return answered;
|
|
395
|
+
}
|
|
396
|
+
if (RuntimeScopeReport.isConstantFile(fileName))
|
|
397
|
+
return narrow?.constantFiles.has(key) === false ? null : this.#route(source, key);
|
|
398
|
+
if (this.#planned?.graph.has(key) && !this.#planned.routed.has(key)) return null;
|
|
399
|
+
return this.#route(source, key);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
//? The bundler gives a module its own chunk, empty or not, wherever the pages reaching it differ from its neighbours':
|
|
403
|
+
//? imported from here, an answering piece is reached by exactly the pages reaching this file and lands in its chunk.
|
|
404
|
+
#answeredFrom(source: string, narrow: NarrowPieces, scope: string): RouteRuntimeResult | null {
|
|
405
|
+
const registered = source.replace(registration, (line, model: string) =>
|
|
406
|
+
narrow.pieces.has(`${scope}/lib/__client/${model}.cnst`)
|
|
407
|
+
? `export { ${model} } from "./__client/${model}.cnst";`
|
|
408
|
+
: line,
|
|
409
|
+
);
|
|
410
|
+
const answering =
|
|
411
|
+
scope === this.#table.scope
|
|
412
|
+
? [...this.#pieces]
|
|
413
|
+
.filter((piece) => constantPieceFile.test(`${piece}.ts`) && !narrow.pieces.has(piece))
|
|
414
|
+
.sort(byCodeUnit)
|
|
415
|
+
: [];
|
|
416
|
+
if (registered === source && !answering.length) return null;
|
|
417
|
+
const imports = answering.map((piece) => `import ${JSON.stringify(this.#absoluteOf(piece))};`);
|
|
418
|
+
return { source: `${registered}\n${imports.join("\n")}\n`, perModule: false, pieces: [] };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#route(source: string, absPath: string): RouteRuntimeResult | null {
|
|
422
|
+
const known = this.#routes.get(absPath);
|
|
423
|
+
if (known?.source === source) return known.result;
|
|
424
|
+
const result = this.#routeNow(source, absPath);
|
|
425
|
+
this.#routes.set(absPath, { source, result });
|
|
426
|
+
return result;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
#routeNow(source: string, absPath: string): RouteRuntimeResult | null {
|
|
430
|
+
const fileName = this.fileNameOf(absPath);
|
|
431
|
+
const referenced = fileName ? ModelReference.modelOf(fileName) : null;
|
|
432
|
+
if (referenced) return this.#referenceOf(source, referenced, absPath);
|
|
433
|
+
if (!fileName || macroAttribute.test(source)) return null;
|
|
434
|
+
if (RuntimeScopeReport.isConstantFile(fileName)) return this.#constantFileOf(source, fileName);
|
|
435
|
+
if (!RuntimeScopeReport.isRuntimeSource(fileName)) return null;
|
|
436
|
+
const [, storeScope, storeModule] = fileName.match(storeFile) ?? [];
|
|
437
|
+
const planned = this.#planned;
|
|
438
|
+
const storeSide = !!storeModule || !!planned?.storeSide.has(absPath);
|
|
439
|
+
const sideNeeds = planned && !storeSide ? this.#importedNeedsOf(absPath, planned) : [];
|
|
440
|
+
const mayUse = RuntimeImports.mayUseRuntime(source, fileName) || RuntimeImports.mayUseConstants(source, fileName);
|
|
441
|
+
if (!storeModule && !sideNeeds.length && !mayUse) return { source, perModule: true, pieces: [] };
|
|
442
|
+
const sourceFile = parsed(fileName, source);
|
|
443
|
+
if (!sourceFile) return null;
|
|
444
|
+
const verdict = RuntimeUse.analyze({ fileName, text: source, sourceFile }, this.#table);
|
|
445
|
+
const pieces = storeSide
|
|
446
|
+
? (this.#descriptorsOf(
|
|
447
|
+
verdict,
|
|
448
|
+
storeScope && storeModule ? { scope: storeScope, module: storeModule } : undefined,
|
|
449
|
+
) ?? [])
|
|
450
|
+
: this.#piecesOf(verdict, sideNeeds);
|
|
451
|
+
if (!pieces) {
|
|
452
|
+
this.#why.whole.set(absPath, this.#whyWhole(fileName, verdict, sideNeeds));
|
|
453
|
+
const edit = RouteRuntimeEdits.leading(sourceFile, this.wholeImports);
|
|
454
|
+
return { source: RouteRuntimeEdits.apply(source, [edit]), perModule: false, pieces: [] };
|
|
455
|
+
}
|
|
456
|
+
const constants = this.#constants.rewrite(sourceFile, fileName, verdict.constants);
|
|
457
|
+
const edits = sourceFile.statements.flatMap((statement) =>
|
|
458
|
+
RouteRuntimeEdits.importEdit(statement, sourceFile, fileName, {
|
|
459
|
+
handlesOnly: storeSide && !verdict.resolvable,
|
|
460
|
+
constants,
|
|
461
|
+
}),
|
|
462
|
+
);
|
|
463
|
+
const result = this.#routed(source, sourceFile, edits, pieces, constants);
|
|
464
|
+
return constants ? result : { ...result, wholeConstants: true };
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
#constantFileOf(source: string, fileName: string): RouteRuntimeResult | null {
|
|
468
|
+
if (!RuntimeImports.mayUseConstants(source, fileName)) return null;
|
|
469
|
+
const sourceFile = parsed(fileName, source);
|
|
470
|
+
if (!sourceFile) return null;
|
|
471
|
+
const { constants: use } = RuntimeUse.analyze({ fileName, text: source, sourceFile }, this.#table);
|
|
472
|
+
const constants = this.#constants.rewrite(sourceFile, fileName, use, { constantFile: true });
|
|
473
|
+
if (!constants) return { source, perModule: true, pieces: [], wholeConstants: true };
|
|
474
|
+
if (!constants.edits.length) return null;
|
|
475
|
+
return this.#routed(source, sourceFile, [], [], constants);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
#routed(
|
|
479
|
+
source: string,
|
|
480
|
+
sourceFile: ts.SourceFile,
|
|
481
|
+
edits: RouteRuntimeTextEdit[],
|
|
482
|
+
pieces: string[],
|
|
483
|
+
constants: ConstantRewrite | null,
|
|
484
|
+
): RouteRuntimeResult {
|
|
485
|
+
const all = [...(constants?.pieces ?? []), ...pieces];
|
|
486
|
+
const leading = [...this.#after(all, (piece) => `@${piece}`), ...(constants?.imports ?? [])];
|
|
487
|
+
const changes = [...edits, ...(constants?.edits ?? [])];
|
|
488
|
+
if (leading.length) changes.push(RouteRuntimeEdits.leading(sourceFile, leading));
|
|
489
|
+
return { source: RouteRuntimeEdits.apply(source, changes), perModule: true, pieces: all };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
//? Loads what a file writing `st.use.<model>` loads: a framework component reads that store for a handed object.
|
|
493
|
+
#referenceOf(source: string, model: string, absPath: string): RouteRuntimeResult {
|
|
494
|
+
const store = this.#table.lookup("st.use", model);
|
|
495
|
+
const needs = store ? "store" : "descriptor";
|
|
496
|
+
const named = store ?? this.#table.lookup("fetch.serializedSignal", model);
|
|
497
|
+
const declaring = named?.owners ?? [];
|
|
498
|
+
//? Another store with a state field named like the model declares the same `st.use` key.
|
|
499
|
+
const own = declaring.filter((owner) => owner.module === model);
|
|
500
|
+
const owners = (own.length ? own : declaring).map((owner) => ({ ...owner, needs }) as const);
|
|
501
|
+
const constants = { reads: [], imports: [], problems: [] };
|
|
502
|
+
const verdict = {
|
|
503
|
+
resolvable: true,
|
|
504
|
+
owners,
|
|
505
|
+
baseStore: false,
|
|
506
|
+
baseDescriptor: false,
|
|
507
|
+
reads: true,
|
|
508
|
+
constants,
|
|
509
|
+
} as const;
|
|
510
|
+
const pieces = this.#piecesOf(verdict);
|
|
511
|
+
if (!pieces) this.#why.whole.set(absPath, this.#whyWhole(this.#nameOf(absPath), verdict, []));
|
|
512
|
+
const loads = pieces ? this.#after(pieces, (piece) => `@${piece}`) : this.wholeImports;
|
|
513
|
+
return { source: ModelReference.sourceOf(source, loads), perModule: !!pieces, pieces: pieces ?? [] };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
#descriptorOf(scope: string, module: string) {
|
|
517
|
+
return [this.#table.scope, scope]
|
|
518
|
+
.map((holder) => `${holder}/lib/__client/${module}.fetch`)
|
|
519
|
+
.find((piece) => this.#pieces.has(piece));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
//? Null where a descriptor the file names has no piece: only the whole runtime answers it then.
|
|
523
|
+
#descriptorsOf(
|
|
524
|
+
verdict: RuntimeUseVerdict,
|
|
525
|
+
store?: { scope: string; module: string },
|
|
526
|
+
strict = false,
|
|
527
|
+
): string[] | null {
|
|
528
|
+
const descriptors = new Set<string>();
|
|
529
|
+
if (verdict.resolvable && verdict.baseDescriptor) descriptors.add(`${this.#table.scope}/lib/__client/_base.fetch`);
|
|
530
|
+
const own = store ? [...this.#table.dependenciesOf(store.scope, store.module), store] : [];
|
|
531
|
+
for (const { scope, module } of own) {
|
|
532
|
+
const descriptor = this.#descriptorOf(scope, module);
|
|
533
|
+
if (descriptor) descriptors.add(descriptor);
|
|
534
|
+
}
|
|
535
|
+
for (const { scope, module, needs } of verdict.resolvable ? verdict.owners : []) {
|
|
536
|
+
const descriptor = this.#descriptorOf(scope, module);
|
|
537
|
+
if (descriptor) descriptors.add(descriptor);
|
|
538
|
+
else if (needs === "descriptor" && strict) return null;
|
|
539
|
+
}
|
|
540
|
+
return [...descriptors];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
//? A store-side file imports no store piece: the piece of a store it names imports that store's file, which may
|
|
544
|
+
//? import this one back. What its code reaches when it runs is loaded by each file outside importing it instead.
|
|
545
|
+
#piecesOf(verdict: RuntimeUseVerdict, sideNeeds: (string[] | null)[] = []): string[] | null {
|
|
546
|
+
if (!verdict.resolvable) return null;
|
|
547
|
+
const descriptors = this.#descriptorsOf(verdict, undefined, true);
|
|
548
|
+
if (!descriptors) return null;
|
|
549
|
+
const stores = new Set<string>();
|
|
550
|
+
for (const { scope, module, needs } of verdict.owners) {
|
|
551
|
+
if (needs !== "store") continue;
|
|
552
|
+
const store = `${scope}/lib/__client/${module}.st`;
|
|
553
|
+
if (!this.#pieces.has(store) || !this.#reaches(store, stores)) return null;
|
|
554
|
+
}
|
|
555
|
+
for (const needs of sideNeeds) if (!needs?.every((need) => this.#reaches(need, stores))) return null;
|
|
556
|
+
return [...descriptors, ...stores];
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
#whyWhole(file: string, verdict: RuntimeUseVerdict, sideNeeds: (string[] | null)[]): RouteRuntimeWhy {
|
|
560
|
+
if (!verdict.resolvable) return { file, line: verdict.line, message: verdict.message };
|
|
561
|
+
const unsynced = (scope: string, module: string) => ({ file, message: RouteRuntime.#unsynced(scope, module) });
|
|
562
|
+
const seen = new Set<string>();
|
|
563
|
+
for (const { scope, module, needs } of verdict.owners) {
|
|
564
|
+
if (needs === "descriptor" && !this.#descriptorOf(scope, module)) return unsynced(scope, module);
|
|
565
|
+
if (needs !== "store") continue;
|
|
566
|
+
const store = `${scope}/lib/__client/${module}.st`;
|
|
567
|
+
if (!this.#pieces.has(store)) return unsynced(scope, module);
|
|
568
|
+
const why = this.#whyUnreached(store, seen);
|
|
569
|
+
if (why) return why;
|
|
570
|
+
}
|
|
571
|
+
for (const needs of sideNeeds)
|
|
572
|
+
for (const need of needs ?? []) {
|
|
573
|
+
const why = this.#whyUnreached(need, seen);
|
|
574
|
+
if (why) return why;
|
|
575
|
+
}
|
|
576
|
+
return { file, message: "imports a file a store file imports, whose code reads names that cannot be told" };
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
#whyUnreached(store: string, seen: Set<string>): RouteRuntimeWhy | null {
|
|
580
|
+
if (seen.has(store)) return null;
|
|
581
|
+
seen.add(store);
|
|
582
|
+
const [, , module] = store.match(storePiece) ?? [];
|
|
583
|
+
if (!module) return null;
|
|
584
|
+
for (const { scope } of this.#table.layers) {
|
|
585
|
+
if (!this.#pieces.has(`${scope}/lib/__client/${module}.st`)) continue;
|
|
586
|
+
const needs = this.#needsOf(scope, module);
|
|
587
|
+
if (!needs)
|
|
588
|
+
return (
|
|
589
|
+
this.#why.store.get(`${scope}/${module}`) ?? {
|
|
590
|
+
file: `${scope}/lib/${module}/${module}.store.ts`,
|
|
591
|
+
message: "reads names that cannot be told",
|
|
592
|
+
}
|
|
593
|
+
);
|
|
594
|
+
for (const need of needs) {
|
|
595
|
+
const why = this.#whyUnreached(need, seen);
|
|
596
|
+
if (why) return why;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
//? False where a store reached from this one picks a name at run time, so only the whole runtime answers it.
|
|
603
|
+
#reaches(store: string, reached: Set<string>): boolean {
|
|
604
|
+
if (reached.has(store)) return true;
|
|
605
|
+
reached.add(store);
|
|
606
|
+
const [, , module] = store.match(storePiece) ?? [];
|
|
607
|
+
if (!module) return true;
|
|
608
|
+
return this.#table.layers
|
|
609
|
+
.filter(({ scope }) => this.#pieces.has(`${scope}/lib/__client/${module}.st`))
|
|
610
|
+
.every(({ scope }) => {
|
|
611
|
+
const needs = this.#needsOf(scope, module);
|
|
612
|
+
return !!needs && needs.every((need) => this.#reaches(need, reached));
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
//? What a file naming a store loads besides the pieces of the names it writes: a save of a store file can change it.
|
|
617
|
+
get storeNeeds(): Record<string, string[] | null> {
|
|
618
|
+
const needs: Record<string, string[] | null> = {};
|
|
619
|
+
for (const piece of [...this.#pieces].sort()) {
|
|
620
|
+
const [, scope, module] = piece.match(storePiece) ?? [];
|
|
621
|
+
if (scope && module) needs[piece] = this.#needsOf(scope, module);
|
|
622
|
+
}
|
|
623
|
+
return needs;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
//? The store pieces a store file's own code names: null where they cannot be told.
|
|
627
|
+
#needsOf(scope: string, module: string): string[] | null {
|
|
628
|
+
const key = `${scope}/${module}`;
|
|
629
|
+
const known = this.#storeNeeds.get(key);
|
|
630
|
+
if (known !== undefined) return known;
|
|
631
|
+
const [file] = [module, `_${module}`]
|
|
632
|
+
.map((dir) => path.join(this.#workspaceRoot, scope, "lib", dir, `${module}.store.ts`))
|
|
633
|
+
.filter((candidate) => this.#read(candidate) !== null);
|
|
634
|
+
const needs = !file ? [] : this.#planned ? this.#sideNeedsOf(file, this.#planned) : this.#ownNeedsOf(file);
|
|
635
|
+
const why = file && (this.#why.side.get(file) ?? this.#why.needs.get(file));
|
|
636
|
+
if (!needs && why) this.#why.store.set(key, why);
|
|
637
|
+
this.#storeNeeds.set(key, needs);
|
|
638
|
+
return needs;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
//? What a file outside the store side loads for each store-side file it imports, as it does for a store it names.
|
|
642
|
+
#importedNeedsOf(absPath: string, planned: Planned): (string[] | null)[] {
|
|
643
|
+
return planned.graph
|
|
644
|
+
.importsOf(absPath)
|
|
645
|
+
.filter((file) => planned.storeSide.has(file))
|
|
646
|
+
.map((file) => this.#sideNeedsOf(file, planned))
|
|
647
|
+
.filter((needs) => needs?.length !== 0);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
//? With the store-side files it imports, whose code runs inside the same import cycle.
|
|
651
|
+
#sideNeedsOf(absPath: string, planned: Planned): string[] | null {
|
|
652
|
+
const known = this.#sideNeeds.get(absPath);
|
|
653
|
+
if (known !== undefined) return known;
|
|
654
|
+
const needs = new Set<string>();
|
|
655
|
+
let told = true;
|
|
656
|
+
for (const file of planned.graph.closureOf([absPath])) {
|
|
657
|
+
const own = this.#ownNeedsOf(file);
|
|
658
|
+
const why = own ? undefined : this.#why.needs.get(file);
|
|
659
|
+
if (why && !this.#why.side.has(absPath)) this.#why.side.set(absPath, why);
|
|
660
|
+
if (!own) told = false;
|
|
661
|
+
for (const need of own ?? []) needs.add(need);
|
|
662
|
+
}
|
|
663
|
+
const result = told ? [...needs] : null;
|
|
664
|
+
this.#sideNeeds.set(absPath, result);
|
|
665
|
+
return result;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
#ownNeedsOf(absPath: string): string[] | null {
|
|
669
|
+
const known = this.#ownNeeds.get(absPath);
|
|
670
|
+
if (known !== undefined) return known;
|
|
671
|
+
const needs = this.#readNeeds(absPath);
|
|
672
|
+
this.#ownNeeds.set(absPath, needs);
|
|
673
|
+
return needs;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
#readNeeds(absPath: string): string[] | null {
|
|
677
|
+
const fileName = this.fileNameOf(absPath);
|
|
678
|
+
if (!fileName || !RuntimeScopeReport.isRuntimeSource(fileName)) return [];
|
|
679
|
+
const text = this.#read(absPath);
|
|
680
|
+
if (text === null) return [];
|
|
681
|
+
const [, , module] = fileName.match(storeFile) ?? [];
|
|
682
|
+
if (!module && !RuntimeImports.mayUseRuntime(text, fileName)) return [];
|
|
683
|
+
const untold = (message: string, line?: number) => {
|
|
684
|
+
this.#why.needs.set(absPath, { file: fileName, message, ...(line ? { line } : {}) });
|
|
685
|
+
return null;
|
|
686
|
+
};
|
|
687
|
+
const sourceFile = macroAttribute.test(text) ? null : parsed(fileName, text);
|
|
688
|
+
if (!sourceFile) return untold("holds a macro import or does not parse, so the names it reads are not followed");
|
|
689
|
+
const verdict = RuntimeUse.analyze({ fileName, text, sourceFile }, this.#table);
|
|
690
|
+
if (!verdict.resolvable) return untold(verdict.message, verdict.line);
|
|
691
|
+
const stores: string[] = [];
|
|
692
|
+
const unsynced = ({ scope, module: name }: { scope: string; module: string }) =>
|
|
693
|
+
untold(RouteRuntime.#unsynced(scope, name));
|
|
694
|
+
for (const owner of verdict.owners) {
|
|
695
|
+
if (owner.needs === "descriptor" && !this.#descriptorOf(owner.scope, owner.module)) return unsynced(owner);
|
|
696
|
+
if (owner.needs !== "store" || owner.module === module) continue;
|
|
697
|
+
const store = `${owner.scope}/lib/__client/${owner.module}.st`;
|
|
698
|
+
if (!this.#pieces.has(store)) return unsynced(owner);
|
|
699
|
+
stores.push(store);
|
|
700
|
+
}
|
|
701
|
+
return stores;
|
|
702
|
+
}
|
|
703
|
+
}
|