@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.
Files changed (278) hide show
  1. package/DEV_RUNTIME_KNOBS.md +1 -1
  2. package/README.ko.md +8 -2
  3. package/README.md +8 -3
  4. package/akanApp/akanApp.host.test.ts +4 -2
  5. package/akanApp/akanApp.host.ts +11 -0
  6. package/akanConfig/akanBin.ts +4 -0
  7. package/akanConfig/akanConfig.test.ts +280 -19
  8. package/akanConfig/akanConfig.ts +96 -270
  9. package/akanConfig/akanWorkers.test.ts +73 -0
  10. package/akanConfig/akanWorkers.ts +61 -0
  11. package/akanConfig/desktopServerConfig.ts +96 -0
  12. package/akanConfig/dockerImage.ts +216 -0
  13. package/akanConfig/index.ts +2 -0
  14. package/akanConfig/nativeFileSettings.ts +33 -0
  15. package/akanConfig/nativeKeys.ts +52 -0
  16. package/akanConfig/types.ts +2 -0
  17. package/app/appBin.test.ts +63 -0
  18. package/app/appBin.ts +50 -0
  19. package/app/applicationBuildReporter.test.ts +51 -1
  20. package/app/applicationBuildReporter.ts +6 -1
  21. package/app/applicationBuildRunner.test.ts +358 -5
  22. package/app/applicationBuildRunner.ts +204 -30
  23. package/app/dictionarySizes.test.ts +40 -0
  24. package/app/dictionarySizes.ts +95 -0
  25. package/app/routeFilesReport.test.ts +59 -0
  26. package/app/routeFilesReport.ts +46 -0
  27. package/app/routeJsBudget.ts +18 -4
  28. package/artifact/implicitRootLayout.test.ts +19 -0
  29. package/artifact/implicitRootLayout.ts +16 -2
  30. package/artifact/implicitRootLayoutRender.test.tsx +4 -1
  31. package/artifact/storeBootstrap.test.ts +28 -0
  32. package/artifact/storeBootstrap.ts +64 -8
  33. package/biome.base.json +7 -5
  34. package/codeAgent/agent/CodeAgent.ts +36 -16
  35. package/codeAgent/agent/CodeAgentAllowed.ts +35 -0
  36. package/codeAgent/agent/CodeAgentCommandLine.ts +372 -0
  37. package/codeAgent/agent/CodeAgentGate.ts +23 -4
  38. package/codeAgent/agent/CodeAgentRpcHost.ts +1 -1
  39. package/codeAgent/agent/codeAgent.test.ts +62 -0
  40. package/codeAgent/agent/codeAgentCommandLine.test.ts +212 -0
  41. package/codeAgent/agent/codeAgentContract.test.ts +117 -5
  42. package/codeAgent/skills/akan-agent/SKILL.md +8 -1
  43. package/codeAgent/skills/akan-code/SKILL.md +38 -3
  44. package/codeAgent/skills/akan-debug/SKILL.md +1 -1
  45. package/codeAgent/skills/akan-deps/SKILL.md +3 -2
  46. package/codeAgent/skills/akan-err/SKILL.md +13 -8
  47. package/codeAgent/skills/akan-mcp/SKILL.md +1 -1
  48. package/codeAgent/skills/akan-module/SKILL.md +2 -2
  49. package/codeAgent/skills/akan-page/SKILL.md +52 -28
  50. package/codeAgent/skills/akan-query/SKILL.md +13 -20
  51. package/codeAgent/skills/akan-runtime/SKILL.md +21 -1
  52. package/codeAgent/skills/akan-service/SKILL.md +10 -11
  53. package/codeAgent/skills/akan-transport/SKILL.md +5 -14
  54. package/codeAgent/skills/akan-ui/SKILL.md +16 -21
  55. package/commandDecorators/command.ts +28 -12
  56. package/commandDecorators/commandBuilder.ts +8 -1
  57. package/commandDecorators/commandDecorators.test.ts +70 -3
  58. package/commandDecorators/helpFormatter.ts +14 -12
  59. package/context/akanContext.ts +2 -0
  60. package/csrE2e/devHmrLatency.e2e.test.ts +1 -1
  61. package/docs/apiReference.test.ts +19 -0
  62. package/docs/apiReference.ts +28 -5
  63. package/executors/appExecutor.ts +26 -12
  64. package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
  65. package/executors/clientRuntimeKeys.fixture.json +393 -0
  66. package/executors/clientRuntimeKeys.test.ts +1690 -0
  67. package/executors/executor.ts +14 -6
  68. package/executors/executors.test.ts +140 -1
  69. package/executors/sysExecutor.ts +112 -8
  70. package/executors/workspaceExecutor.ts +7 -4
  71. package/frontendBuild/allRoutesBuilder.ts +99 -7
  72. package/frontendBuild/autoImportSync.ts +3 -2
  73. package/frontendBuild/buildRouteClient.test.ts +686 -13
  74. package/frontendBuild/clientBuildTypes.ts +26 -0
  75. package/frontendBuild/clientEntriesBundler.ts +49 -1
  76. package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
  77. package/frontendBuild/clientEntryDiscovery.ts +136 -14
  78. package/frontendBuild/clientEntryGroups.test.ts +77 -0
  79. package/frontendBuild/clientEntryGroups.ts +61 -0
  80. package/frontendBuild/csrDevArtifactWriter.ts +2 -2
  81. package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
  82. package/frontendBuild/csrDevModuleCompiler.ts +44 -8
  83. package/frontendBuild/csrDevPatcher.test.ts +1 -1
  84. package/frontendBuild/csrDevPatcher.ts +33 -7
  85. package/frontendBuild/csrDevTypes.ts +9 -0
  86. package/frontendBuild/devBarrelImports.test.ts +154 -0
  87. package/frontendBuild/devBarrelImports.ts +83 -23
  88. package/frontendBuild/devRegistryBundler.ts +4 -2
  89. package/frontendBuild/devRouteRuntime.test.ts +492 -0
  90. package/frontendBuild/devRouteRuntime.ts +167 -0
  91. package/frontendBuild/dictionaryAssetBuilder.test.ts +408 -0
  92. package/frontendBuild/dictionaryAssetBuilder.ts +185 -17
  93. package/frontendBuild/dictionaryConstants.ts +136 -0
  94. package/frontendBuild/dictionaryOwnParts.ts +88 -0
  95. package/frontendBuild/dictionaryPackState.ts +131 -0
  96. package/frontendBuild/dictionaryPacking.test.ts +207 -0
  97. package/frontendBuild/dictionaryPacking.ts +160 -0
  98. package/frontendBuild/dictionaryRoutes.test.ts +251 -0
  99. package/frontendBuild/dictionaryRoutes.ts +227 -0
  100. package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
  101. package/frontendBuild/pagesBundleBuilder.ts +78 -2
  102. package/frontendBuild/precompressArtifacts.test.ts +22 -0
  103. package/frontendBuild/precompressArtifacts.ts +34 -8
  104. package/frontendBuild/registryMetadataFingerprint.ts +3 -3
  105. package/frontendBuild/renderedEntries.ts +144 -0
  106. package/frontendBuild/routeClientBuilder.ts +407 -43
  107. package/frontendBuild/routeHandoffs.test.ts +155 -0
  108. package/frontendBuild/routeHandoffs.ts +117 -0
  109. package/frontendBuild/routeModulePreloads.test.ts +32 -0
  110. package/frontendBuild/routeModulePreloads.ts +20 -11
  111. package/frontendBuild/routeRuntimeCheck.test.ts +280 -0
  112. package/frontendBuild/routeRuntimeCheck.ts +248 -0
  113. package/frontendBuild/routeRuntimePays.test.ts +143 -0
  114. package/frontendBuild/routeRuntimePays.ts +117 -0
  115. package/frontendBuild/ssrBaseArtifactBuilder.ts +24 -34
  116. package/frontendBuild/ssrDevBundler.test.ts +150 -1
  117. package/frontendBuild/ssrDevBundler.ts +66 -11
  118. package/frontendBuild/vendorFileImports.test.ts +254 -0
  119. package/frontendBuild/vendorFileImports.ts +163 -0
  120. package/frontendBuild/vendorNarrowing.ts +28 -0
  121. package/frontendBuild/vendorSpecifiers.ts +2 -0
  122. package/incrementalBuilder/buildBatch.proc.ts +9 -9
  123. package/incrementalBuilder/buildBatchProtocol.ts +1 -1
  124. package/incrementalBuilder/builderBoot.ts +5 -5
  125. package/incrementalBuilder/builderGraphPayloads.ts +4 -4
  126. package/incrementalBuilder/builderLanes.ts +2 -2
  127. package/incrementalBuilder/builderWatch.ts +8 -8
  128. package/incrementalBuilder/csrRegistry.ts +4 -4
  129. package/incrementalBuilder/incrementalBuilder.proc.test.ts +79 -0
  130. package/incrementalBuilder/incrementalBuilder.proc.ts +27 -8
  131. package/incrementalBuilder/ssrRegistry.ts +6 -6
  132. package/index.ts +1 -1
  133. package/integration/renderAdmissionSoak.test.ts +99 -0
  134. package/integration/renderAdmissionSoak.ts +609 -0
  135. package/lint/__fixtures__/no-arbitrary-color/every-color-function/bad.tsx +11 -0
  136. package/lint/__fixtures__/no-arbitrary-color/every-color-function/good.tsx +2 -0
  137. package/lint/__fixtures__/no-daisyui-legacy-class/every-class/bad.tsx +34 -0
  138. package/lint/__fixtures__/no-daisyui-legacy-class/every-class/good.tsx +2 -0
  139. package/lint/__fixtures__/no-inline-color/every-color-function/bad.tsx +10 -0
  140. package/lint/__fixtures__/no-inline-color/every-color-function/good.tsx +2 -0
  141. package/lint/__fixtures__/no-raw-palette-class/every-palette/bad.tsx +22 -0
  142. package/lint/__fixtures__/no-raw-palette-class/every-palette/good.tsx +2 -0
  143. package/lint/biomeBase.test.ts +50 -3
  144. package/lint/biomeBase.ts +1 -1
  145. package/lint/biomeCheck.test.ts +162 -0
  146. package/lint/biomeCheck.ts +131 -0
  147. package/lint/linter.test.ts +16 -11
  148. package/lint/no-arbitrary-color.grit +5 -0
  149. package/lint/no-conditional-st-hook.grit +5 -1
  150. package/lint/no-daisyui-legacy-class.grit +8 -0
  151. package/lint/no-document-cookie.grit +3 -0
  152. package/lint/no-inline-color.grit +10 -2
  153. package/lint/no-oversized-list-limit.grit +2 -0
  154. package/lint/no-raw-palette-class.grit +7 -0
  155. package/lint/no-unpublished-form-setter.grit +2 -0
  156. package/lint/no-web-only-api-outside-webkit.grit +5 -0
  157. package/lint/no-web-storage.grit +3 -0
  158. package/lint/withheldEnvFile.ts +17 -0
  159. package/mobile/desktopBin.ts +2 -2
  160. package/mobile/desktopServerStage.test.ts +18 -3
  161. package/mobile/desktopServerStage.ts +7 -2
  162. package/mobile/nativeApp.test.ts +7 -5
  163. package/mobile/nativeApp.ts +2 -2
  164. package/mobile/nativeConfig.test.ts +96 -1
  165. package/mobile/nativeConfig.ts +46 -12
  166. package/mobile/nativePublicEnv.ts +1 -1
  167. package/mobile/serverBundleSecrets.ts +43 -0
  168. package/package.json +20 -171
  169. package/packageExports.test.ts +38 -24
  170. package/quality/dictionaryKeys.test.ts +356 -0
  171. package/quality/dictionaryKeys.ts +488 -0
  172. package/quality/objectImportReport.test.ts +36 -0
  173. package/quality/objectImportReport.ts +105 -0
  174. package/quality/qualityScanner.ts +1 -1
  175. package/quality/routeRuntimeReport.test.ts +91 -0
  176. package/quality/routeRuntimeReport.ts +121 -0
  177. package/quality/runtimeConstantFacts.ts +71 -0
  178. package/quality/runtimeConstantGraph.ts +123 -0
  179. package/quality/runtimeConstantReads.ts +116 -0
  180. package/quality/runtimeImports.test.ts +190 -0
  181. package/quality/runtimeImports.ts +233 -0
  182. package/quality/runtimeKeyNames.ts +46 -0
  183. package/quality/runtimeLexicalScope.ts +132 -0
  184. package/quality/runtimeMarkerReach.test.ts +84 -0
  185. package/quality/runtimeMarkerReach.ts +255 -0
  186. package/quality/runtimeModuleFacts.ts +290 -0
  187. package/quality/runtimeNameTable.test.ts +172 -0
  188. package/quality/runtimeNameTable.ts +477 -0
  189. package/quality/runtimeScopeReport.test.ts +123 -0
  190. package/quality/runtimeScopeReport.ts +167 -0
  191. package/quality/runtimeSliceKey.ts +269 -0
  192. package/quality/runtimeSyntax.ts +133 -0
  193. package/quality/runtimeTable.fixture.ts +189 -0
  194. package/quality/runtimeUse.test.ts +737 -0
  195. package/quality/runtimeUse.ts +696 -0
  196. package/quality/runtimeUseReadable.test.ts +393 -0
  197. package/quality/ssrScanner.test.ts +13 -0
  198. package/quality/ssrScanner.ts +1 -1
  199. package/quality/translatorBindings.ts +187 -0
  200. package/test/secretEnvFixture.ts +134 -0
  201. package/test/testHelpers.ts +11 -0
  202. package/transforms/barrelAnalyzer.ts +26 -4
  203. package/transforms/barrelImportsPlugin.ts +121 -32
  204. package/transforms/exportReach.test.ts +162 -0
  205. package/transforms/exportReach.ts +308 -0
  206. package/transforms/frameworkObjects.fixture.tsx +58 -0
  207. package/transforms/frameworkObjects.test.ts +54 -0
  208. package/transforms/modelReference.test.ts +196 -0
  209. package/transforms/modelReference.ts +39 -0
  210. package/transforms/moduleParts.test.ts +225 -0
  211. package/transforms/moduleParts.ts +418 -0
  212. package/transforms/objectImportEdits.ts +150 -0
  213. package/transforms/objectMemberImports.test.ts +633 -0
  214. package/transforms/objectMemberImports.ts +571 -0
  215. package/transforms/objectMemberLookup.ts +150 -0
  216. package/transforms/objectModule.test.ts +307 -0
  217. package/transforms/objectModule.ts +482 -0
  218. package/transforms/objectModuleAnalyzer.ts +104 -0
  219. package/transforms/partImports.test.ts +76 -0
  220. package/transforms/partImports.ts +110 -0
  221. package/transforms/partModules.test.ts +187 -0
  222. package/transforms/partModules.ts +90 -0
  223. package/transforms/partPurity.ts +124 -0
  224. package/transforms/reactIconImports.test.ts +231 -0
  225. package/transforms/reactIconImports.ts +325 -0
  226. package/transforms/routeConstants.ts +122 -0
  227. package/transforms/routeRuntime.test.ts +835 -0
  228. package/transforms/routeRuntime.ts +703 -0
  229. package/transforms/routeRuntimeEdits.ts +92 -0
  230. package/transforms/routeRuntimeGraph.ts +66 -0
  231. package/transforms/routeRuntimePlan.ts +244 -0
  232. package/transforms/secretEnvDeclaration.test.ts +208 -0
  233. package/transforms/secretEnvDeclaration.ts +213 -0
  234. package/transforms/secretEnvFile.test.ts +117 -0
  235. package/transforms/secretEnvFile.ts +113 -0
  236. package/transforms/secretEnvName.test.ts +54 -0
  237. package/transforms/secretEnvName.ts +29 -0
  238. package/transforms/secretEnvSource.test.ts +148 -0
  239. package/transforms/secretEnvSource.ts +166 -0
  240. package/transforms/serverEnvEvaluationError.ts +11 -0
  241. package/transforms/serverEnvPlugin.test.ts +2 -1
  242. package/transforms/serverEnvPlugin.ts +13 -0
  243. package/transforms/serverEnvProfile.test.ts +129 -2
  244. package/transforms/serverEnvProfile.ts +126 -40
  245. package/transforms/serverEnvSecrets.test.ts +292 -0
  246. package/transforms/signalDescriptors.test.ts +280 -0
  247. package/transforms/signalDescriptors.ts +227 -0
  248. package/transforms/thisFreeExports.ts +87 -0
  249. package/transforms/transforms.test.ts +180 -3
  250. package/transforms/tsconfigPackageResolver.test.ts +1 -0
  251. package/tunnel/createTunnel.test.ts +29 -8
  252. package/tunnel/createTunnel.ts +22 -11
  253. package/tunnel/ssh2Module.test.ts +37 -0
  254. package/tunnel/ssh2Module.ts +32 -0
  255. package/tunnel/sshTunnel.ts +4 -2
  256. package/typecheck/projectTypecheck.test.ts +32 -1
  257. package/typecheck/projectTypecheck.ts +3 -3
  258. package/typecheck/typeChecker.test.ts +49 -0
  259. package/typecheck/typeChecker.ts +9 -7
  260. package/typecheck/withheldDiagnostics.ts +41 -0
  261. package/util/fileEditor.ts +1 -0
  262. package/util/selectModel.ts +1 -0
  263. package/workflow/executor.ts +6 -2
  264. package/workflow/index.ts +1 -0
  265. package/workflow/plan.ts +36 -7
  266. package/workflow/types.ts +1 -1
  267. package/workflow/uiTarget.ts +50 -0
  268. package/workspace/envSeed.test.ts +30 -1
  269. package/workspace/envSeed.ts +30 -1
  270. package/workspace/getRelatedCnsts.ts +7 -0
  271. package/workspace/mainCheckout.test.ts +55 -0
  272. package/workspace/mainCheckout.ts +38 -0
  273. package/workspace/scanInfo.test.ts +39 -0
  274. package/workspace/scanInfo.ts +55 -6
  275. package/workspace/slicePlanner.ts +1 -1
  276. package/workspace/workspaceLayout.test.ts +5 -1
  277. package/workspace/workspaceLayout.ts +3 -0
  278. package/CHANGELOG.md +0 -2717
