@akanjs/devkit 3.3.0-rc.3 → 3.3.0-rc.31
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
|
@@ -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,98 @@ 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
|
+
});
|
|
192
286
|
});
|
|
@@ -4,9 +4,12 @@ import { SOURCE_EXTS } from "../akanApp/devHostPolicy";
|
|
|
4
4
|
import type { App } from "../commandDecorators";
|
|
5
5
|
import { BarrelAnalyzer, type BarrelExportMap } from "../transforms/barrelAnalyzer";
|
|
6
6
|
import { createTsconfigPackageResolver, rewriteBarrelImports } from "../transforms/barrelImportsPlugin";
|
|
7
|
+
import { ObjectMemberImports } from "../transforms/objectMemberImports";
|
|
7
8
|
import type { AkanConfig, ClientEntryDiscovery, DiscoverOptions, ScannedImport } from "./clientBuildTypes";
|
|
8
9
|
|
|
9
10
|
const USE_CLIENT_RE = /^\s*(?:\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/\s*|\/\/[^\n]*\n\s*)*["']use client["']/;
|
|
11
|
+
const EXPORT_STAR_RE = /^\s*export\s*\*/m;
|
|
12
|
+
const HOOK_NAME_RE = /^use[A-Z0-9]/;
|
|
10
13
|
const NODE_MODULES_RE = /[\\/]node_modules[\\/]/;
|
|
11
14
|
const AKANJS_NODE_MODULE_RE = /[\\/]node_modules[\\/]akanjs[\\/]/;
|
|
12
15
|
// Bun's scanImports also surfaces CSS and asset imports, none of which can be a client component.
|
|
@@ -56,6 +59,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
56
59
|
#swappedEntriesCaches = new WeakMap<object, Map<string, Set<string>>>();
|
|
57
60
|
#readFiles = new Set<string>();
|
|
58
61
|
#barrelImporters = new Map<string, Set<string>>();
|
|
62
|
+
#objects: ObjectMemberImports | null;
|
|
63
|
+
#objectReaders = new Map<string, Set<string>>();
|
|
59
64
|
//? Barrels a save went beside, by the analysis before it: a save of any component re-reads its barrel, and only an
|
|
60
65
|
//? export that moved sends that barrel's importers back to the disk.
|
|
61
66
|
#reread = new Map<string, Promise<BarrelExportMap | null>>();
|
|
@@ -64,14 +69,24 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
64
69
|
//? answers its caller but caches nothing (a registry check walks this instance beside the slow lane's route builds).
|
|
65
70
|
#epoch = 0;
|
|
66
71
|
|
|
67
|
-
constructor(
|
|
72
|
+
constructor(
|
|
73
|
+
akanConfig: Pick<AkanConfig, "barrelImports">,
|
|
74
|
+
resolvePackage: PackageResolver,
|
|
75
|
+
{ packageRoot }: { packageRoot?: string } = {},
|
|
76
|
+
) {
|
|
68
77
|
this.#akanConfig = akanConfig;
|
|
69
78
|
this.#resolvePackage = resolvePackage;
|
|
70
|
-
this.#analyzer = new BarrelAnalyzer({ resolvePackage });
|
|
79
|
+
this.#analyzer = new BarrelAnalyzer({ resolvePackage, ...(packageRoot ? { packageRoot } : {}) });
|
|
80
|
+
this.#objects = packageRoot
|
|
81
|
+
? new ObjectMemberImports({ resolve: ObjectMemberImports.resolverOf(resolvePackage) })
|
|
82
|
+
: null;
|
|
71
83
|
}
|
|
72
84
|
|
|
85
|
+
//? An app narrows as its bundles do, so discovery reaches the member files they load.
|
|
73
86
|
static async create(app: App): Promise<GraphClientEntryDiscovery> {
|
|
74
|
-
return new GraphClientEntryDiscovery(await app.getConfig(), await createTsconfigPackageResolver(app)
|
|
87
|
+
return new GraphClientEntryDiscovery(await app.getConfig(), await createTsconfigPackageResolver(app), {
|
|
88
|
+
packageRoot: app.workspace.workspaceRoot,
|
|
89
|
+
});
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
async discover(seeds: string[], { importsAs }: DiscoverOptions = {}): Promise<string[]> {
|
|
@@ -86,6 +101,26 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
86
101
|
return Array.from(entries).sort();
|
|
87
102
|
}
|
|
88
103
|
|
|
104
|
+
async serverModules(seeds: string[], { importsAs }: DiscoverOptions = {}): Promise<string[]> {
|
|
105
|
+
const seen = new Set<string>();
|
|
106
|
+
const queue = seeds.map((seed) => path.resolve(seed));
|
|
107
|
+
for (const absPath of queue) {
|
|
108
|
+
if (seen.has(absPath) || shouldSkipNodeModule(absPath)) continue;
|
|
109
|
+
const facts = await this.#facts(absPath);
|
|
110
|
+
if (!facts || facts.isClientEntry) continue;
|
|
111
|
+
seen.add(absPath);
|
|
112
|
+
const swaps = importsAs?.get(absPath);
|
|
113
|
+
for (const imp of facts.imports) {
|
|
114
|
+
const swapped = swaps?.get(imp.path);
|
|
115
|
+
const spec = swapped === undefined ? imp.path : swapped;
|
|
116
|
+
if (!spec || NON_SOURCE_EXT_RE.test(spec)) continue;
|
|
117
|
+
const resolved = await this.#resolveSpecifier(spec, path.dirname(absPath));
|
|
118
|
+
if (resolved) queue.push(path.resolve(resolved));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return [...seen].sort();
|
|
122
|
+
}
|
|
123
|
+
|
|
89
124
|
async resolveImport(spec: string, importerDir: string): Promise<string | null> {
|
|
90
125
|
const resolved = await this.#resolveSpecifier(spec, importerDir);
|
|
91
126
|
return resolved ? path.resolve(resolved) : null;
|
|
@@ -114,6 +149,7 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
114
149
|
this.#swappedEntriesCaches = new WeakMap();
|
|
115
150
|
for (const [barrel, before] of this.#analyzer.forget(files))
|
|
116
151
|
if (!this.#reread.has(barrel)) this.#reread.set(barrel, before);
|
|
152
|
+
this.#forgetObjects(files);
|
|
117
153
|
//? A file gone, or one never read, can leave a found resolution on its old target (`Chart.tsx` to
|
|
118
154
|
//? `Chart/index.tsx`), so a move drops them all; a save of a file already read keeps them (a walk re-resolving
|
|
119
155
|
//? every package cost 3x). Misses lapse with the epoch on their own (#lookup).
|
|
@@ -123,6 +159,18 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
123
159
|
this.#resolvedSpecifierCache.clear();
|
|
124
160
|
}
|
|
125
161
|
|
|
162
|
+
//? An importer's facts hold the member files its object imports narrowed to: a save of the object module or of a
|
|
163
|
+
//? member file can move them.
|
|
164
|
+
#forgetObjects(files: string[]) {
|
|
165
|
+
if (!this.#objects) return;
|
|
166
|
+
this.#objects.modules.forget(files);
|
|
167
|
+
for (const file of files) {
|
|
168
|
+
const absPath = path.resolve(file);
|
|
169
|
+
for (const importer of this.#objectReaders.get(absPath) ?? []) this.#factsCache.delete(importer);
|
|
170
|
+
this.#objectReaders.delete(absPath);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
126
174
|
//? An importer's facts hold the files its barrel imports narrowed to, which an export moved between them changes.
|
|
127
175
|
async #dropRenarrowed(): Promise<void> {
|
|
128
176
|
const reread = [...this.#reread];
|
|
@@ -172,7 +220,7 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
172
220
|
.catch(() => null);
|
|
173
221
|
if (content === null) return null;
|
|
174
222
|
this.#readFiles.add(absPath);
|
|
175
|
-
if (USE_CLIENT_RE.test(content)) return { isClientEntry:
|
|
223
|
+
if (USE_CLIENT_RE.test(content)) return { isClientEntry: !this.#exportsOnlyHooks(content), imports: [] };
|
|
176
224
|
return { isClientEntry: false, imports: this.#scanImports(await this.#rewrite(content, absPath)) };
|
|
177
225
|
})();
|
|
178
226
|
this.#factsCache.set(absPath, cached);
|
|
@@ -181,16 +229,49 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
181
229
|
}
|
|
182
230
|
|
|
183
231
|
async #rewrite(content: string, absPath: string): Promise<string> {
|
|
232
|
+
const rewritten = await this.#rewriteBarrels(content, absPath);
|
|
233
|
+
return (await this.#narrowObjects(rewritten, absPath)) ?? rewritten;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async #rewriteBarrels(content: string, absPath: string): Promise<string> {
|
|
184
237
|
if (this.#akanConfig.barrelImports.length === 0) return content;
|
|
185
238
|
for (const barrel of this.#akanConfig.barrelImports.filter((barrel) => content.includes(barrel)))
|
|
186
239
|
this.#barrelImporters.set(barrel, (this.#barrelImporters.get(barrel) ?? new Set()).add(absPath));
|
|
187
240
|
try {
|
|
188
|
-
|
|
241
|
+
const reexports = this.#objects !== null;
|
|
242
|
+
return (
|
|
243
|
+
(await rewriteBarrelImports(content, this.#akanConfig.barrelImports, this.#analyzer, { reexports })) ?? content
|
|
244
|
+
);
|
|
189
245
|
} catch {
|
|
190
246
|
return content;
|
|
191
247
|
}
|
|
192
248
|
}
|
|
193
249
|
|
|
250
|
+
async #narrowObjects(content: string, absPath: string): Promise<string | null> {
|
|
251
|
+
if (!this.#objects) return null;
|
|
252
|
+
try {
|
|
253
|
+
const narrowed = await this.#objects.rewrite(content, absPath);
|
|
254
|
+
for (const file of narrowed?.read ?? []) {
|
|
255
|
+
const absFile = path.resolve(file);
|
|
256
|
+
this.#objectReaders.set(absFile, (this.#objectReaders.get(absFile) ?? new Set()).add(absPath));
|
|
257
|
+
}
|
|
258
|
+
return narrowed?.contents ?? null;
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
//? A hook is never rendered, so no flight row names its file; `export *` hides names from the scan, so it stays an entry.
|
|
265
|
+
#exportsOnlyHooks(content: string): boolean {
|
|
266
|
+
if (EXPORT_STAR_RE.test(content)) return false;
|
|
267
|
+
try {
|
|
268
|
+
const { exports } = this.#tsTranspiler.scan(content);
|
|
269
|
+
return exports.length > 0 && exports.every((name) => HOOK_NAME_RE.test(name));
|
|
270
|
+
} catch {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
194
275
|
#scanImports(source: string): ScannedImport[] {
|
|
195
276
|
try {
|
|
196
277
|
return this.#tsTranspiler.scanImports(source);
|
|
@@ -226,8 +307,8 @@ export class GraphClientEntryDiscovery implements ClientEntryDiscovery {
|
|
|
226
307
|
this.#resolvedSpecifierCache,
|
|
227
308
|
cacheKey,
|
|
228
309
|
async () => {
|
|
229
|
-
if (spec.startsWith(".") ||
|
|
230
|
-
const abs =
|
|
310
|
+
if (spec.startsWith(".") || path.isAbsolute(spec)) {
|
|
311
|
+
const abs = path.resolve(importerDir, spec);
|
|
231
312
|
return await this.#resolveFileCandidate(abs);
|
|
232
313
|
}
|
|
233
314
|
const pkg = await this.#resolvePackage(spec);
|
|
@@ -3,6 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import type { App } from "../commandDecorators";
|
|
6
|
+
import { printedInOwnProcess, tempDirs } from "../test/testHelpers";
|
|
6
7
|
import { CsrDevModuleCompiler } from "./csrDevModuleCompiler";
|
|
7
8
|
import { CsrDevPaths } from "./csrDevPaths";
|
|
8
9
|
import { CsrDevResolver } from "./csrDevResolver";
|
|
@@ -189,6 +190,28 @@ describe("CsrDevModuleCompiler", () => {
|
|
|
189
190
|
await rm(root, { recursive: true, force: true });
|
|
190
191
|
});
|
|
191
192
|
|
|
193
|
+
test("answers the model runtime marker with the context's source, in an installed package and in app code", async () => {
|
|
194
|
+
const markers = ["node_modules/akanjs/ui/modelRuntime.ts", "pkgs/akanjs/ui/modelRuntime.ts"].map(file);
|
|
195
|
+
for (const marker of markers) await Bun.write(marker, "export {};\n");
|
|
196
|
+
await Bun.write(file("apps/demo/lib/st.ts"), 'export const st = "store";\n');
|
|
197
|
+
const paths = new CsrDevPaths(root);
|
|
198
|
+
const modelRuntime = `import ${JSON.stringify(file("apps/demo/lib/st.ts"))};\n`;
|
|
199
|
+
const compiler = new CsrDevModuleCompiler({
|
|
200
|
+
app: { verbose: () => undefined, logger: { error: (message: string) => logged.push(message) } } as unknown as App,
|
|
201
|
+
paths,
|
|
202
|
+
resolver: new CsrDevResolver({ paths, context, entryFiles: [] }),
|
|
203
|
+
context: { ...context, modelRuntime },
|
|
204
|
+
outDir: file("out"),
|
|
205
|
+
routePrefix: "/_akan/registry/",
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const { modules } = await compiler.compile(markers, new Set());
|
|
209
|
+
|
|
210
|
+
for (const marker of markers)
|
|
211
|
+
expect(modules.find((module) => module.file === marker)?.deps).toEqual([file("apps/demo/lib/st.ts")]);
|
|
212
|
+
expect(modules.map((module) => module.file)).toContain(file("apps/demo/lib/st.ts"));
|
|
213
|
+
});
|
|
214
|
+
|
|
192
215
|
test("a pre-bundled file keeps its own __reExport and __toESM, which Bun leaves in place of its own", async () => {
|
|
193
216
|
const { modules } = await compile("node_modules/prebundled/chunk.mjs");
|
|
194
217
|
const [module] = modules;
|
|
@@ -388,3 +411,66 @@ describe("CsrDevModuleCompiler barrel narrowing", () => {
|
|
|
388
411
|
]);
|
|
389
412
|
});
|
|
390
413
|
});
|
|
414
|
+
|
|
415
|
+
describe("CsrDevModuleCompiler loading per module", () => {
|
|
416
|
+
const makeTempRoot = tempDirs("akan-csr-dev-descriptors-");
|
|
417
|
+
|
|
418
|
+
test("answers a module piece's macro with descriptors read from the signal, in a process of its own", async () => {
|
|
419
|
+
const root = CsrDevPaths.realpath(await makeTempRoot());
|
|
420
|
+
await Bun.write(
|
|
421
|
+
path.join(root, "apps/demo/lib/sig.ts"),
|
|
422
|
+
`const serialized = { draft: { endpoint: { draftOf: { type: "query", args: [], returns: { refName: "draft-descriptor-ref" } } } } };
|
|
423
|
+
export const fetch = { ownSerializedSignal: serialized };
|
|
424
|
+
export const getSerializedSignalOf = (name: string) => (name === "base" ? {} : { [name]: serialized[name as "draft"] });
|
|
425
|
+
`,
|
|
426
|
+
);
|
|
427
|
+
const piece = path.join(root, "apps/demo/lib/__client/draft.fetch.ts");
|
|
428
|
+
await Bun.write(
|
|
429
|
+
piece,
|
|
430
|
+
'import { getSerializedSignalOf } from "../sig" with { type: "macro" };\nexport const descriptors = getSerializedSignalOf("draft");\n',
|
|
431
|
+
);
|
|
432
|
+
const dir = JSON.stringify(import.meta.dir);
|
|
433
|
+
|
|
434
|
+
const compiled = (await printedInOwnProcess(
|
|
435
|
+
`
|
|
436
|
+
const path = await import("node:path");
|
|
437
|
+
const { CsrDevModuleCompiler } = await import(path.join(${dir}, "csrDevModuleCompiler.ts"));
|
|
438
|
+
const { CsrDevPaths } = await import(path.join(${dir}, "csrDevPaths.ts"));
|
|
439
|
+
const { CsrDevResolver } = await import(path.join(${dir}, "csrDevResolver.ts"));
|
|
440
|
+
const { SignalDescriptors } = await import(path.join(${dir}, "../transforms/signalDescriptors.ts"));
|
|
441
|
+
const [root, piece] = ${JSON.stringify([root, piece])};
|
|
442
|
+
let answered = 0;
|
|
443
|
+
const inline = SignalDescriptors.prototype.inline;
|
|
444
|
+
SignalDescriptors.prototype.inline = async function (...args) {
|
|
445
|
+
const contents = await inline.apply(this, args);
|
|
446
|
+
if (contents !== null) answered += 1;
|
|
447
|
+
return contents;
|
|
448
|
+
};
|
|
449
|
+
const logged = [];
|
|
450
|
+
const context = {
|
|
451
|
+
...${JSON.stringify(context)},
|
|
452
|
+
routeRuntime: { scopes: ["apps/demo"], redirectOf: () => null, transform: () => null },
|
|
453
|
+
};
|
|
454
|
+
const paths = new CsrDevPaths(root);
|
|
455
|
+
const compiler = new CsrDevModuleCompiler({
|
|
456
|
+
app: {
|
|
457
|
+
verbose: () => undefined,
|
|
458
|
+
logger: { error: (message) => logged.push(message), warn: (message) => logged.push(message) },
|
|
459
|
+
workspace: { workspaceRoot: root },
|
|
460
|
+
},
|
|
461
|
+
paths,
|
|
462
|
+
resolver: new CsrDevResolver({ paths, context, entryFiles: [] }),
|
|
463
|
+
context,
|
|
464
|
+
outDir: path.join(root, "out"),
|
|
465
|
+
routePrefix: "/_akan/registry/",
|
|
466
|
+
});
|
|
467
|
+
const { modules } = await compiler.compile([piece], new Set());
|
|
468
|
+
console.log(JSON.stringify({ answered, logged, factories: modules.map(({ factory }) => factory) }));
|
|
469
|
+
`,
|
|
470
|
+
root,
|
|
471
|
+
)) as { answered: number; logged: string[]; factories: string[] };
|
|
472
|
+
|
|
473
|
+
expect({ answered: compiled.answered, logged: compiled.logged }).toEqual({ answered: 1, logged: [] });
|
|
474
|
+
expect(compiled.factories.join("\n")).toContain("draft-descriptor-ref");
|
|
475
|
+
}, 30_000);
|
|
476
|
+
});
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import { SSR_DEV_DIRNAME } from "akanjs/server/hmr/csrDevManifest";
|
|
2
3
|
import type { BunPlugin } from "bun";
|
|
4
|
+
import { StoreBootstrap } from "../artifact/storeBootstrap";
|
|
3
5
|
import type { App } from "../commandDecorators";
|
|
4
6
|
import { loaderFor } from "../transforms/moduleSyntax";
|
|
7
|
+
import { SignalDescriptors } from "../transforms/signalDescriptors";
|
|
5
8
|
import { CsrDevFactoryCheck } from "./csrDevFactoryCheck";
|
|
6
9
|
import { CsrDevPaths } from "./csrDevPaths";
|
|
7
10
|
import { CsrDevResolver } from "./csrDevResolver";
|
|
@@ -13,6 +16,7 @@ import type {
|
|
|
13
16
|
CsrDevSharedHelpers,
|
|
14
17
|
} from "./csrDevTypes";
|
|
15
18
|
import { DevBarrelImports } from "./devBarrelImports";
|
|
19
|
+
import { DevRouteRuntime } from "./devRouteRuntime";
|
|
16
20
|
import { RouteClientBuilder } from "./routeClientBuilder";
|
|
17
21
|
|
|
18
22
|
interface OutputMetafile {
|
|
@@ -52,7 +56,8 @@ export interface CsrDevCompileResult {
|
|
|
52
56
|
//* Every module becomes its own CJS factory: one Bun.build with each import external, so a save re-runs only the
|
|
53
57
|
//* modules it changed instead of reloading one scope-hoisted file.
|
|
54
58
|
export class CsrDevModuleCompiler {
|
|
55
|
-
static readonly #storeRoot = /(?:^|\/)lib\/st\.ts$/;
|
|
59
|
+
static readonly #storeRoot = /(?:^|\/)lib\/(?:st|__client\/(?:_st|[^/]+\.st))\.ts$/;
|
|
60
|
+
static readonly #storeOwners = `/.akan/generated/${SSR_DEV_DIRNAME}/${DevRouteRuntime.ownersModule}.ts`;
|
|
56
61
|
static readonly #macroImport = /\b(?:with|assert)\s*\{\s*type\s*:\s*["']macro["']/;
|
|
57
62
|
static readonly #factoryArgument =
|
|
58
63
|
/(?<![\w$.])(?:require|module|exports|__akanImport|\$RefreshReg\$|\$RefreshSig\$)(?![\w$]|\s*:)/;
|
|
@@ -69,6 +74,7 @@ export class CsrDevModuleCompiler {
|
|
|
69
74
|
readonly #assets = new Set<string>();
|
|
70
75
|
#barrels: Promise<DevBarrelImports | null> | null = null;
|
|
71
76
|
readonly #narrowed = new Map<string, Record<string, CsrDevNarrowedImport>>();
|
|
77
|
+
readonly #routed = new Map<string, string>();
|
|
72
78
|
|
|
73
79
|
constructor({ app, paths, resolver, context, outDir, routePrefix }: CsrDevModuleCompilerOptions) {
|
|
74
80
|
this.#app = app;
|
|
@@ -216,6 +222,7 @@ export class CsrDevModuleCompiler {
|
|
|
216
222
|
}
|
|
217
223
|
const sourceMap = sourceMaps.get(`${artifact.path}.map`);
|
|
218
224
|
const narrowed = this.#narrowed.get(file);
|
|
225
|
+
const routed = this.#routed.get(file);
|
|
219
226
|
modules.push({
|
|
220
227
|
id,
|
|
221
228
|
file,
|
|
@@ -226,6 +233,7 @@ export class CsrDevModuleCompiler {
|
|
|
226
233
|
mtimeMs: mtimes.get(file) ?? CsrDevPaths.mtimeOf(file),
|
|
227
234
|
hash: hashes.get(file) ?? "",
|
|
228
235
|
...(narrowed && Object.keys(narrowed).length > 0 ? { narrowed } : {}),
|
|
236
|
+
...(routed ? { routed } : {}),
|
|
229
237
|
});
|
|
230
238
|
}
|
|
231
239
|
return { modules, misses, tangled };
|
|
@@ -279,8 +287,18 @@ export class CsrDevModuleCompiler {
|
|
|
279
287
|
//? rewrite leaves a free `__akanMeta` that no factory header declares. So a file importing a macro has its macros run
|
|
280
288
|
//? first by a build of its own, with every other import left as written, and the module build reads that output.
|
|
281
289
|
async #evaluateMacros(files: string[]): Promise<Map<string, string>> {
|
|
282
|
-
const
|
|
290
|
+
const { routeRuntime } = this.#context;
|
|
291
|
+
const sources = await Promise.all(
|
|
292
|
+
files
|
|
293
|
+
.filter((file) => !routeRuntime?.redirectOf(file))
|
|
294
|
+
.map(async (file) => [file, await Bun.file(file).text()] as const),
|
|
295
|
+
);
|
|
283
296
|
const importers = sources.filter(([, source]) => CsrDevModuleCompiler.#macroImport.test(source));
|
|
297
|
+
const descriptors = routeRuntime
|
|
298
|
+
? new SignalDescriptors(this.#app.workspace.workspaceRoot, routeRuntime.scopes, {
|
|
299
|
+
onUnread: (reason) => this.#app.logger.warn(`[csr-dev] ${reason}, so their pieces run the macro`),
|
|
300
|
+
}).plugin
|
|
301
|
+
: null;
|
|
284
302
|
const outputs = await Promise.all(
|
|
285
303
|
importers.map(async ([file]) => {
|
|
286
304
|
const result = await Bun.build({
|
|
@@ -288,6 +306,7 @@ export class CsrDevModuleCompiler {
|
|
|
288
306
|
target: "browser",
|
|
289
307
|
format: "esm",
|
|
290
308
|
plugins: [
|
|
309
|
+
...(descriptors ? [descriptors] : []),
|
|
291
310
|
{
|
|
292
311
|
name: "akan-csr-macro-pass",
|
|
293
312
|
setup: (build) =>
|
|
@@ -314,6 +333,14 @@ export class CsrDevModuleCompiler {
|
|
|
314
333
|
name: "akan-csr-registry",
|
|
315
334
|
setup: (build) => {
|
|
316
335
|
build.onLoad({ filter: /\.css$/ }, () => ({ contents: "", loader: "js" }));
|
|
336
|
+
const { modelRuntime, routeRuntime } = this.#context;
|
|
337
|
+
if (modelRuntime !== undefined)
|
|
338
|
+
build.onLoad({ filter: StoreBootstrap.modelRuntimeFilter }, () => ({ contents: modelRuntime, loader: "ts" }));
|
|
339
|
+
if (routeRuntime && !vendor)
|
|
340
|
+
build.onLoad({ filter: /[\\/]lib[\\/](?:useClient|st)\.ts$/ }, (args) => {
|
|
341
|
+
const contents = routeRuntime.redirectOf(args.path);
|
|
342
|
+
return contents === null ? undefined : { contents, loader: "ts" };
|
|
343
|
+
});
|
|
317
344
|
if (macroOutputs.size > 0) {
|
|
318
345
|
const exact = [...macroOutputs.keys()].map((file) => file.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
319
346
|
build.onLoad({ filter: new RegExp(`^(?:${exact.join("|")})$`) }, (args) => {
|
|
@@ -325,9 +352,16 @@ export class CsrDevModuleCompiler {
|
|
|
325
352
|
build.onLoad({ filter: /\.(?:tsx?|jsx?)$/ }, async (args) => {
|
|
326
353
|
if (CsrDevPaths.isVendorFile(args.path)) return undefined;
|
|
327
354
|
const source = await Bun.file(args.path).text();
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
355
|
+
const file = CsrDevPaths.realpath(args.path);
|
|
356
|
+
const routed = routeRuntime?.transform(source, args.path) ?? null;
|
|
357
|
+
if (routed) this.#routed.set(file, routed.stamp);
|
|
358
|
+
else this.#routed.delete(file);
|
|
359
|
+
const input = routed?.contents ?? source;
|
|
360
|
+
const rewrite = await (await this.#barrelImports())?.rewrite(input, args.path, {
|
|
361
|
+
perModule: routed?.perModule,
|
|
362
|
+
});
|
|
363
|
+
if (rewrite) this.#narrowed.set(file, rewrite.narrowed);
|
|
364
|
+
const narrowed = rewrite?.contents ?? input;
|
|
331
365
|
const normalized = args.path.endsWith(".tsx")
|
|
332
366
|
? RouteClientBuilder.normalizeNamedDefaultFunctionForFastRefresh(narrowed, { path: args.path })
|
|
333
367
|
: null;
|
|
@@ -359,7 +393,7 @@ export class CsrDevModuleCompiler {
|
|
|
359
393
|
}
|
|
360
394
|
|
|
361
395
|
#barrelImports(): Promise<DevBarrelImports | null> {
|
|
362
|
-
this.#barrels ??= DevBarrelImports.create(this.#app, this.#context
|
|
396
|
+
this.#barrels ??= DevBarrelImports.create(this.#app, this.#context);
|
|
363
397
|
return this.#barrels;
|
|
364
398
|
}
|
|
365
399
|
|
|
@@ -413,8 +447,10 @@ export class CsrDevModuleCompiler {
|
|
|
413
447
|
);
|
|
414
448
|
}
|
|
415
449
|
// StoreRegistry.build merges into one global store instance, so every importer already holds the updated `st`:
|
|
416
|
-
// re-running the store root is the whole update
|
|
417
|
-
|
|
450
|
+
// re-running the store root, or a module's store piece, is the whole update: bubbling past it would only reach the
|
|
451
|
+
// page modules.
|
|
452
|
+
if (CsrDevModuleCompiler.#storeRoot.test(id) || id.endsWith(CsrDevModuleCompiler.#storeOwners))
|
|
453
|
+
patched += "\nmodule.hot.accept();";
|
|
418
454
|
const shared = CsrDevModuleCompiler.#shareHelpers(patched);
|
|
419
455
|
//? What `import.meta` is in a browser module: a `url` and no `env`, so `import.meta.env` checks (jotai, zustand)
|
|
420
456
|
//? branch as they do in the ESM bundle. On the header line, outside the preamble that modules share.
|
|
@@ -75,7 +75,7 @@ export class CsrDevPatcher {
|
|
|
75
75
|
if (state.graph.configKey !== context.configKey)
|
|
76
76
|
return this.#handBack("the dev bundle config changed", generation, context);
|
|
77
77
|
const metadataFile = changedFiles.find((file) => this.#bundler.isMetadataFile(file));
|
|
78
|
-
// `lib/useClient.ts`
|
|
78
|
+
// `lib/useClient.ts` and `lib/usePage.ts` inline signal and dictionary metadata through macros at build time.
|
|
79
79
|
if (metadataFile) return this.#handBack(`${path.basename(metadataFile)} changed`, generation, context);
|
|
80
80
|
//? Only a registry read from disk: one held in memory saw every save since, and a metadata save was handed back.
|
|
81
81
|
if (!resident && state.graph.metadata !== (await this.#bundler.metadataFingerprint()))
|
|
@@ -110,6 +110,8 @@ export class CsrDevPatcher {
|
|
|
110
110
|
const forgotten = this.#forgetDeletedModules(state, present);
|
|
111
111
|
const changed = onlyRoots ? new Set<string>() : await this.#changedModules(graph, changedFiles, present);
|
|
112
112
|
const restamped = onlyRoots ? null : await this.#renarrowed(context, graph, changedFiles, changed);
|
|
113
|
+
const routeTable = onlyRoots ? undefined : await this.#rerouted(context, graph, changed);
|
|
114
|
+
for (const id of CsrDevPatcher.#extendingAppStores(context, graph, changed)) changed.add(id);
|
|
113
115
|
for (const id of this.#withoutFactory(state, present)) changed.add(id);
|
|
114
116
|
for (const file of entries.changed) changed.add(paths.idOf(file));
|
|
115
117
|
const rootFiles = [...Object.values(entries.files), ...roots.filter((file) => fs.existsSync(file))];
|
|
@@ -124,6 +126,7 @@ export class CsrDevPatcher {
|
|
|
124
126
|
if (changed.size === 0) {
|
|
125
127
|
//? No importer moved (none compiles), so the restamped barrels already hold.
|
|
126
128
|
if (restamped) graph.barrels = { ...graph.barrels, ...restamped };
|
|
129
|
+
if (routeTable !== undefined) graph.routeTable = routeTable;
|
|
127
130
|
if (carried.length === 0) return { kind: "unchanged" };
|
|
128
131
|
return await this.#retryCarried(context, state, carried, { announce, allowWholeAppBuilds });
|
|
129
132
|
}
|
|
@@ -161,12 +164,7 @@ export class CsrDevPatcher {
|
|
|
161
164
|
await writer.writeJson("graph.json", graph);
|
|
162
165
|
throw error;
|
|
163
166
|
}
|
|
164
|
-
const refusal =
|
|
165
|
-
result.refusedVendors.length > 0
|
|
166
|
-
? "an npm module joined the graph"
|
|
167
|
-
: result.unresolved.length > 0
|
|
168
|
-
? `a new import needs the resolution build: ${result.unresolved[0]}`
|
|
169
|
-
: null;
|
|
167
|
+
const refusal = CsrDevPatcher.#refusalOf(result);
|
|
170
168
|
if (refusal) {
|
|
171
169
|
//? The entry files are already rewritten, so the worker would see them unchanged and patch a moved route table:
|
|
172
170
|
//? a graph whose entries name none makes it reload instead.
|
|
@@ -192,6 +190,7 @@ export class CsrDevPatcher {
|
|
|
192
190
|
}
|
|
193
191
|
graph.pending = graph.pending.filter((id) => !orphans.includes(id));
|
|
194
192
|
this.#bundler.merge(graph, resolver, compiled, { ...restamped, ...result.barrels });
|
|
193
|
+
if (routeTable !== undefined) graph.routeTable = routeTable;
|
|
195
194
|
if (!this.#resident) delete graph.failed;
|
|
196
195
|
for (const module of compiled) this.#delegated.delete(module.id);
|
|
197
196
|
graph.entries = entryIds;
|
|
@@ -261,6 +260,11 @@ export class CsrDevPatcher {
|
|
|
261
260
|
return retried.kind === "delegate" ? retried : outcome;
|
|
262
261
|
}
|
|
263
262
|
|
|
263
|
+
static #refusalOf(result: CsrDevCompileResult): string | null {
|
|
264
|
+
if (result.refusedVendors.length > 0) return "an npm module joined the graph";
|
|
265
|
+
return result.unresolved.length > 0 ? `a new import needs the resolution build: ${result.unresolved[0]}` : null;
|
|
266
|
+
}
|
|
267
|
+
|
|
264
268
|
//? Compiled apart from the save: a new root that failed to compile, and that no module imports, must not fail every
|
|
265
269
|
//? later save's round with it. Its error still reaches the overlay (this throws after the save's patch went out).
|
|
266
270
|
async #retryCarried(
|
|
@@ -540,6 +544,28 @@ export class CsrDevPatcher {
|
|
|
540
544
|
return barrels;
|
|
541
545
|
}
|
|
542
546
|
|
|
547
|
+
//? A store, signal or constant save can move a name to another module, or make one known: a module whose own text
|
|
548
|
+
//? did not change may then need other pieces. Each module the route runtime rewrote is rewritten again, and compiles
|
|
549
|
+
//? when it comes out different. The digest is recorded like a barrel stamp, with the compile that follows.
|
|
550
|
+
async #rerouted(context: CsrDevContext, graph: CsrDevGraph, changed: Set<string>): Promise<string | undefined> {
|
|
551
|
+
const { routeRuntime } = context;
|
|
552
|
+
if (!routeRuntime || graph.routeTable === routeRuntime.digest) return undefined;
|
|
553
|
+
const { paths } = this.#bundler;
|
|
554
|
+
for (const [id, module] of Object.entries(graph.modules)) {
|
|
555
|
+
if (!module.routed || changed.has(id)) continue;
|
|
556
|
+
const file = paths.fileOf(id);
|
|
557
|
+
if (!fs.existsSync(file)) continue;
|
|
558
|
+
if (routeRuntime.transform(await Bun.file(file).text(), file)?.stamp !== module.routed) changed.add(id);
|
|
559
|
+
}
|
|
560
|
+
return routeRuntime.digest;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
static #extendingAppStores(context: CsrDevContext, graph: CsrDevGraph, changed: Set<string>): string[] {
|
|
564
|
+
const { routeRuntime } = context;
|
|
565
|
+
if (!routeRuntime) return [];
|
|
566
|
+
return [...changed].flatMap((id) => routeRuntime.appStoresAfter(id, (module) => graph.modules[module]?.deps));
|
|
567
|
+
}
|
|
568
|
+
|
|
543
569
|
static #remember(code: CsrDevCode, compiled: CsrDevCompileResult["modules"]) {
|
|
544
570
|
for (const module of compiled) {
|
|
545
571
|
if (module.vendor) continue;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PageEntry } from "../artifact/implicitRootLayout";
|
|
2
|
+
import type { DevRouteRuntime } from "./devRouteRuntime";
|
|
2
3
|
|
|
3
4
|
export interface CsrDevNarrowedImport {
|
|
4
5
|
names: string[];
|
|
@@ -19,6 +20,7 @@ export interface CsrDevGraphModule {
|
|
|
19
20
|
deps: string[];
|
|
20
21
|
helpers?: string;
|
|
21
22
|
narrowed?: Record<string, CsrDevNarrowedImport>;
|
|
23
|
+
routed?: string;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export interface CsrDevGraph {
|
|
@@ -36,6 +38,8 @@ export interface CsrDevGraph {
|
|
|
36
38
|
/** The stamp of the signal and dictionary sources the last whole build inlined; see `RegistryMetadataFingerprint`. */
|
|
37
39
|
metadata?: string;
|
|
38
40
|
barrels?: Record<string, CsrDevBarrelStamp>;
|
|
41
|
+
/** The `DevRouteRuntime` digest every module's `routed` stamp was taken under. */
|
|
42
|
+
routeTable?: string;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
export interface CsrDevContext {
|
|
@@ -50,6 +54,10 @@ export interface CsrDevContext {
|
|
|
50
54
|
/** Bare specifiers the page already loads (an SSR page's import map): required as `vendor:<specifier>`, not compiled. */
|
|
51
55
|
externals?: readonly string[];
|
|
52
56
|
barrelImports?: readonly string[];
|
|
57
|
+
/** What `akanjs/ui/modelRuntime` holds instead of its empty source, when the bootstrap loads only the page runtime. */
|
|
58
|
+
modelRuntime?: string;
|
|
59
|
+
/** Where pages load per module: what each client module imports, as the production build decides it. */
|
|
60
|
+
routeRuntime?: DevRouteRuntime;
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
export interface CsrDevSharedHelpers {
|
|
@@ -68,6 +76,7 @@ export interface CsrDevCompiledModule {
|
|
|
68
76
|
mtimeMs: number;
|
|
69
77
|
hash: string;
|
|
70
78
|
narrowed?: Record<string, CsrDevNarrowedImport>;
|
|
79
|
+
routed?: string;
|
|
71
80
|
}
|
|
72
81
|
|
|
73
82
|
/** Module factories by id and helper definitions by hash, kept in memory so app.js is written without a pass over the disk. */
|