@akanjs/devkit 3.3.0-rc.3 → 3.3.0-rc.30
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 +547 -0
- package/DEV_RUNTIME_KNOBS.md +1 -1
- package/README.ko.md +3 -2
- package/README.md +3 -3
- package/akanApp/akanApp.host.test.ts +1 -1
- package/akanApp/akanApp.host.ts +11 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +66 -7
- package/akanConfig/akanConfig.ts +53 -204
- package/akanConfig/akanWorkers.test.ts +39 -0
- package/akanConfig/akanWorkers.ts +32 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +37 -0
- package/akanConfig/nativeKeys.ts +52 -0
- package/akanConfig/types.ts +1 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +32 -0
- package/app/applicationBuildReporter.ts +4 -0
- package/app/applicationBuildRunner.test.ts +275 -0
- package/app/applicationBuildRunner.ts +126 -5
- package/app/dictionarySizes.ts +94 -0
- 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 +60 -8
- package/biome.base.json +5 -1
- 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-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 +4 -14
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-runtime/SKILL.md +20 -0
- package/codeAgent/skills/akan-service/SKILL.md +2 -5
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +2 -5
- package/context/akanContext.ts +2 -0
- package/docs/apiReference.test.ts +9 -0
- package/docs/apiReference.ts +14 -0
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1591 -0
- package/executors/executors.test.ts +2 -0
- package/executors/sysExecutor.ts +66 -3
- package/executors/workspaceExecutor.ts +7 -4
- package/frontendBuild/allRoutesBuilder.ts +72 -3
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +515 -12
- package/frontendBuild/clientBuildTypes.ts +15 -0
- package/frontendBuild/clientEntriesBundler.ts +38 -0
- package/frontendBuild/clientEntryDiscovery.test.ts +95 -1
- package/frontendBuild/clientEntryDiscovery.ts +88 -7
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.ts +33 -7
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/devBarrelImports.test.ts +100 -0
- package/frontendBuild/devBarrelImports.ts +82 -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 +316 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +152 -16
- package/frontendBuild/dictionaryConstants.ts +82 -0
- package/frontendBuild/dictionaryPacking.test.ts +84 -0
- package/frontendBuild/dictionaryPacking.ts +92 -0
- package/frontendBuild/dictionaryRoutes.test.ts +186 -0
- package/frontendBuild/dictionaryRoutes.ts +225 -0
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +81 -3
- package/frontendBuild/precompressArtifacts.ts +7 -0
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/routeClientBuilder.ts +288 -15
- package/frontendBuild/routeHandoffs.test.ts +145 -0
- package/frontendBuild/routeHandoffs.ts +115 -0
- package/frontendBuild/routeRuntimeCheck.test.ts +257 -0
- package/frontendBuild/routeRuntimeCheck.ts +241 -0
- package/frontendBuild/routeRuntimePays.test.ts +137 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/rscServerRendererGuard.test.ts +60 -0
- package/frontendBuild/rscServerRendererGuard.ts +68 -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/biomeCheck.test.ts +145 -0
- package/lint/biomeCheck.ts +131 -0
- 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.ts +1 -0
- 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/serverBundleSecrets.ts +43 -0
- package/package.json +15 -169
- package/packageExports.test.ts +38 -24
- package/quality/dictionaryKeys.test.ts +173 -0
- package/quality/dictionaryKeys.ts +415 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +102 -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/test/secretEnvFixture.ts +134 -0
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +26 -4
- package/transforms/barrelImportsPlugin.ts +82 -14
- 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/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +347 -0
- package/transforms/objectMemberImports.ts +537 -0
- package/transforms/objectModule.test.ts +237 -0
- package/transforms/objectModule.ts +458 -0
- package/transforms/objectModuleAnalyzer.ts +104 -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 +115 -2
- package/transforms/serverEnvProfile.ts +117 -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 +18 -11
- package/typecheck/projectTypecheck.test.ts +29 -0
- 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/getRelatedCnsts.ts +7 -0
- package/workspace/scanInfo.ts +1 -1
- package/workspace/workspaceLayout.ts +1 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { realpath, utimes } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { App } from "../commandDecorators";
|
|
5
|
+
import { tempDirs, writeText as write } from "../test/testHelpers";
|
|
6
|
+
import { CsrDevModuleCompiler } from "./csrDevModuleCompiler";
|
|
7
|
+
import { CsrDevPatcher } from "./csrDevPatcher";
|
|
8
|
+
import { CsrDevPaths } from "./csrDevPaths";
|
|
9
|
+
import { CsrDevResolver } from "./csrDevResolver";
|
|
10
|
+
import { DevRouteRuntime } from "./devRouteRuntime";
|
|
11
|
+
import { SsrDevBundler } from "./ssrDevBundler";
|
|
12
|
+
|
|
13
|
+
const makeTempRoot = tempDirs("akan-dev-route-runtime-");
|
|
14
|
+
const tsPaths = { "@apps/*": ["./apps/*"] };
|
|
15
|
+
|
|
16
|
+
const storeSource = (name: string, keys: string[]) =>
|
|
17
|
+
`import { store } from "akanjs/store";\nexport class ${name}Store extends store("${name}" as const, () => ({ ${keys.map((key) => `${key}: 0`).join(", ")} })) {}\n`;
|
|
18
|
+
const piece = (name: string) => `(globalThis as { pieces?: string[] }).pieces?.push("piece:${name}");\nexport {};\n`;
|
|
19
|
+
const reader = (name: string, read: string) =>
|
|
20
|
+
`"use client";\nimport { st } from "@apps/demo/client";\nexport const ${name} = () => [${read}, "${name}-ui"];\n`;
|
|
21
|
+
|
|
22
|
+
const routeWorkspace = async () => {
|
|
23
|
+
const root = await realpath(await makeTempRoot());
|
|
24
|
+
const file = (relative: string) => path.join(root, "apps/demo", relative);
|
|
25
|
+
const files: Record<string, string> = {
|
|
26
|
+
"client.ts": 'export { st } from "./lib/st";\nexport { usePage } from "./lib/useClient";\n',
|
|
27
|
+
"lib/st.ts": 'export const st = { mark: "whole-store" };\n',
|
|
28
|
+
"lib/cnst.ts": 'export const constants = "every-constant";\n',
|
|
29
|
+
"lib/useClient.ts": 'export const usePage = () => "whole-client";\n',
|
|
30
|
+
"lib/usePage.ts":
|
|
31
|
+
'export const st = { use: {} as Record<string, () => unknown> };\nexport const usePage = () => "page";\n',
|
|
32
|
+
"lib/__client/_useClient.ts": 'export const usePage = () => "assembled-client";\n',
|
|
33
|
+
"lib/__client/_st.ts": 'export const st = { mark: "assembled-store" };\n',
|
|
34
|
+
"lib/__client/_base.fetch.ts": 'export const base = "base-descriptors";\n',
|
|
35
|
+
"lib/__client/refs.ts": "export {};\n",
|
|
36
|
+
"lib/_draft/draft.store.ts": storeSource("draft", ["draftText"]),
|
|
37
|
+
"lib/__client/draft.st.ts": piece("draft.st"),
|
|
38
|
+
"lib/_tally/tally.store.ts": storeSource("tally", ["tallyCount"]),
|
|
39
|
+
"lib/__client/tally.st.ts": piece("tally.st"),
|
|
40
|
+
"ui/Draft.tsx": reader("Draft", "st.use.draftText()"),
|
|
41
|
+
"ui/Tally.tsx": reader("Tally", "st.use.tallyCount()"),
|
|
42
|
+
"ui/Note.tsx": reader("Note", "st.use.noteText()"),
|
|
43
|
+
"ui/Pick.tsx":
|
|
44
|
+
'"use client";\nimport { st } from "@apps/demo/client";\nexport const Pick = (name: string) => st.use[name];\n',
|
|
45
|
+
};
|
|
46
|
+
for (const [relative, text] of Object.entries(files)) await write(file(relative), text);
|
|
47
|
+
await write(path.join(root, "tsconfig.json"), JSON.stringify({ compilerOptions: { baseUrl: ".", paths: tsPaths } }));
|
|
48
|
+
const akanjsRoot = path.dirname(Bun.resolveSync("akanjs/package.json", import.meta.dir));
|
|
49
|
+
for (const base of ["package.json", "store/baseSt.ts", "signal/base.signal.ts", "signal/agent.signal.ts"])
|
|
50
|
+
await write(path.join(root, "node_modules/akanjs", base), await Bun.file(path.join(akanjsRoot, base)).text());
|
|
51
|
+
const app = {
|
|
52
|
+
name: "demo",
|
|
53
|
+
cwdPath: path.join(root, "apps/demo"),
|
|
54
|
+
dist: { cwdPath: path.join(root, "dist/apps/demo") },
|
|
55
|
+
workspace: { workspaceRoot: root },
|
|
56
|
+
basePaths: new Set<string>(),
|
|
57
|
+
getConfig: async () => ({
|
|
58
|
+
optimizeImports: [],
|
|
59
|
+
barrelImports: ["@apps/demo/client", "akanjs/common"],
|
|
60
|
+
basePaths: new Set<string>(),
|
|
61
|
+
}),
|
|
62
|
+
getTsConfig: async () => ({ compilerOptions: { paths: tsPaths } }),
|
|
63
|
+
getPublicEnv: () => ({}),
|
|
64
|
+
verbose: () => undefined,
|
|
65
|
+
logger: { error: () => undefined },
|
|
66
|
+
} as unknown as App;
|
|
67
|
+
return { root, file, app };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const writeMemoModel = async (file: (relative: string) => string) => {
|
|
71
|
+
const files: Record<string, string> = {
|
|
72
|
+
"client.ts": [
|
|
73
|
+
'export * as cnst from "./lib/cnst";',
|
|
74
|
+
'export { usePage } from "./lib/usePage";',
|
|
75
|
+
'export { fetch } from "./lib/useClient";',
|
|
76
|
+
'export { st } from "./lib/st";',
|
|
77
|
+
'export { Memo } from "./lib/memo";',
|
|
78
|
+
"",
|
|
79
|
+
].join("\n"),
|
|
80
|
+
"lib/useClient.ts": 'export const usePage = () => "whole-client";\nexport const fetch = {};\n',
|
|
81
|
+
"lib/memo/index.ts": 'import * as Zone from "./Memo.Zone";\nexport const Memo = { Zone };\n',
|
|
82
|
+
"lib/memo/Memo.Zone.tsx": '"use client";\nexport const View = () => "memo-view";\n',
|
|
83
|
+
"lib/memo/memo.constant.ts": 'export const MemoStatus = ["active"] as const;\n',
|
|
84
|
+
"lib/memo/memo.store.ts": storeSource("memo", ["memoText"]),
|
|
85
|
+
"lib/__client/memo.cnst.ts":
|
|
86
|
+
'import * as cnst from "../memo/memo.constant";\nexport const memo = { cnst };\nexport { memo as loaded };\n',
|
|
87
|
+
"lib/__client/memo.fetch.ts": 'import { loaded as cnst0 } from "./memo.cnst";\nexport const loaded = [cnst0];\n',
|
|
88
|
+
"lib/__client/memo.st.ts":
|
|
89
|
+
'import { loaded as cnst0 } from "./memo.cnst";\nimport { loaded as fetch0 } from "./memo.fetch";\nexport const memo = [cnst0, fetch0];\n',
|
|
90
|
+
};
|
|
91
|
+
for (const [relative, text] of Object.entries(files)) await write(file(relative), text);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const sortedIds = (modules: { id: string }[]) =>
|
|
95
|
+
modules.map((module) => module.id).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
96
|
+
|
|
97
|
+
describe("DevRouteRuntime", () => {
|
|
98
|
+
test("a file naming one module's state imports the tab's store and that module's store piece by a relative path", async () => {
|
|
99
|
+
const { root, file } = await routeWorkspace();
|
|
100
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
101
|
+
const source = await Bun.file(file("ui/Draft.tsx")).text();
|
|
102
|
+
|
|
103
|
+
const routed = runtime?.transform(source, file("ui/Draft.tsx"));
|
|
104
|
+
|
|
105
|
+
expect(routed?.perModule).toBe(true);
|
|
106
|
+
expect(routed?.contents).toContain('import { st } from "akanjs/store";');
|
|
107
|
+
expect(routed?.contents).toContain('"../lib/__client/draft.st.ts"');
|
|
108
|
+
expect(routed?.contents).not.toContain('"@apps/');
|
|
109
|
+
expect(routed?.contents).not.toContain("tally.st");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a model's reference file takes that model's store piece by a relative path, by a name it passes to `loadedAfter`", async () => {
|
|
113
|
+
const { root, file } = await routeWorkspace();
|
|
114
|
+
const source = '"use client";\n\nexport const reference = "draft";\n';
|
|
115
|
+
await write(file("lib/__client/draft.ref.ts"), source);
|
|
116
|
+
await write(file("lib/_draft/draft.store.ts"), storeSource("draft", ["draft", "draftText"]));
|
|
117
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
118
|
+
|
|
119
|
+
const routed = runtime?.transform(source, file("lib/__client/draft.ref.ts"));
|
|
120
|
+
|
|
121
|
+
expect(routed?.perModule).toBe(true);
|
|
122
|
+
expect(routed?.contents).toBe(
|
|
123
|
+
'"use client";\nimport { loadedAfter as __akanLoadedAfter } from "akanjs/base";\nimport { draft as __akanLoaded0 } from "./draft.st.ts";\n__akanLoadedAfter(__akanLoaded0);\n\nexport const reference = "draft";\n',
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("lists each store key under the module whose store declares it", async () => {
|
|
128
|
+
const { root } = await routeWorkspace();
|
|
129
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
130
|
+
|
|
131
|
+
expect(runtime?.owners.use["draft (apps/demo)"]).toContain("draftText");
|
|
132
|
+
expect(runtime?.owners.use["tally (apps/demo)"]).toContain("tallyCount");
|
|
133
|
+
expect(runtime?.ownersSource()).toContain('[Symbol.for("akanjs.store.owners")] = {"use":{');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("a store save that declares a name a file reads moves the inputs, the digest and that file's stamp", async () => {
|
|
137
|
+
const { root, file } = await routeWorkspace();
|
|
138
|
+
const source = await Bun.file(file("ui/Note.tsx")).text();
|
|
139
|
+
const inputs = DevRouteRuntime.inputsOf(root, ["apps/demo"]);
|
|
140
|
+
const before = await DevRouteRuntime.read(root, "apps/demo");
|
|
141
|
+
const unknown = before?.transform(source, file("ui/Note.tsx"));
|
|
142
|
+
|
|
143
|
+
await write(file("lib/_draft/draft.store.ts"), storeSource("draft", ["draftText", "noteText"]));
|
|
144
|
+
const after = await DevRouteRuntime.read(root, "apps/demo");
|
|
145
|
+
const known = after?.transform(source, file("ui/Note.tsx"));
|
|
146
|
+
|
|
147
|
+
expect(DevRouteRuntime.inputsOf(root, ["apps/demo"])).not.toBe(inputs);
|
|
148
|
+
expect(after?.digest).not.toBe(before?.digest);
|
|
149
|
+
expect(unknown?.contents).not.toContain("draft.st");
|
|
150
|
+
expect(known?.contents).toContain('"../lib/__client/draft.st.ts"');
|
|
151
|
+
expect(known?.stamp).not.toBe(unknown?.stamp);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("a save of a scalar's constant file moves the inputs, and the table read again holds the name it declares", async () => {
|
|
155
|
+
const { root, file } = await routeWorkspace();
|
|
156
|
+
const scalar = (names: string[]) =>
|
|
157
|
+
`import { via } from "akanjs/constant";\n${names.map((name) => `export class ${name} extends via((field) => ({ lat: field(Number) })) {}\n`).join("")}`;
|
|
158
|
+
await write(file("lib/__scalar/spot/spot.constant.ts"), scalar(["Spot"]));
|
|
159
|
+
const inputs = DevRouteRuntime.inputsOf(root, ["apps/demo"]);
|
|
160
|
+
const before = await DevRouteRuntime.read(root, "apps/demo");
|
|
161
|
+
|
|
162
|
+
await write(file("lib/__scalar/spot/spot.constant.ts"), scalar(["Spot", "SpotRange"]));
|
|
163
|
+
await utimes(file("lib/__scalar/spot/spot.constant.ts"), new Date(), new Date(Date.now() + 5_000));
|
|
164
|
+
const after = await DevRouteRuntime.read(root, "apps/demo");
|
|
165
|
+
|
|
166
|
+
expect(DevRouteRuntime.inputsOf(root, ["apps/demo"])).not.toBe(inputs);
|
|
167
|
+
expect(before?.digest).not.toBe(after?.digest);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("a file edited to read a store by a name picked at run time takes the assembled runtime instead of pieces", async () => {
|
|
171
|
+
const { root, file } = await routeWorkspace();
|
|
172
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
173
|
+
|
|
174
|
+
const routed = runtime?.transform(await Bun.file(file("ui/Pick.tsx")).text(), file("ui/Pick.tsx"));
|
|
175
|
+
|
|
176
|
+
expect(routed?.perModule).toBe(false);
|
|
177
|
+
expect(routed?.contents).toContain(`${root}/apps/demo/lib/__client/_useClient.ts`);
|
|
178
|
+
expect(routed?.contents).toContain(`${root}/apps/demo/lib/__client/_st.ts`);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a lib's store, store piece or assembled store names the loaded app piece of that module and the app's assembled store to run after it", async () => {
|
|
182
|
+
const { root } = await routeWorkspace();
|
|
183
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
184
|
+
const loaded: Record<string, string[]> = {
|
|
185
|
+
"apps/demo/lib/__client/draft.st.ts": ["apps/demo/lib/_draft/draft.store.ts"],
|
|
186
|
+
"apps/demo/lib/__client/_st.ts": ["apps/demo/lib/__client/draft.st.ts"],
|
|
187
|
+
"apps/demo/lib/_draft/draft.store.ts": [],
|
|
188
|
+
};
|
|
189
|
+
const depsOf = (id: string) => loaded[id];
|
|
190
|
+
|
|
191
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/__client/draft.st.ts", depsOf)).toEqual([
|
|
192
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
193
|
+
"apps/demo/lib/__client/_st.ts",
|
|
194
|
+
]);
|
|
195
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/_draft/draft.store.ts", depsOf)).toEqual([
|
|
196
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
197
|
+
"apps/demo/lib/__client/_st.ts",
|
|
198
|
+
]);
|
|
199
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/st.ts", depsOf)).toEqual(["apps/demo/lib/__client/_st.ts"]);
|
|
200
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/__client/_st.ts", depsOf)).toEqual([
|
|
201
|
+
"apps/demo/lib/__client/_st.ts",
|
|
202
|
+
]);
|
|
203
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/_tally/tally.store.ts", depsOf)).toEqual([
|
|
204
|
+
"apps/demo/lib/__client/_st.ts",
|
|
205
|
+
]);
|
|
206
|
+
expect(runtime?.appStoresAfter("apps/demo/lib/__client/draft.st.ts", depsOf)).toEqual([]);
|
|
207
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/__lib/lib.store.ts", depsOf)).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("a lib's store the app's store of that model extends names that app store first", async () => {
|
|
211
|
+
const { root } = await routeWorkspace();
|
|
212
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
213
|
+
const loaded: Record<string, string[]> = {
|
|
214
|
+
"apps/demo/lib/__client/draft.st.ts": ["apps/demo/lib/_draft/draft.store.ts"],
|
|
215
|
+
"apps/demo/lib/__client/_st.ts": ["apps/demo/lib/__client/draft.st.ts"],
|
|
216
|
+
"apps/demo/lib/_draft/draft.store.ts": ["apps/demo/lib/__lib/lib.store.ts"],
|
|
217
|
+
"apps/demo/lib/__lib/lib.store.ts": ["libs/shared/lib/_draft/draft.store.ts"],
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
expect(runtime?.appStoresAfter("libs/shared/lib/_draft/draft.store.ts", (id) => loaded[id])).toEqual([
|
|
221
|
+
"apps/demo/lib/_draft/draft.store.ts",
|
|
222
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
223
|
+
"apps/demo/lib/__client/_st.ts",
|
|
224
|
+
]);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("the dev registry loading per module", () => {
|
|
229
|
+
const compile = async (app: App, root: string, roots: string[]) => {
|
|
230
|
+
const context = await new SsrDevBundler(app).context();
|
|
231
|
+
const paths = new CsrDevPaths(root);
|
|
232
|
+
const resolver = new CsrDevResolver({ paths, context, entryFiles: roots });
|
|
233
|
+
await resolver.prepass();
|
|
234
|
+
const compiler = new CsrDevModuleCompiler({
|
|
235
|
+
app,
|
|
236
|
+
paths,
|
|
237
|
+
resolver,
|
|
238
|
+
context,
|
|
239
|
+
outDir: path.join(root, "out"),
|
|
240
|
+
routePrefix: "/_akan/ssr-dev/",
|
|
241
|
+
});
|
|
242
|
+
return (await compiler.compile(roots, new Set())).modules;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
test("compiles each page's own store pieces once, never the scope's whole store, client runtime or client barrel", async () => {
|
|
246
|
+
const { root, file, app } = await routeWorkspace();
|
|
247
|
+
|
|
248
|
+
const modules = await compile(app, root, [file("ui/Draft.tsx"), file("ui/Tally.tsx")]);
|
|
249
|
+
|
|
250
|
+
expect(sortedIds(modules)).toEqual([
|
|
251
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
252
|
+
"apps/demo/lib/__client/tally.st.ts",
|
|
253
|
+
"apps/demo/ui/Draft.tsx",
|
|
254
|
+
"apps/demo/ui/Tally.tsx",
|
|
255
|
+
]);
|
|
256
|
+
const piece = modules.find((module) => module.id === "apps/demo/lib/__client/draft.st.ts");
|
|
257
|
+
expect(piece?.factory).toContain("module.hot.accept()");
|
|
258
|
+
expect(modules.find((module) => module.id === "apps/demo/ui/Draft.tsx")?.routed).toBeString();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("a scope's lib/st.ts and lib/useClient.ts load as their assembled pieces", async () => {
|
|
262
|
+
const { root, file, app } = await routeWorkspace();
|
|
263
|
+
|
|
264
|
+
const modules = await compile(app, root, [file("client.ts"), file("ui/Pick.tsx")]);
|
|
265
|
+
|
|
266
|
+
expect(sortedIds(modules)).toEqual([
|
|
267
|
+
"apps/demo/client.ts",
|
|
268
|
+
"apps/demo/lib/__client/_st.ts",
|
|
269
|
+
"apps/demo/lib/__client/_useClient.ts",
|
|
270
|
+
"apps/demo/lib/cnst.ts",
|
|
271
|
+
"apps/demo/lib/st.ts",
|
|
272
|
+
"apps/demo/lib/useClient.ts",
|
|
273
|
+
"apps/demo/ui/Pick.tsx",
|
|
274
|
+
]);
|
|
275
|
+
const code = modules.map((module) => module.factory).join("\n");
|
|
276
|
+
expect(code).not.toContain("whole-store");
|
|
277
|
+
expect(code).not.toContain("whole-client");
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("a store file picking an endpoint at run time takes its handle from the page runtime and loads no store, and each file naming its store loads the assembled runtime", async () => {
|
|
281
|
+
const { root, file, app } = await routeWorkspace();
|
|
282
|
+
for (const client of ["lib/useClient.ts", "lib/usePage.ts"])
|
|
283
|
+
await write(file(client), "export const fetch = {};\n");
|
|
284
|
+
const before = await DevRouteRuntime.read(root, "apps/demo");
|
|
285
|
+
await write(
|
|
286
|
+
file("lib/_draft/draft.store.ts"),
|
|
287
|
+
'import { store } from "akanjs/store";\nimport { fetch } from "../useClient";\nexport class DraftStore extends store("draft" as const, () => ({ draftText: 0 })) {\n run(name: string) {\n return (fetch as Record<string, () => unknown>)[name]?.();\n }\n}\n',
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const modules = await compile(app, root, [file("lib/_draft/draft.store.ts")]);
|
|
291
|
+
const runtime = await DevRouteRuntime.read(root, "apps/demo");
|
|
292
|
+
const [draft, tally] = await Promise.all(
|
|
293
|
+
["ui/Draft.tsx", "ui/Tally.tsx"].map(async (ui) => runtime?.transform(await Bun.file(file(ui)).text(), file(ui))),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
expect(sortedIds(modules)).toEqual(["apps/demo/lib/_draft/draft.store.ts", "apps/demo/lib/usePage.ts"]);
|
|
297
|
+
expect(draft?.perModule).toBe(false);
|
|
298
|
+
expect(draft?.contents).toContain('/apps/demo/lib/__client/_useClient.ts";');
|
|
299
|
+
expect(draft?.contents).toContain('/apps/demo/lib/__client/_st.ts";');
|
|
300
|
+
expect(tally?.perModule).toBe(true);
|
|
301
|
+
expect(runtime?.digest).not.toBe(before?.digest);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("a lib's lib/st.ts loads as the lib's assembled store followed by the app's", async () => {
|
|
305
|
+
const { root, file, app } = await routeWorkspace();
|
|
306
|
+
const lib = (relative: string) => path.join(root, "libs/shared", relative);
|
|
307
|
+
await write(file("lib/st.ts"), 'export { st as shared } from "@libs/shared/client";\nexport const st = {};\n');
|
|
308
|
+
await write(lib("lib/st.ts"), 'export const st = { mark: "whole-lib-store" };\n');
|
|
309
|
+
await write(lib("lib/__client/_st.ts"), 'export const st = { mark: "assembled-lib-store" };\n');
|
|
310
|
+
await write(lib("lib/_shelf/shelf.store.ts"), storeSource("shelf", ["shelfSize"]));
|
|
311
|
+
await write(lib("lib/__client/shelf.st.ts"), piece("shelf.st"));
|
|
312
|
+
|
|
313
|
+
const modules = await compile(app, root, [lib("lib/st.ts")]);
|
|
314
|
+
|
|
315
|
+
expect(sortedIds(modules)).toEqual([
|
|
316
|
+
"apps/demo/lib/__client/_st.ts",
|
|
317
|
+
"libs/shared/lib/__client/_st.ts",
|
|
318
|
+
"libs/shared/lib/st.ts",
|
|
319
|
+
]);
|
|
320
|
+
expect(modules.map((module) => module.factory).join("\n")).not.toContain("whole-lib-store");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test.each([
|
|
324
|
+
["the object first", "Memo, cnst"],
|
|
325
|
+
["the constants first", "cnst, Memo"],
|
|
326
|
+
])(
|
|
327
|
+
"a file naming one component of an object and one constant, %s, compiles that component's file and that model's registration, never every constant",
|
|
328
|
+
async (_, names) => {
|
|
329
|
+
const { root, file, app } = await routeWorkspace();
|
|
330
|
+
await writeMemoModel(file);
|
|
331
|
+
await write(
|
|
332
|
+
file("ui/Status.tsx"),
|
|
333
|
+
`"use client";\nimport { ${names} } from "@apps/demo/client";\nexport const Status = () => [Memo.Zone.View, cnst.MemoStatus];\n`,
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
const modules = await compile(app, root, [file("ui/Status.tsx")]);
|
|
337
|
+
|
|
338
|
+
expect(sortedIds(modules)).toEqual([
|
|
339
|
+
"apps/demo/lib/__client/memo.cnst.ts",
|
|
340
|
+
"apps/demo/lib/memo/Memo.Zone.tsx",
|
|
341
|
+
"apps/demo/lib/memo/memo.constant.ts",
|
|
342
|
+
"apps/demo/lib/usePage.ts",
|
|
343
|
+
"apps/demo/ui/Status.tsx",
|
|
344
|
+
]);
|
|
345
|
+
},
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
class RouteRegistry extends SsrDevBundler {
|
|
349
|
+
roots: string[] = [];
|
|
350
|
+
protected override async rootFiles(entryFiles: string[]) {
|
|
351
|
+
return [...entryFiles, ...this.roots];
|
|
352
|
+
}
|
|
353
|
+
override async metadataFingerprint() {
|
|
354
|
+
return "same";
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
test("a store save that gives a read name an owner recompiles the file reading it, whose own text did not change", async () => {
|
|
359
|
+
const { file, app } = await routeWorkspace();
|
|
360
|
+
const bundler = new RouteRegistry(app);
|
|
361
|
+
bundler.roots = [file("ui/Note.tsx")];
|
|
362
|
+
expect(await bundler.update([])).toMatchObject({ reload: true, first: true });
|
|
363
|
+
const patcher = new CsrDevPatcher(bundler, { resident: true });
|
|
364
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
365
|
+
const appJs = async () => await Bun.file(path.join(bundler.outDir, "app.js")).text();
|
|
366
|
+
expect(await appJs()).not.toContain('__akan.define("apps/demo/lib/__client/draft.st.ts"');
|
|
367
|
+
|
|
368
|
+
await write(file("lib/_draft/draft.store.ts"), storeSource("draft", ["draftText", "noteText"]));
|
|
369
|
+
const saved = await patcher.update([file("lib/_draft/draft.store.ts")]);
|
|
370
|
+
|
|
371
|
+
expect(saved.kind).toBe("update");
|
|
372
|
+
expect(saved.kind === "update" ? saved.update.changedIds : []).toContain("apps/demo/ui/Note.tsx");
|
|
373
|
+
expect(await appJs()).toContain('__akan.define("apps/demo/lib/__client/draft.st.ts"');
|
|
374
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("a constant file's save reloads the tab, whose boot runs that model's registration before its fetch piece and both before its store piece", async () => {
|
|
378
|
+
const { file, app } = await routeWorkspace();
|
|
379
|
+
await writeMemoModel(file);
|
|
380
|
+
await write(
|
|
381
|
+
file("ui/MemoText.tsx"),
|
|
382
|
+
'"use client";\nimport { st, cnst } from "@apps/demo/client";\nexport const MemoText = () => [st.use.memoText(), cnst.MemoStatus];\n',
|
|
383
|
+
);
|
|
384
|
+
const bundler = new RouteRegistry(app);
|
|
385
|
+
bundler.roots = [file("ui/MemoText.tsx")];
|
|
386
|
+
await bundler.update([]);
|
|
387
|
+
const patcher = new CsrDevPatcher(bundler, { resident: true });
|
|
388
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
389
|
+
|
|
390
|
+
await write(file("lib/memo/memo.constant.ts"), 'export const MemoStatus = ["active", "done"] as const;\n');
|
|
391
|
+
const saved = await patcher.update([file("lib/memo/memo.constant.ts")]);
|
|
392
|
+
|
|
393
|
+
const update = saved.kind === "update" ? saved.update : null;
|
|
394
|
+
expect(update).toMatchObject({ reload: true, reason: "memo.constant.ts changed" });
|
|
395
|
+
const appJs = await Bun.file(path.join(bundler.outDir, "app.js")).text();
|
|
396
|
+
const factoryOf = (id: string) =>
|
|
397
|
+
appJs.slice(appJs.indexOf(`__akan.define("${id}"`)).split("\n__akan.define(")[0] ?? "";
|
|
398
|
+
const required = (id: string) =>
|
|
399
|
+
[...factoryOf(id).matchAll(/require\("([^"]+)"\)/g)].map(([, dep]) => dep?.replace(/^.*\/lib\//, "lib/"));
|
|
400
|
+
expect(factoryOf("apps/demo/lib/__client/memo.cnst.ts")).toContain("lib/memo/memo.constant.ts");
|
|
401
|
+
expect(required("apps/demo/lib/__client/memo.fetch.ts")).toEqual(["lib/__client/memo.cnst.ts"]);
|
|
402
|
+
expect(required("apps/demo/lib/__client/memo.st.ts")).toEqual([
|
|
403
|
+
"lib/__client/memo.cnst.ts",
|
|
404
|
+
"lib/__client/memo.fetch.ts",
|
|
405
|
+
]);
|
|
406
|
+
expect(appJs).toContain('"done"');
|
|
407
|
+
expect(appJs).not.toContain('__akan.define("apps/demo/lib/cnst.ts"');
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test("a store save that adds a key patches the owners table, which takes its own update, and leaves the bootstrap as it was", async () => {
|
|
411
|
+
const { file, app } = await routeWorkspace();
|
|
412
|
+
const bundler = new RouteRegistry(app);
|
|
413
|
+
bundler.roots = [file("ui/Draft.tsx")];
|
|
414
|
+
await bundler.update([]);
|
|
415
|
+
const patcher = new CsrDevPatcher(bundler, { resident: true });
|
|
416
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
417
|
+
const ownersId = "apps/demo/.akan/generated/ssr-dev/storeOwners.ts";
|
|
418
|
+
|
|
419
|
+
await write(file("lib/_draft/draft.store.ts"), storeSource("draft", ["draftText", "draftNote"]));
|
|
420
|
+
const saved = await patcher.update([file("lib/_draft/draft.store.ts")]);
|
|
421
|
+
|
|
422
|
+
const update = saved.kind === "update" ? saved.update : null;
|
|
423
|
+
expect(update?.reload).toBe(false);
|
|
424
|
+
expect(update?.changedIds).toEqual([ownersId]);
|
|
425
|
+
const patch = await Bun.file(path.join(bundler.outDir, `patch-${update?.generation}.js`)).text();
|
|
426
|
+
const owners = patch.slice(patch.indexOf(`"${ownersId}": function`));
|
|
427
|
+
expect(owners).toContain('"draftNote"');
|
|
428
|
+
expect(owners).toContain("module.hot.accept()");
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("a lib store piece's save runs the app's piece of that module and the app's assembled store again after it", async () => {
|
|
432
|
+
const { root, file, app } = await routeWorkspace();
|
|
433
|
+
const libPiece = path.join(root, "libs/shared/lib/__client/draft.st.ts");
|
|
434
|
+
await write(libPiece, piece("shared-draft.st"));
|
|
435
|
+
await write(
|
|
436
|
+
file("lib/__client/draft.st.ts"),
|
|
437
|
+
`import "../../../../libs/shared/lib/__client/draft.st";\n${piece("draft.st")}`,
|
|
438
|
+
);
|
|
439
|
+
const bundler = new RouteRegistry(app);
|
|
440
|
+
bundler.roots = [file("ui/Draft.tsx"), file("ui/Tally.tsx"), file("client.ts")];
|
|
441
|
+
await bundler.update([]);
|
|
442
|
+
const patcher = new CsrDevPatcher(bundler, { resident: true });
|
|
443
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
444
|
+
|
|
445
|
+
await write(libPiece, piece("shared-draft.st-saved"));
|
|
446
|
+
const saved = await patcher.update([libPiece]);
|
|
447
|
+
|
|
448
|
+
const update = saved.kind === "update" ? saved.update : null;
|
|
449
|
+
expect(update?.reload).toBe(false);
|
|
450
|
+
expect(update?.changedIds.toSorted()).toEqual([
|
|
451
|
+
"apps/demo/lib/__client/_st.ts",
|
|
452
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
453
|
+
"libs/shared/lib/__client/draft.st.ts",
|
|
454
|
+
]);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("a lib store class save runs the app's store extending it, the app's piece of that module and the app's assembled store again after it", async () => {
|
|
458
|
+
const { root, file, app } = await routeWorkspace();
|
|
459
|
+
const lib = (relative: string) => path.join(root, "libs/shared", relative);
|
|
460
|
+
await write(lib("lib/_draft/draft.store.ts"), storeSource("draft", ["draftText"]));
|
|
461
|
+
await write(lib("lib/__client/draft.st.ts"), `import "../_draft/draft.store";\n${piece("shared-draft.st")}`);
|
|
462
|
+
await write(
|
|
463
|
+
file("lib/__lib/lib.store.ts"),
|
|
464
|
+
'import "../../../../libs/shared/lib/_draft/draft.store";\nexport {};\n',
|
|
465
|
+
);
|
|
466
|
+
await write(
|
|
467
|
+
file("lib/_draft/draft.store.ts"),
|
|
468
|
+
`import "../__lib/lib.store";\n${storeSource("draft", ["draftText"])}`,
|
|
469
|
+
);
|
|
470
|
+
await write(
|
|
471
|
+
file("lib/__client/draft.st.ts"),
|
|
472
|
+
`import "../../../../libs/shared/lib/__client/draft.st";\nimport "../_draft/draft.store";\n${piece("draft.st")}`,
|
|
473
|
+
);
|
|
474
|
+
const bundler = new RouteRegistry(app);
|
|
475
|
+
bundler.roots = [file("ui/Draft.tsx"), file("ui/Tally.tsx"), file("client.ts")];
|
|
476
|
+
await bundler.update([]);
|
|
477
|
+
const patcher = new CsrDevPatcher(bundler, { resident: true });
|
|
478
|
+
expect(await patcher.update([])).toEqual({ kind: "unchanged" });
|
|
479
|
+
|
|
480
|
+
await write(lib("lib/_draft/draft.store.ts"), storeSource("draft", ["draftText", "draftNote"]));
|
|
481
|
+
const saved = await patcher.update([lib("lib/_draft/draft.store.ts")]);
|
|
482
|
+
|
|
483
|
+
const update = saved.kind === "update" ? saved.update : null;
|
|
484
|
+
expect(update?.reload).toBe(false);
|
|
485
|
+
expect(update?.changedIds.toSorted()).toEqual([
|
|
486
|
+
"apps/demo/lib/__client/_st.ts",
|
|
487
|
+
"apps/demo/lib/__client/draft.st.ts",
|
|
488
|
+
"apps/demo/lib/_draft/draft.store.ts",
|
|
489
|
+
"libs/shared/lib/_draft/draft.store.ts",
|
|
490
|
+
]);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { RuntimeFamily } from "../quality/runtimeNameTable";
|
|
4
|
+
import { RouteRuntime } from "../transforms/routeRuntime";
|
|
5
|
+
import { CsrDevPaths } from "./csrDevPaths";
|
|
6
|
+
|
|
7
|
+
export interface DevRouteTransform {
|
|
8
|
+
contents: string;
|
|
9
|
+
perModule: boolean;
|
|
10
|
+
stamp: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type StoreOwners = Record<"use" | "do" | "slice", Record<string, string[]>>;
|
|
14
|
+
|
|
15
|
+
const storeFamilies = { use: "st.use", do: "st.do", slice: "st.slice" } as const satisfies Record<
|
|
16
|
+
keyof StoreOwners,
|
|
17
|
+
RuntimeFamily
|
|
18
|
+
>;
|
|
19
|
+
const roles = ["signal", "store", "constant"] as const;
|
|
20
|
+
const workspaceImport = /(\b(?:from|import)\s*)"(@(?:apps|libs)\/[^/"]+\/lib\/(?:usePage|__client\/[\w.-]+))"/g;
|
|
21
|
+
|
|
22
|
+
//* The production `RouteRuntime` for the dev registry: the same verdicts, with each piece it adds written as a
|
|
23
|
+
//* relative path, which resolves against the disk with no resolution build, so a save naming a new module stays a patch.
|
|
24
|
+
export class DevRouteRuntime {
|
|
25
|
+
//? Read by `StoreFacade` to name the module behind a key the page has not loaded; only a dev bootstrap sets it.
|
|
26
|
+
static readonly ownersKey = "akanjs.store.owners";
|
|
27
|
+
//? Not in the bootstrap: a store save changes the table, and a change reaching the bootstrap reloads the tab.
|
|
28
|
+
static readonly ownersModule = "storeOwners";
|
|
29
|
+
static readonly #libStoreRoot = /^libs\/[^/]+\/lib\/(?:st|__client\/(_st|[^/]+\.st))\.ts$/;
|
|
30
|
+
static readonly #libStoreClass = /^libs\/[^/]+\/lib\/(_?([^/]+))\/\2\.store\.ts$/;
|
|
31
|
+
readonly #workspaceRoot: string;
|
|
32
|
+
readonly #runtime: RouteRuntime;
|
|
33
|
+
readonly scopes: string[];
|
|
34
|
+
readonly digest: string;
|
|
35
|
+
|
|
36
|
+
static async read(workspaceRoot: string, appScope: string): Promise<DevRouteRuntime | null> {
|
|
37
|
+
const runtime = await RouteRuntime.read(workspaceRoot, appScope);
|
|
38
|
+
if (!runtime) return null;
|
|
39
|
+
const scopes = runtime.table.layers.map(({ scope }) => scope);
|
|
40
|
+
const pieces = scopes.map((scope) => DevRouteRuntime.#namesIn(path.join(workspaceRoot, scope, "lib/__client")));
|
|
41
|
+
const digest = Bun.hash(JSON.stringify([runtime.table.layers, pieces, runtime.storeNeeds])).toString(36);
|
|
42
|
+
return new DevRouteRuntime(workspaceRoot, runtime, scopes, digest);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//? Every file the table and the piece list are read from, by mtime: checked on each save, re-read only once it moves.
|
|
46
|
+
static inputsOf(workspaceRoot: string, scopes: readonly string[]): string {
|
|
47
|
+
const mtimeOf = (file: string) => fs.statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? -1;
|
|
48
|
+
const stamped = scopes.map((scope) => {
|
|
49
|
+
const lib = path.join(workspaceRoot, scope, "lib");
|
|
50
|
+
const dirs = DevRouteRuntime.#dirsIn(lib).map((dir) => [
|
|
51
|
+
dir,
|
|
52
|
+
...roles.map((role) => mtimeOf(path.join(lib, dir, `${dir.replace(/^_/, "")}.${role}.ts`))),
|
|
53
|
+
]);
|
|
54
|
+
const scalars = DevRouteRuntime.#dirsIn(path.join(lib, "__scalar")).map((dir) => [
|
|
55
|
+
dir,
|
|
56
|
+
mtimeOf(path.join(lib, "__scalar", dir, `${dir}.constant.ts`)),
|
|
57
|
+
]);
|
|
58
|
+
const pieces = DevRouteRuntime.#namesIn(path.join(lib, "__client"));
|
|
59
|
+
return [scope, mtimeOf(path.join(lib, "st.ts")), pieces, dirs, scalars];
|
|
60
|
+
});
|
|
61
|
+
return Bun.hash(JSON.stringify(stamped)).toString(36);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
constructor(workspaceRoot: string, runtime: RouteRuntime, scopes: string[], digest: string) {
|
|
65
|
+
this.#workspaceRoot = workspaceRoot;
|
|
66
|
+
this.#runtime = runtime;
|
|
67
|
+
this.scopes = scopes;
|
|
68
|
+
this.digest = digest;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get wholeSource(): string {
|
|
72
|
+
return this.#runtime.wholeSource;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
redirectOf(absPath: string): string | null {
|
|
76
|
+
return this.#runtime.redirectOf(absPath);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
transform(source: string, absPath: string): DevRouteTransform | null {
|
|
80
|
+
const routed = this.#runtime.transform(source, absPath);
|
|
81
|
+
if (!routed) return null;
|
|
82
|
+
const from = path.dirname(CsrDevPaths.realpath(absPath));
|
|
83
|
+
const contents =
|
|
84
|
+
routed.source === source
|
|
85
|
+
? source
|
|
86
|
+
: routed.source.replace(workspaceImport, (written, lead: string, specifier: string) => {
|
|
87
|
+
const file = CsrDevPaths.resolveOnDisk(path.join(this.#workspaceRoot, specifier.slice(1)));
|
|
88
|
+
return file ? `${lead}${JSON.stringify(DevRouteRuntime.#relative(from, file))}` : written;
|
|
89
|
+
});
|
|
90
|
+
const stamp = Bun.hash(`${routed.perModule}\n${contents}`).toString(36);
|
|
91
|
+
return { contents, perModule: routed.perModule, stamp };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//? A lib store run again takes its registry entry and actions back from the app's store extending it.
|
|
95
|
+
appStoresAfter(id: string, depsOf: (id: string) => readonly string[] | undefined): string[] {
|
|
96
|
+
const appLib = `${this.#runtime.table.scope}/lib`;
|
|
97
|
+
const [storeClass, dir = "", model = ""] = id.match(DevRouteRuntime.#libStoreClass) ?? [];
|
|
98
|
+
const [libRoot, piece = "_st"] = storeClass ? [id, `${model}.st`] : (id.match(DevRouteRuntime.#libStoreRoot) ?? []);
|
|
99
|
+
if (!libRoot) return [];
|
|
100
|
+
const after = [`${appLib}/__client/${piece}.ts`, `${appLib}/__client/_st.ts`];
|
|
101
|
+
const appStore = `${appLib}/${dir}/${model}.store.ts`;
|
|
102
|
+
if (storeClass && DevRouteRuntime.#imports(depsOf, appStore, id)) after.unshift(appStore);
|
|
103
|
+
return [...new Set(after)].filter((file) => !!depsOf(file));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get owners(): StoreOwners {
|
|
107
|
+
const { table } = this.#runtime;
|
|
108
|
+
const ownersOf = (family: RuntimeFamily) => {
|
|
109
|
+
const byModule: Record<string, string[]> = {};
|
|
110
|
+
for (const key of table.keysOf(family))
|
|
111
|
+
for (const { scope, module } of table.lookup(family, key)?.owners ?? []) {
|
|
112
|
+
const label = `${module} (${scope})`;
|
|
113
|
+
byModule[label] = [...(byModule[label] ?? []), key];
|
|
114
|
+
}
|
|
115
|
+
return byModule;
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
use: ownersOf(storeFamilies.use),
|
|
119
|
+
do: ownersOf(storeFamilies.do),
|
|
120
|
+
slice: ownersOf(storeFamilies.slice),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
ownersSource(): string {
|
|
125
|
+
return `(globalThis as Record<symbol, unknown>)[Symbol.for(${JSON.stringify(DevRouteRuntime.ownersKey)})] = ${JSON.stringify(this.owners)};\n`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static #relative(from: string, file: string): string {
|
|
129
|
+
const relative = path.relative(from, CsrDevPaths.realpath(file));
|
|
130
|
+
if (path.isAbsolute(relative)) return relative.split(path.sep).join("/");
|
|
131
|
+
const posix = relative.split(path.sep).join("/");
|
|
132
|
+
return posix.startsWith("../") ? posix : `./${posix}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static #imports(depsOf: (id: string) => readonly string[] | undefined, from: string, target: string): boolean {
|
|
136
|
+
const seen = new Set([from]);
|
|
137
|
+
const queue = [from];
|
|
138
|
+
for (const id of queue)
|
|
139
|
+
for (const dep of depsOf(id) ?? []) {
|
|
140
|
+
if (dep === target) return true;
|
|
141
|
+
if (seen.has(dep)) continue;
|
|
142
|
+
seen.add(dep);
|
|
143
|
+
queue.push(dep);
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static #dirsIn(dir: string): string[] {
|
|
149
|
+
try {
|
|
150
|
+
return fs
|
|
151
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
152
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("__"))
|
|
153
|
+
.map((entry) => entry.name)
|
|
154
|
+
.sort();
|
|
155
|
+
} catch {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static #namesIn(dir: string): string[] {
|
|
161
|
+
try {
|
|
162
|
+
return fs.readdirSync(dir).sort();
|
|
163
|
+
} catch {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|