@@ -77,6 +77,32 @@ export interface BundleClientEntriesResult {
77
77
  externalsByChunkUrl: Map<string, string[]>;
78
78
  /** Workspace-relative client reference id per absolute source entry. */
79
79
  clientReferenceIdByAbsPath: Map<string, string>;
80
+ graph: ClientBundleGraph;
81
+ }
82
+
83
+ /** What one bundle linked, by absolute path: no import the bundle left external. */
84
+ export interface ClientBundleGraph {
85
+ importsByModule: Map<string, ClientModuleImport[]>;
86
+ chunksByModule: Map<string, string[]>;
87
+ /** Static imports only: a chunk loaded by `import()` runs after the chunk asking for it. */
88
+ importsByChunk: Map<string, string[]>;
89
+ /** What the bundler placed on its own: a file taken apart by export is several, held together in the maps above. */
90
+ linked?: ClientLinkedInput[];
91
+ }
92
+
93
+ /** By client entry, the export names asked of it; null for all of them. */
94
+ export type AskedExports = ReadonlyMap<string, readonly string[] | null>;
95
+
96
+ export interface ClientLinkedInput {
97
+ module: string;
98
+ imports: ClientModuleImport[];
99
+ chunks: string[];
100
+ }
101
+
102
+ export interface ClientModuleImport {
103
+ path: string;
104
+ /** Asked for by `import()`, so it loads when that call runs. */
105
+ lazy: boolean;
80
106
  }
81
107
 
82
108
  export type AkanConfig = Awaited<ReturnType<App["getConfig"]>>;
@@ -2,14 +2,18 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import type { SsrManifest, SsrManifestEntry } from "akanjs/server";
4
4
  import type { BunPlugin } from "bun";
5
+ import { ModuleParts } from "../transforms/moduleParts";
5
6
  import { scanUseClientExports, toClientReferencePath } from "../transforms/rscUseClientTransform";
6
7
  import { bundleConditions, bundleDefine } from "./bundleDefine";
7
8
  import {
8
9
  type BundleClientEntriesInternalOptions,
9
10
  type BundleClientEntriesResult,
10
11
  CLIENT_BUNDLE_NAMING,
12
+ type ClientBundleGraph,
11
13
  type ClientBundleTarget,
14
+ type ClientLinkedInput,
12
15
  type ClientManifest,
16
+ type ClientModuleImport,
13
17
  type MetafileInput,
14
18
  type MetafileOutput,
15
19
  type OpaqueEntryAliases,
@@ -102,9 +106,51 @@ export class ClientEntriesBundler {
102
106
  entryChunkUrlsByAbsPath: this.#entryChunkUrlsByAbsPath,
103
107
  externalsByChunkUrl: this.#collectExternalsByChunkUrl(),
104
108
  clientReferenceIdByAbsPath: this.#clientReferenceIdByAbsPath,
109
+ graph: this.#graph(),
105
110
  };
106
111
  }
107
112
 
113
+ #graph(): ClientBundleGraph {
114
+ const absByInput = new Map<string, string>();
115
+ const absOf = (input: string) => {
116
+ const known = absByInput.get(input);
117
+ if (known) return known;
118
+ const abs = this.#absFromEntryPoint(input);
119
+ absByInput.set(input, abs);
120
+ return abs;
121
+ };
122
+ const importsByModule = new Map<string, ClientModuleImport[]>();
123
+ const linkedByInput = new Map<string, ClientLinkedInput>();
124
+ for (const [input, { imports = [] }] of Object.entries(this.#metafileInputs)) {
125
+ const module = absOf(input);
126
+ const own = imports
127
+ .filter((imported) => !imported.external)
128
+ .map((imported) => ({ path: absOf(imported.path), lazy: imported.kind === "dynamic-import" }))
129
+ .filter((imported) => imported.path !== module);
130
+ linkedByInput.set(input, { module, imports: own, chunks: [] });
131
+ const held = importsByModule.getOrInsert(module, []);
132
+ for (const imported of own)
133
+ if (!held.some((known) => known.path === imported.path && known.lazy === imported.lazy)) held.push(imported);
134
+ }
135
+ const chunksByModule = new Map<string, string[]>();
136
+ const importsByChunk = new Map<string, string[]>();
137
+ for (const [chunk, info] of this.#metafileOutputsByAbs) {
138
+ if (!chunk.endsWith(".js")) continue;
139
+ for (const input of Object.keys(info.inputs ?? {})) {
140
+ const chunks = chunksByModule.getOrInsert(absOf(input), []);
141
+ if (!chunks.includes(chunk)) chunks.push(chunk);
142
+ linkedByInput.get(input)?.chunks.push(chunk);
143
+ }
144
+ importsByChunk.set(
145
+ chunk,
146
+ info.imports
147
+ .filter((imported) => imported.kind !== "dynamic-import" && imported.path.endsWith(".js"))
148
+ .map((imported) => this.#absFromOutdir(imported.path)),
149
+ );
150
+ }
151
+ return { importsByModule, chunksByModule, importsByChunk, linked: [...linkedByInput.values()] };
152
+ }
153
+
108
154
  async #createOpaqueEntryAliases(): Promise<OpaqueEntryAliases> {
109
155
  const aliasDir = path.join(this.#app.cwdPath, ".akan", "generated", "client-entry-alias", this.#outputSubdir);
110
156
  fs.mkdirSync(aliasDir, { recursive: true });
@@ -233,7 +279,9 @@ export class ClientEntriesBundler {
233
279
  return path.isAbsolute(p) ? p : path.resolve(this.#outdir, p);
234
280
  }
235
281
 
236
- #absFromEntryPoint(p: string): string {
282
+ //? A file taken apart by export is linked as several inputs, each named by the file's path and its part.
283
+ #absFromEntryPoint(input: string): string {
284
+ const p = input.replace(ModuleParts.specifier, "");
237
285
  if (path.isAbsolute(p)) return path.resolve(p);
238
286
  const candidates = [path.resolve(process.cwd(), p), path.resolve(this.#app.cwdPath, p)];
239
287
  return candidates.find((candidate) => fs.existsSync(candidate)) ?? candidates[0];
@@ -1,5 +1,5 @@
1
1
  import { afterAll, describe, expect, test } from "bun:test";
2
- import { mkdtemp, rm } from "node:fs/promises";
2
+ import { mkdtemp, realpath, rm } from "node:fs/promises";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
5
  import { createTsconfigPackageResolver } from "../transforms/barrelImportsPlugin";
@@ -189,4 +189,311 @@ describe("GraphClientEntryDiscovery", () => {
189
189
  fresh.invalidate([file("Server.tsx")]);
190
190
  expect(await fresh.discover([file("Page.tsx")], { importsAs })).toEqual([]);
191
191
  });
192
+
193
+ test("the server modules of a walk are every file it reads before a client entry, with the same imports swapped or dropped", async () => {
194
+ const file = await tempDir();
195
+ await Bun.write(file("Page.tsx"), 'import "./Layout";\nimport "./Card";\n');
196
+ await Bun.write(file("Layout.tsx"), 'import "./Registry";\nimport "./Helper";\n');
197
+ await Bun.write(file("Helper.ts"), 'import "./Page";\nexport const helper = 1;\n');
198
+ await Bun.write(file("Registry.ts"), 'import "./Reference";\n');
199
+ await Bun.write(file("Reference.ts"), '"use client";\nexport const reference = "memo";\n');
200
+ await Bun.write(file("Card.tsx"), '"use client";\nimport "./Inner";\nexport const Card = () => null;\n');
201
+ await Bun.write(file("Inner.ts"), "export const inner = 1;\n");
202
+ const discovery = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
203
+ const names = (files: string[]) => files.map((each) => path.basename(each));
204
+
205
+ expect(names(await discovery.serverModules([file("Page.tsx")]))).toEqual([
206
+ "Helper.ts",
207
+ "Layout.tsx",
208
+ "Page.tsx",
209
+ "Registry.ts",
210
+ ]);
211
+ const dropped = new Map([[file("Layout.tsx"), new Map([["./Registry", null]])]]);
212
+ expect(names(await discovery.serverModules([file("Page.tsx")], { importsAs: dropped }))).toEqual([
213
+ "Helper.ts",
214
+ "Layout.tsx",
215
+ "Page.tsx",
216
+ ]);
217
+ expect(names(await discovery.serverModules([file("Card.tsx")]))).toEqual([]);
218
+ });
219
+
220
+ test("a client file exporting only hooks is no entry of a server graph that imports it, one with a component still is", async () => {
221
+ const file = await tempDir();
222
+ await Bun.write(file("Layout.tsx"), 'import "./store";\nimport "./Panel";\n');
223
+ await Bun.write(
224
+ file("store.ts"),
225
+ 'import { useForm } from "./useForm";\nimport { useEffect } from "./reactHooks";\nimport "./useMixed";\nexport const st = { useForm, useEffect };\n',
226
+ );
227
+ await Bun.write(
228
+ file("useForm.ts"),
229
+ '"use client";\nimport "./Inner";\nexport interface FormShape {}\nexport const useForm = () => null;\nexport function useOther() {}\n',
230
+ );
231
+ await Bun.write(file("reactHooks.ts"), '"use client";\nexport { useContext, useEffect } from "react";\n');
232
+ await Bun.write(
233
+ file("useMixed.ts"),
234
+ '"use client";\nexport const useMixed = () => null;\nexport const Mixed = () => null;\n',
235
+ );
236
+ await Bun.write(file("Inner.tsx"), '"use client";\nexport const Inner = () => null;\n');
237
+ await Bun.write(
238
+ file("Panel.tsx"),
239
+ '"use client";\nimport { useForm } from "./useForm";\nexport const Panel = () => useForm();\n',
240
+ );
241
+ const discovery = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
242
+ expect(await discovery.discover([file("Layout.tsx")])).toEqual([file("Panel.tsx"), file("useMixed.ts")]);
243
+ });
244
+
245
+ test("a client file with a default export or a star re-export stays an entry", async () => {
246
+ const file = await tempDir();
247
+ await Bun.write(file("S.tsx"), 'import "./A";\nimport "./B";\nimport "./C";\n');
248
+ await Bun.write(file("A.ts"), '"use client";\nexport default function useA() {}\n');
249
+ await Bun.write(file("B.ts"), '"use client";\nexport * from "./C";\nexport const useB = () => null;\n');
250
+ await Bun.write(file("C.ts"), '"use client";\nexport const C = () => null;\n');
251
+ const discovery = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
252
+ expect(await discovery.discover([file("S.tsx")])).toEqual([file("A.ts"), file("B.ts"), file("C.ts")]);
253
+ });
254
+
255
+ test("a page writing out an object's part reaches only that part's file under a route runtime, until the object moves it", async () => {
256
+ const dir = await realpath(await mkdtemp(path.join(os.tmpdir(), "akan-discovery-")));
257
+ dirs.push(dir);
258
+ const file = (name: string) => path.join(dir, name);
259
+ await Bun.write(
260
+ file("lib/memo/index.ts"),
261
+ 'import * as Template from "./Memo.Template";\nimport * as Zone from "./Memo.Zone";\nexport const Memo = { Template, Zone };\n',
262
+ );
263
+ for (const part of ["Template", "Zone", "Panel"])
264
+ await Bun.write(file(`lib/memo/Memo.${part}.tsx`), '"use client";\nexport const View = () => null;\n');
265
+ await Bun.write(
266
+ file("S.tsx"),
267
+ 'import { Memo } from "./lib/memo";\nexport const Page = () => <Memo.Zone.View />;\n',
268
+ );
269
+ const discoveryOf = (options?: { packageRoot: string }) =>
270
+ new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never, options);
271
+ const route = discoveryOf({ packageRoot: dir });
272
+
273
+ expect(await discoveryOf().discover([file("S.tsx")])).toEqual([
274
+ file("lib/memo/Memo.Template.tsx"),
275
+ file("lib/memo/Memo.Zone.tsx"),
276
+ ]);
277
+ expect(await route.discover([file("S.tsx")])).toEqual([file("lib/memo/Memo.Zone.tsx")]);
278
+
279
+ await Bun.write(
280
+ file("lib/memo/index.ts"),
281
+ 'import * as Template from "./Memo.Template";\nimport * as Zone from "./Memo.Panel";\nexport const Memo = { Template, Zone };\n',
282
+ );
283
+ route.invalidate([file("lib/memo/index.ts")]);
284
+ expect(await route.discover([file("S.tsx")])).toEqual([file("lib/memo/Memo.Panel.tsx")]);
285
+ });
286
+
287
+ describe("read by export", () => {
288
+ const byExport = () =>
289
+ new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never, { byExport: true });
290
+ const client = (name: string) => `"use client";\nexport const ${name} = () => null;\n`;
291
+ const writeView = async (file: (name: string) => string) => {
292
+ for (const name of ["Remove", "Chart", "Editor", "Panel", "Badge"])
293
+ await Bun.write(file(`ui/${name}.tsx`), client(name));
294
+ await Bun.write(
295
+ file("Memo.View.tsx"),
296
+ [
297
+ 'import { Badge } from "./ui/Badge";',
298
+ 'import { Chart } from "./ui/Chart";',
299
+ 'import { Remove } from "./ui/Remove";',
300
+ "const Title = () => <Badge />;",
301
+ "export const General = () => [<Title />, <Remove />];",
302
+ "export const Stats = () => <Chart />;",
303
+ 'export const Rich = async () => (await import("./ui/Editor")).Editor;',
304
+ "",
305
+ ].join("\n"),
306
+ );
307
+ };
308
+
309
+ test("a route reaches the client files of the export its page names, not of the whole server file", async () => {
310
+ const file = await tempDir();
311
+ await writeView(file);
312
+ await Bun.write(file("general.tsx"), 'import { General } from "./Memo.View";\nexport default General;\n');
313
+ await Bun.write(file("stats.tsx"), 'import { Stats as S } from "./Memo.View";\nexport default () => <S />;\n');
314
+ await Bun.write(file("rich.tsx"), 'import * as View from "./Memo.View";\nexport default View.Rich;\n');
315
+ await Bun.write(file("all.tsx"), 'import * as View from "./Memo.View";\nexport default () => View;\n');
316
+ const discovery = byExport();
317
+ const whole = ["Badge", "Chart", "Editor", "Remove"].map((name) => file(`ui/${name}.tsx`));
318
+
319
+ expect(await discovery.discover([file("general.tsx")])).toEqual(whole);
320
+ expect(await discovery.discoverRendered([file("general.tsx")])).toEqual([
321
+ file("ui/Badge.tsx"),
322
+ file("ui/Remove.tsx"),
323
+ ]);
324
+ expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
325
+ expect(await discovery.discoverRendered([file("rich.tsx")])).toEqual([file("ui/Editor.tsx")]);
326
+ expect(await discovery.discoverRendered([file("all.tsx")])).toEqual(whole);
327
+ });
328
+
329
+ test("each entry comes with the exports the route's server files ask of it, and with none named when one asks for the file", async () => {
330
+ const file = await tempDir();
331
+ const pair = file("ui/Pair.tsx");
332
+ await Bun.write(pair, `${client("A")}export const B = () => null;\nexport const C = () => null;\n`);
333
+ await Bun.write(file("a.tsx"), 'import { A } from "./ui/Pair";\nexport default () => <A />;\n');
334
+ await Bun.write(
335
+ file("ab.tsx"),
336
+ 'import { A } from "./ui/Pair";\nimport { B as Bee } from "./ui/Pair";\nexport default () => [A, Bee];\n',
337
+ );
338
+ await Bun.write(file("all.tsx"), 'import * as Pair from "./ui/Pair";\nexport default () => Pair;\n');
339
+ const discovery = byExport();
340
+
341
+ expect(await discovery.discoverAsked([file("a.tsx")])).toEqual(new Map([[pair, ["A"]]]));
342
+ expect(await discovery.discoverAsked([file("ab.tsx")])).toEqual(new Map([[pair, ["A", "B"]]]));
343
+ expect(await discovery.discoverAsked([file("all.tsx")])).toEqual(new Map([[pair, null]]));
344
+ expect(await discovery.discoverAsked([file("a.tsx"), file("all.tsx")])).toEqual(new Map([[pair, null]]));
345
+ const whole = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
346
+ expect(await whole.discoverAsked([file("a.tsx")])).toEqual(new Map([[pair, null]]));
347
+ });
348
+
349
+ test("a second route asking the same export gets the same answer, and a saved file is read again", async () => {
350
+ const file = await tempDir();
351
+ await writeView(file);
352
+ await Bun.write(file("stats.tsx"), 'import { Stats } from "./Memo.View";\nexport default Stats;\n');
353
+ await Bun.write(file("board.tsx"), 'import { Stats } from "./Memo.View";\nexport default () => <Stats />;\n');
354
+ const discovery = byExport();
355
+
356
+ expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
357
+ expect(await discovery.discoverRendered([file("board.tsx")])).toEqual([file("ui/Chart.tsx")]);
358
+
359
+ await Bun.write(
360
+ file("Memo.View.tsx"),
361
+ 'import { Panel } from "./ui/Panel";\nexport const Stats = () => <Panel />;\n',
362
+ );
363
+ discovery.invalidate([file("Memo.View.tsx")]);
364
+ expect(await discovery.discoverRendered([file("board.tsx")])).toEqual([file("ui/Panel.tsx")]);
365
+ });
366
+
367
+ test("without the setting, a route reaches what the whole server file imports", async () => {
368
+ const file = await tempDir();
369
+ await writeView(file);
370
+ await Bun.write(file("stats.tsx"), 'import { Stats } from "./Memo.View";\nexport default Stats;\n');
371
+ const discovery = new GraphClientEntryDiscovery({ barrelImports: [] }, (async () => null) as never);
372
+
373
+ expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual(
374
+ ["Badge", "Chart", "Editor", "Remove"].map((name) => file(`ui/${name}.tsx`)),
375
+ );
376
+ });
377
+
378
+ test("a client file registered by a top-level statement is reached by every route evaluating the module", async () => {
379
+ const file = await tempDir();
380
+ await writeView(file);
381
+ await Bun.write(file("registry.ts"), "export const parts: unknown[] = [];\n");
382
+ await Bun.write(
383
+ file("Memo.Zone.tsx"),
384
+ [
385
+ 'import { parts } from "./registry";',
386
+ 'import { Chart } from "./ui/Chart";',
387
+ 'import { Panel } from "./ui/Panel";',
388
+ "parts.push(Panel);",
389
+ "export const Stats = () => <Chart />;",
390
+ "export const Plain = () => null;",
391
+ "",
392
+ ].join("\n"),
393
+ );
394
+ await Bun.write(file("plain.tsx"), 'import { Plain } from "./Memo.Zone";\nexport default Plain;\n');
395
+
396
+ expect(await byExport().discoverRendered([file("plain.tsx")])).toEqual([file("ui/Panel.tsx")]);
397
+ });
398
+
399
+ test("a name asked through `export *` is followed into the file that holds it, and an unknown name takes the whole file", async () => {
400
+ const file = await tempDir();
401
+ await writeView(file);
402
+ await Bun.write(file("index.ts"), 'export * from "./Memo.View";\nexport * from "./other";\n');
403
+ await Bun.write(file("other.tsx"), 'import { Panel } from "./ui/Panel";\nexport const Side = () => <Panel />;\n');
404
+ await Bun.write(
405
+ file("legacy.js"),
406
+ 'const { Panel } = require("./ui/Panel");\nmodule.exports = { Old: Panel };\n',
407
+ );
408
+ await Bun.write(file("stats.tsx"), 'import { Stats } from "./index";\nexport default Stats;\n');
409
+ await Bun.write(file("old.tsx"), 'import { Old } from "./legacy";\nexport default Old;\n');
410
+ const discovery = byExport();
411
+
412
+ expect(await discovery.discoverRendered([file("stats.tsx")])).toEqual([file("ui/Chart.tsx")]);
413
+ expect(await discovery.discoverRendered([file("old.tsx")])).toEqual([file("ui/Panel.tsx")]);
414
+ });
415
+
416
+ test("an import swapped or dropped for the walk is swapped or dropped by export too", async () => {
417
+ const file = await tempDir();
418
+ await writeView(file);
419
+ await Bun.write(
420
+ file("Shell.tsx"),
421
+ 'import { Panel } from "./ui/Panel";\nexport const General = () => <Panel />;\n',
422
+ );
423
+ await Bun.write(
424
+ file("page.tsx"),
425
+ 'import { General } from "./Memo.View";\nimport { Chart } from "./ui/Chart";\nexport default () => [<General />, <Chart />];\n',
426
+ );
427
+ const discovery = byExport();
428
+ const importsAs = new Map([
429
+ [
430
+ file("page.tsx"),
431
+ new Map<string, string | null>([
432
+ ["./Memo.View", "./Shell"],
433
+ ["./ui/Chart", null],
434
+ ]),
435
+ ],
436
+ ]);
437
+
438
+ expect(await discovery.discoverRendered([file("page.tsx")], { importsAs })).toEqual([file("ui/Panel.tsx")]);
439
+ expect(await discovery.discoverRendered([file("page.tsx")])).toEqual(
440
+ ["Badge", "Chart", "Remove"].map((name) => file(`ui/${name}.tsx`)),
441
+ );
442
+ });
443
+ });
444
+
445
+ test("a page naming a part of the object a wrapper copies in reaches that part's file and none of the wrapper's own, until the copied object or its barrel moves it", async () => {
446
+ const dir = await realpath(await mkdtemp(path.join(os.tmpdir(), "akan-discovery-")));
447
+ dirs.push(dir);
448
+ const file = (name: string) => path.join(dir, name);
449
+ const client = (name: string) => `"use client";\nexport const ${name} = () => null;\n`;
450
+ const fieldIndex = (text: string) =>
451
+ `import { Text } from "./${text}";\nexport const Field = Object.assign(() => null, { Text });\n`;
452
+ const wrapper = [
453
+ 'import { memo } from "react";',
454
+ 'import { Field as BaseField } from "@demo/ui";',
455
+ 'import { Editor } from "./Editor";',
456
+ "const Rich = memo(() => <Editor />);",
457
+ "export const Field = Object.assign(() => null, BaseField, { Rich });",
458
+ ].join("\n");
459
+ const pageOf = (wrap: string, member: string) =>
460
+ `import { Field } from "./wrap/${wrap}";\nexport const Page = () => <Field.${member} />;\n`;
461
+ await Bun.write(file("ui/index.ts"), 'export * from "./Field";\n');
462
+ await Bun.write(file("ui/Field/index.tsx"), fieldIndex("Text"));
463
+ await Bun.write(file("ui/FieldNext/index.tsx"), fieldIndex("TextLast"));
464
+ for (const text of ["Field/Text", "Field/TextNext", "FieldNext/TextLast"])
465
+ await Bun.write(file(`ui/${text}.tsx`), client("Text"));
466
+ await Bun.write(file("wrap/Field.tsx"), wrapper);
467
+ await Bun.write(file("wrap/ClientField.tsx"), `"use client";\n${wrapper}`);
468
+ await Bun.write(file("wrap/Editor.tsx"), client("Editor"));
469
+ await Bun.write(file("Text.tsx"), pageOf("Field", "Text"));
470
+ await Bun.write(file("Rich.tsx"), pageOf("Field", "Rich"));
471
+ await Bun.write(file("ClientText.tsx"), pageOf("ClientField", "Text"));
472
+ await Bun.write(file("ClientRich.tsx"), pageOf("ClientField", "Rich"));
473
+ const entries: Record<string, string> = {
474
+ "@demo/ui": "ui/index.ts",
475
+ "@demo/ui/Field": "ui/Field/index.tsx",
476
+ "@demo/ui/FieldNext": "ui/FieldNext/index.tsx",
477
+ };
478
+ const resolvePackage = async (pkgName: string) => {
479
+ const entry = entries[pkgName];
480
+ return entry ? { pkgName, entryFile: file(entry), pkgDir: file("ui") } : null;
481
+ };
482
+ const route = new GraphClientEntryDiscovery({ barrelImports: ["@demo/ui"] }, resolvePackage as never, {
483
+ packageRoot: dir,
484
+ });
485
+
486
+ expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/Field/Text.tsx")]);
487
+ expect(await route.discover([file("Rich.tsx")])).toEqual([file("ui/Field/Text.tsx"), file("wrap/Editor.tsx")]);
488
+ expect(await route.discover([file("ClientText.tsx")])).toEqual([file("ui/Field/Text.tsx")]);
489
+ expect(await route.discover([file("ClientRich.tsx")])).toEqual([file("wrap/ClientField.tsx")]);
490
+
491
+ await Bun.write(file("ui/Field/index.tsx"), fieldIndex("TextNext"));
492
+ route.invalidate([file("ui/Field/index.tsx")]);
493
+ expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/Field/TextNext.tsx")]);
494
+
495
+ await Bun.write(file("ui/index.ts"), 'export * from "./FieldNext";\n');
496
+ route.invalidate([file("ui/index.ts")]);
497
+ expect(await route.discover([file("Text.tsx")])).toEqual([file("ui/FieldNext/TextLast.tsx")]);
498
+ });
192
499
  });