@akanjs/devkit 3.3.0-rc.8 → 3.3.0-rc.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEV_RUNTIME_KNOBS.md +1 -1
- package/README.ko.md +7 -1
- package/README.md +7 -1
- package/akanApp/akanApp.host.test.ts +3 -1
- package/akanApp/akanApp.host.ts +26 -2
- package/akanApp/akanApp.host.wiring.test.ts +111 -0
- package/akanApp/devHostPolicy.ts +5 -0
- package/akanApp/devMetadataRestart.ts +93 -0
- package/akanApp/devRecycler.test.ts +2 -0
- package/akanApp/devRecycler.ts +9 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +307 -18
- package/akanConfig/akanConfig.ts +104 -210
- package/akanConfig/akanPreview.test.ts +28 -0
- package/akanConfig/akanPreview.ts +24 -0
- package/akanConfig/akanWorkers.test.ts +73 -0
- package/akanConfig/akanWorkers.ts +61 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +233 -0
- package/akanConfig/index.ts +3 -0
- package/akanConfig/nativeFileSettings.ts +33 -0
- package/akanConfig/types.ts +4 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +51 -1
- package/app/applicationBuildReporter.ts +6 -1
- package/app/applicationBuildRunner.test.ts +436 -5
- package/app/applicationBuildRunner.ts +237 -37
- package/app/clientServerBoundary.test.ts +115 -0
- package/app/clientServerBoundary.ts +89 -0
- package/app/dictionarySizes.test.ts +40 -0
- package/app/dictionarySizes.ts +95 -0
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/implicitRootLayout.test.ts +19 -0
- package/artifact/implicitRootLayout.ts +16 -2
- package/artifact/implicitRootLayoutRender.test.tsx +7 -2
- package/artifact/storeBootstrap.test.ts +28 -0
- package/artifact/storeBootstrap.ts +64 -8
- package/biome.base.json +7 -5
- package/codeAgent/index.ts +1 -34
- package/codeAgent/skills/akan-agent/SKILL.md +2 -2
- package/codeAgent/skills/akan-debug/SKILL.md +11 -5
- package/codeAgent/skills/akan-deps/SKILL.md +10 -3
- 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 +65 -30
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-runtime/SKILL.md +21 -1
- package/codeAgent/skills/akan-service/SKILL.md +21 -20
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +16 -21
- package/codeAgent/skills/akan-validate/SKILL.md +8 -2
- package/commandDecorators/command.ts +28 -12
- package/commandDecorators/commandBuilder.ts +8 -1
- package/commandDecorators/commandDecorators.test.ts +70 -3
- package/commandDecorators/helpFormatter.ts +14 -12
- package/context/akanContext.ts +2 -0
- package/csrE2e/devHmrLatency.e2e.test.ts +1 -1
- package/csrE2e/ssrRegistry.e2e.test.ts +108 -22
- package/docs/apiReference.test.ts +25 -0
- package/docs/apiReference.ts +38 -5
- package/docs/declarationReader.ts +30 -0
- package/executors/appExecutor.ts +26 -12
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1690 -0
- package/executors/executor.ts +14 -6
- package/executors/executors.test.ts +155 -1
- package/executors/pkgExecutor.ts +2 -1
- package/executors/sysExecutor.ts +112 -8
- package/executors/workspaceExecutor.ts +7 -4
- package/frontendBuild/allRoutesBuilder.ts +99 -7
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +684 -6
- package/frontendBuild/clientBuildTypes.ts +26 -0
- package/frontendBuild/clientEntriesBundler.test.ts +79 -0
- package/frontendBuild/clientEntriesBundler.ts +70 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
- package/frontendBuild/clientEntryDiscovery.ts +134 -12
- package/frontendBuild/clientEntryGroups.test.ts +77 -0
- package/frontendBuild/clientEntryGroups.ts +61 -0
- package/frontendBuild/csrDevArtifactWriter.ts +2 -2
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.test.ts +4 -5
- package/frontendBuild/csrDevPatcher.ts +37 -11
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/cssCandidateCache.test.ts +12 -0
- package/frontendBuild/cssCandidateCache.ts +5 -3
- package/frontendBuild/cssCompiler.test.ts +14 -0
- package/frontendBuild/devBarrelImports.test.ts +124 -0
- package/frontendBuild/devBarrelImports.ts +81 -21
- package/frontendBuild/devChangePlanner.ts +0 -1
- package/frontendBuild/devRegistryBundler.ts +4 -2
- package/frontendBuild/devRouteRuntime.test.ts +492 -0
- package/frontendBuild/devRouteRuntime.ts +167 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +408 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +185 -17
- package/frontendBuild/dictionaryConstants.ts +136 -0
- package/frontendBuild/dictionaryOwnParts.ts +88 -0
- package/frontendBuild/dictionaryPackState.ts +131 -0
- package/frontendBuild/dictionaryPacking.test.ts +207 -0
- package/frontendBuild/dictionaryPacking.ts +160 -0
- package/frontendBuild/dictionaryRoutes.test.ts +251 -0
- package/frontendBuild/dictionaryRoutes.ts +227 -0
- package/frontendBuild/frontendBuild.test.ts +5 -3
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +78 -2
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +34 -8
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/renderedEntries.ts +144 -0
- package/frontendBuild/routeClientBuilder.ts +407 -43
- package/frontendBuild/routeHandoffs.test.ts +155 -0
- package/frontendBuild/routeHandoffs.ts +117 -0
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +280 -0
- package/frontendBuild/routeRuntimeCheck.ts +248 -0
- package/frontendBuild/routeRuntimePays.test.ts +143 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/ssrBaseArtifactBuilder.ts +30 -35
- 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.test.ts +47 -3
- package/frontendBuild/vendorNarrowing.ts +38 -2
- package/frontendBuild/vendorSpecifiers.ts +2 -0
- package/frontendBuild/workerChunks.ts +61 -0
- package/guideline/agentAssets.ts +1 -1
- package/incrementalBuilder/buildBatch.proc.ts +27 -1
- package/incrementalBuilder/buildBatchProtocol.ts +1 -0
- package/incrementalBuilder/builderWatch.ts +11 -1
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +159 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +31 -3
- package/incrementalBuilder/pendingMetadata.test.ts +27 -0
- package/incrementalBuilder/pendingMetadata.ts +19 -0
- package/integration/devResourceProbe.ts +14 -0
- package/integration/devStability.integration.test.ts +46 -21
- package/integration/renderAdmissionSoak.ts +6 -0
- package/integration/ssrMemoryProbe.ts +5 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/bad.tsx +11 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/bad.tsx +34 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/good.tsx +2 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/bad.tsx +10 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/bad.tsx +22 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/good.tsx +2 -0
- package/lint/biomeBase.test.ts +50 -3
- package/lint/biomeBase.ts +1 -1
- package/lint/biomeCheck.test.ts +162 -0
- package/lint/biomeCheck.ts +131 -0
- package/lint/linter.test.ts +16 -11
- package/lint/no-arbitrary-color.grit +5 -0
- package/lint/no-conditional-st-hook.grit +5 -1
- package/lint/no-daisyui-legacy-class.grit +8 -0
- package/lint/no-document-cookie.grit +3 -0
- package/lint/no-inline-color.grit +10 -2
- package/lint/no-oversized-list-limit.grit +2 -0
- package/lint/no-raw-palette-class.grit +7 -0
- package/lint/no-unpublished-form-setter.grit +2 -0
- package/lint/no-web-only-api-outside-webkit.grit +5 -0
- package/lint/no-web-storage.grit +3 -0
- package/lint/withheldEnvFile.ts +17 -0
- package/mobile/desktopBin.ts +2 -2
- package/mobile/desktopServerStage.test.ts +18 -3
- package/mobile/desktopServerStage.ts +7 -2
- package/mobile/nativeApp.test.ts +13 -5
- package/mobile/nativeApp.ts +9 -4
- package/mobile/nativeConfig.test.ts +17 -1
- package/mobile/nativeConfig.ts +11 -6
- package/mobile/nativePublicEnv.ts +1 -1
- package/mobile/serverBundleSecrets.ts +43 -0
- package/package.json +11 -12
- package/packageExports.test.ts +3 -2
- package/preview/PreviewView.ts +43 -6
- package/preview/previewView.test.ts +31 -0
- package/quality/dictionaryKeys.test.ts +356 -0
- package/quality/dictionaryKeys.ts +488 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +105 -0
- package/quality/qualityScanner.ts +1 -1
- package/quality/routeRuntimeReport.test.ts +91 -0
- package/quality/routeRuntimeReport.ts +121 -0
- package/quality/runtimeConstantFacts.ts +71 -0
- package/quality/runtimeConstantGraph.ts +123 -0
- package/quality/runtimeConstantReads.ts +116 -0
- package/quality/runtimeImports.test.ts +190 -0
- package/quality/runtimeImports.ts +233 -0
- package/quality/runtimeKeyNames.ts +46 -0
- package/quality/runtimeLexicalScope.ts +132 -0
- package/quality/runtimeMarkerReach.test.ts +84 -0
- package/quality/runtimeMarkerReach.ts +255 -0
- package/quality/runtimeModuleFacts.ts +290 -0
- package/quality/runtimeNameTable.test.ts +172 -0
- package/quality/runtimeNameTable.ts +477 -0
- package/quality/runtimeScopeReport.test.ts +123 -0
- package/quality/runtimeScopeReport.ts +167 -0
- package/quality/runtimeSliceKey.ts +269 -0
- package/quality/runtimeSyntax.ts +133 -0
- package/quality/runtimeTable.fixture.ts +189 -0
- package/quality/runtimeUse.test.ts +737 -0
- package/quality/runtimeUse.ts +696 -0
- package/quality/runtimeUseReadable.test.ts +393 -0
- package/quality/ssrScanner.test.ts +13 -0
- package/quality/ssrScanner.ts +1 -1
- package/quality/translatorBindings.ts +187 -0
- package/test/fakeTerminal.ts +45 -0
- package/test/secretEnvFixture.ts +134 -0
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +24 -2
- package/transforms/barrelImportsPlugin.ts +121 -32
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +308 -0
- package/transforms/frameworkObjects.fixture.tsx +58 -0
- package/transforms/frameworkObjects.test.ts +54 -0
- package/transforms/modelReference.test.ts +196 -0
- package/transforms/modelReference.ts +39 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +633 -0
- package/transforms/objectMemberImports.ts +571 -0
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +307 -0
- package/transforms/objectModule.ts +482 -0
- package/transforms/objectModuleAnalyzer.ts +104 -0
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
- package/transforms/reactIconImports.test.ts +231 -0
- package/transforms/reactIconImports.ts +325 -0
- package/transforms/routeConstants.ts +122 -0
- package/transforms/routeRuntime.test.ts +835 -0
- package/transforms/routeRuntime.ts +703 -0
- package/transforms/routeRuntimeEdits.ts +92 -0
- package/transforms/routeRuntimeGraph.ts +66 -0
- package/transforms/routeRuntimePlan.ts +244 -0
- package/transforms/secretEnvDeclaration.test.ts +211 -0
- package/transforms/secretEnvDeclaration.ts +219 -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 +152 -0
- package/transforms/secretEnvSource.ts +197 -0
- package/transforms/serverEnvEvaluationError.ts +11 -0
- package/transforms/serverEnvPlugin.test.ts +155 -1
- package/transforms/serverEnvPlugin.ts +123 -19
- package/transforms/serverEnvProfile.test.ts +129 -2
- package/transforms/serverEnvProfile.ts +133 -43
- package/transforms/serverEnvSecrets.test.ts +268 -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 +177 -0
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/tunnel/createTunnel.ts +5 -1
- package/tunnel/ssh2Module.test.ts +37 -0
- package/tunnel/ssh2Module.ts +28 -0
- package/tunnel/sshTunnel.ts +4 -2
- package/typecheck/projectTypecheck.test.ts +32 -1
- package/typecheck/projectTypecheck.ts +3 -3
- package/typecheck/typeChecker.test.ts +49 -0
- package/typecheck/typeChecker.ts +9 -7
- package/typecheck/withheldDiagnostics.ts +41 -0
- package/util/devInstallCommand.ts +7 -0
- package/util/jsonFormatter.test.ts +13 -9
- package/workflow/executor.ts +6 -2
- package/workflow/index.ts +1 -0
- package/workflow/plan.ts +36 -7
- package/workflow/types.ts +1 -1
- package/workflow/uiTarget.ts +50 -0
- package/workspace/envSeed.test.ts +30 -1
- package/workspace/envSeed.ts +30 -1
- package/workspace/mainCheckout.test.ts +55 -0
- package/workspace/mainCheckout.ts +38 -0
- package/workspace/scanInfo.test.ts +39 -0
- package/workspace/scanInfo.ts +55 -6
- package/workspace/slicePlanner.ts +2 -1
- package/workspace/workspaceLayout.test.ts +22 -1
- package/workspace/workspaceLayout.ts +3 -3
- package/CHANGELOG.md +0 -2903
- package/codeAgent/agent/AkanCodeCompaction.ts +0 -49
- package/codeAgent/agent/AkanCodeServices.ts +0 -122
- package/codeAgent/agent/AkanContextFiles.ts +0 -94
- package/codeAgent/agent/CodeAgent.ts +0 -626
- package/codeAgent/agent/CodeAgentAllowed.ts +0 -35
- package/codeAgent/agent/CodeAgentAsks.ts +0 -66
- package/codeAgent/agent/CodeAgentCommandLine.ts +0 -372
- package/codeAgent/agent/CodeAgentEventMapper.ts +0 -225
- package/codeAgent/agent/CodeAgentGate.ts +0 -258
- package/codeAgent/agent/CodeAgentProxy.ts +0 -83
- package/codeAgent/agent/CodeAgentRpcHost.ts +0 -109
- package/codeAgent/agent/CodeAgentRpcListener.ts +0 -195
- package/codeAgent/agent/CodeAgentStreamPrinter.ts +0 -86
- package/codeAgent/agent/CodeAgentSuspended.ts +0 -85
- package/codeAgent/agent/CodeAgentUi.ts +0 -47
- package/codeAgent/agent/CodeMailbox.ts +0 -148
- package/codeAgent/agent/CodeSessionFork.ts +0 -56
- package/codeAgent/agent/CodeSessionIndex.ts +0 -78
- package/codeAgent/agent/SubagentPool.ts +0 -219
- package/codeAgent/agent/WorkspaceTrust.ts +0 -200
- package/codeAgent/agent/akanCodeCompaction.test.ts +0 -36
- package/codeAgent/agent/akanCodeModel.ts +0 -47
- package/codeAgent/agent/akanCodeServices.test.ts +0 -54
- package/codeAgent/agent/akanContextFiles.test.ts +0 -116
- package/codeAgent/agent/akanSystemPrompt.ts +0 -50
- package/codeAgent/agent/codeAgent.test.ts +0 -337
- package/codeAgent/agent/codeAgentCommandLine.test.ts +0 -212
- package/codeAgent/agent/codeAgentContract.test.ts +0 -755
- package/codeAgent/agent/codeAgentHosts.ts +0 -30
- package/codeAgent/agent/codeAgentProxy.test.ts +0 -126
- package/codeAgent/agent/codeAgentRpcListener.test.ts +0 -215
- package/codeAgent/agent/codeAgentSuspended.test.ts +0 -47
- package/codeAgent/agent/codeMailbox.test.ts +0 -76
- package/codeAgent/agent/codeSessionFork.test.ts +0 -58
- package/codeAgent/agent/codeSessionIndex.test.ts +0 -77
- package/codeAgent/agent/consoleToStderr.ts +0 -8
- package/codeAgent/agent/jsonLines.ts +0 -29
- package/codeAgent/agent/stringArg.ts +0 -5
- package/codeAgent/feedback/DevLogFeedback.ts +0 -82
- package/codeAgent/feedback/PreviewFeedback.ts +0 -46
- package/codeAgent/feedback/TurnFeedback.ts +0 -98
- package/codeAgent/feedback/VerifyFeedback.ts +0 -39
- package/codeAgent/tools/AkanCli.ts +0 -51
- package/codeAgent/tools/AkanCodePlugins.ts +0 -142
- package/codeAgent/tools/AkanEditScope.ts +0 -53
- package/codeAgent/tools/AkanEnvKeys.ts +0 -50
- package/codeAgent/tools/AkanToolPack.ts +0 -86
- package/codeAgent/tools/AkanVerifier.ts +0 -30
- package/codeAgent/tools/AskToolPack.ts +0 -99
- package/codeAgent/tools/DeepSeekWeb.ts +0 -127
- package/codeAgent/tools/MailToolPack.ts +0 -76
- package/codeAgent/tools/McpClient.ts +0 -209
- package/codeAgent/tools/McpOAuth.ts +0 -233
- package/codeAgent/tools/McpOAuthLoopback.ts +0 -64
- package/codeAgent/tools/McpServerConfig.ts +0 -131
- package/codeAgent/tools/McpSignIn.ts +0 -158
- package/codeAgent/tools/McpTokenStore.ts +0 -128
- package/codeAgent/tools/McpToolPack.ts +0 -206
- package/codeAgent/tools/SessionToolPack.ts +0 -91
- package/codeAgent/tools/WebToolPack.ts +0 -315
- package/codeAgent/tools/akanCodePlugins.test.ts +0 -954
- package/codeAgent/tools/askToolPack.test.ts +0 -116
- package/codeAgent/tools/deepSeekWeb.test.ts +0 -113
- package/codeAgent/tools/mcpOAuth.test.ts +0 -648
- package/codeAgent/tools/mcpServerConfig.test.ts +0 -341
- package/piDependencyPins.test.ts +0 -45
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ObjectModule } from "./objectModule";
|
|
3
|
+
|
|
4
|
+
export interface ReachedImport {
|
|
5
|
+
specifier: string;
|
|
6
|
+
/** null: the whole module (a namespace handed on, a bare or dynamic import, a name the file re-exports as `*`). */
|
|
7
|
+
names: string[] | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ExportReached =
|
|
11
|
+
| { known: true; imports: ReachedImport[] }
|
|
12
|
+
/** The file neither declares nor names it: one of its `export *` targets may. */
|
|
13
|
+
| { known: false; stars: string[] };
|
|
14
|
+
|
|
15
|
+
interface ImportedName {
|
|
16
|
+
specifier: string;
|
|
17
|
+
/** `*` for a namespace import, `default` for a default import. */
|
|
18
|
+
imported: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//? null: the name is read as a value of its own. A set: only these members of it are read (`View.General`).
|
|
22
|
+
type Reads = Map<string, Set<string> | null>;
|
|
23
|
+
|
|
24
|
+
interface Unit {
|
|
25
|
+
reads: Reads;
|
|
26
|
+
dynamic: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const CJS_EXPORT_RE = /\b(?:module\.exports|exports\.[A-Za-z_$])/;
|
|
30
|
+
|
|
31
|
+
//* What an importer of one export of a server module can reach: the imports read by that export's declaration and by
|
|
32
|
+
//* the top-level declarations it reads in turn. A type reaches nothing. A statement that is not a declaration runs for
|
|
33
|
+
//* every importer, so what it reads is reached always. Names are matched by text, so a shadowed name counts as read:
|
|
34
|
+
//* the answer can hold more than the export uses, never less.
|
|
35
|
+
export class ExportReach {
|
|
36
|
+
readonly stars: string[] = [];
|
|
37
|
+
readonly #imports = new Map<string, ImportedName>();
|
|
38
|
+
readonly #bare: string[] = [];
|
|
39
|
+
readonly #units = new Map<string, Unit[]>();
|
|
40
|
+
readonly #effects: Unit[] = [];
|
|
41
|
+
readonly #exported = new Map<string, string>();
|
|
42
|
+
readonly #reexports = new Map<string, ImportedName>();
|
|
43
|
+
readonly #types = new Set<string>();
|
|
44
|
+
|
|
45
|
+
//? null for a CommonJS module: what it exports is assigned at run time.
|
|
46
|
+
static read(source: string, file: string): ExportReach | null {
|
|
47
|
+
const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ObjectModule.scriptKindOf(file));
|
|
48
|
+
const esm = sourceFile.statements.some(
|
|
49
|
+
(statement) =>
|
|
50
|
+
ts.isImportDeclaration(statement) ||
|
|
51
|
+
ts.isExportDeclaration(statement) ||
|
|
52
|
+
ts.isExportAssignment(statement) ||
|
|
53
|
+
ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword),
|
|
54
|
+
);
|
|
55
|
+
if (!esm && CJS_EXPORT_RE.test(source)) return null;
|
|
56
|
+
const reach = new ExportReach();
|
|
57
|
+
for (const statement of sourceFile.statements) reach.#collect(statement);
|
|
58
|
+
return reach;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
of(name: string): ExportReached {
|
|
62
|
+
const reexport = this.#reexports.get(name);
|
|
63
|
+
if (reexport) return { known: true, imports: [ExportReach.#whole(reexport)] };
|
|
64
|
+
const local = this.#exported.get(name);
|
|
65
|
+
if (local !== undefined) return { known: true, imports: this.#reached([ExportReach.#reading(local)], []) };
|
|
66
|
+
if (this.#types.has(name)) return { known: true, imports: [] };
|
|
67
|
+
return { known: false, stars: this.stars };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
always(): ReachedImport[] {
|
|
71
|
+
return this.#reached(this.#effects, this.#bare);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
whole(): ReachedImport[] {
|
|
75
|
+
const reexports = [...this.#reexports.values(), ...this.stars.map((specifier) => ({ specifier, imported: "*" }))];
|
|
76
|
+
return ExportReach.#merged([
|
|
77
|
+
...this.#reached([...this.#effects, ...[...this.#units.values()].flat()], this.#bare),
|
|
78
|
+
...reexports.map(ExportReach.#whole),
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static #whole({ specifier, imported }: ImportedName): ReachedImport {
|
|
83
|
+
return { specifier, names: imported === "*" ? null : [imported] };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static #reading(name: string): Unit {
|
|
87
|
+
return { reads: new Map([[name, null]]), dynamic: [] };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#reached(starts: Unit[], bare: string[]): ReachedImport[] {
|
|
91
|
+
const used = new Map<string, Set<string> | null>();
|
|
92
|
+
const dynamic = new Set(bare);
|
|
93
|
+
const seen = new Set<Unit>();
|
|
94
|
+
const queue = [...starts];
|
|
95
|
+
for (const unit of queue) {
|
|
96
|
+
if (seen.has(unit)) continue;
|
|
97
|
+
seen.add(unit);
|
|
98
|
+
for (const specifier of unit.dynamic) dynamic.add(specifier);
|
|
99
|
+
for (const [name, members] of unit.reads) {
|
|
100
|
+
if (this.#imports.has(name)) {
|
|
101
|
+
const known = used.get(name);
|
|
102
|
+
if (members === null || known === null) used.set(name, null);
|
|
103
|
+
else used.set(name, new Set([...(known ?? []), ...members]));
|
|
104
|
+
}
|
|
105
|
+
queue.push(...(this.#units.get(name) ?? []));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const reached: ReachedImport[] = [...dynamic].map((specifier) => ({ specifier, names: null }));
|
|
109
|
+
for (const [local, members] of used) {
|
|
110
|
+
const { specifier, imported } = this.#imports.get(local) as ImportedName;
|
|
111
|
+
if (imported !== "*") reached.push({ specifier, names: [imported] });
|
|
112
|
+
else reached.push({ specifier, names: members ? [...members] : null });
|
|
113
|
+
}
|
|
114
|
+
return ExportReach.#merged(reached);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static #merged(reached: ReachedImport[]): ReachedImport[] {
|
|
118
|
+
const bySpecifier = new Map<string, Set<string> | null>();
|
|
119
|
+
for (const { specifier, names } of reached) {
|
|
120
|
+
const known = bySpecifier.get(specifier);
|
|
121
|
+
if (names === null || known === null) bySpecifier.set(specifier, null);
|
|
122
|
+
else bySpecifier.set(specifier, new Set([...(known ?? []), ...names]));
|
|
123
|
+
}
|
|
124
|
+
return [...bySpecifier].map(([specifier, names]) => ({ specifier, names: names ? [...names].sort() : null }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#collect(statement: ts.Statement) {
|
|
128
|
+
if (ts.isImportDeclaration(statement)) return this.#collectImport(statement);
|
|
129
|
+
if (ts.isExportDeclaration(statement)) return this.#collectExportList(statement);
|
|
130
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) {
|
|
131
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword)) this.#types.add(statement.name.text);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.DeclareKeyword) || ts.isEmptyStatement(statement)) return;
|
|
135
|
+
if (ts.isImportEqualsDeclaration(statement)) return this.#collectImportEquals(statement);
|
|
136
|
+
if (ts.isExportAssignment(statement)) {
|
|
137
|
+
if (statement.isExportEquals) return this.#effects.push(ExportReach.#unitOf(statement));
|
|
138
|
+
this.#exported.set("default", "default");
|
|
139
|
+
return this.#declare("default", ExportReach.#unitOf(statement.expression));
|
|
140
|
+
}
|
|
141
|
+
if (ts.isVariableStatement(statement)) return this.#collectVariables(statement);
|
|
142
|
+
if (
|
|
143
|
+
ts.isFunctionDeclaration(statement) ||
|
|
144
|
+
ts.isClassDeclaration(statement) ||
|
|
145
|
+
ts.isEnumDeclaration(statement) ||
|
|
146
|
+
ts.isModuleDeclaration(statement)
|
|
147
|
+
) {
|
|
148
|
+
if (ts.isFunctionDeclaration(statement) && !statement.body) return;
|
|
149
|
+
const name = statement.name && !ts.isStringLiteral(statement.name) ? statement.name.getText() : "default";
|
|
150
|
+
this.#declare(name, ExportReach.#unitOf(statement));
|
|
151
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.DefaultKeyword)) this.#exported.set("default", name);
|
|
152
|
+
else if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword)) this.#exported.set(name, name);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const unit = ExportReach.#unitOf(statement);
|
|
156
|
+
const owner = ts.isExpressionStatement(statement) ? this.#ownerOf(statement.expression) : null;
|
|
157
|
+
if (owner) this.#declare(owner, unit);
|
|
158
|
+
else this.#effects.push(unit);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//? `Card.Header = Header` and `Object.assign(Card, { Header })` change only `Card`, so they are read with it.
|
|
162
|
+
#ownerOf(expression: ts.Expression): string | null {
|
|
163
|
+
let target: ts.Expression | undefined;
|
|
164
|
+
if (ts.isBinaryExpression(expression) && expression.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
165
|
+
target = expression.left;
|
|
166
|
+
while (ts.isPropertyAccessExpression(target)) target = target.expression;
|
|
167
|
+
if (target === expression.left) return null;
|
|
168
|
+
} else if (
|
|
169
|
+
ts.isCallExpression(expression) &&
|
|
170
|
+
ts.isPropertyAccessExpression(expression.expression) &&
|
|
171
|
+
expression.expression.name.text === "assign" &&
|
|
172
|
+
ts.isIdentifier(expression.expression.expression) &&
|
|
173
|
+
expression.expression.expression.text === "Object"
|
|
174
|
+
)
|
|
175
|
+
[target] = expression.arguments;
|
|
176
|
+
return target && ts.isIdentifier(target) && this.#units.has(target.text) ? target.text : null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#declare(name: string, unit: Unit) {
|
|
180
|
+
this.#units.set(name, [...(this.#units.get(name) ?? []), unit]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
#collectImport(statement: ts.ImportDeclaration) {
|
|
184
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier)) return;
|
|
185
|
+
const specifier = statement.moduleSpecifier.text;
|
|
186
|
+
const clause = statement.importClause;
|
|
187
|
+
if (!clause) return this.#bare.push(specifier);
|
|
188
|
+
if (clause.phaseModifier === ts.SyntaxKind.TypeKeyword) return;
|
|
189
|
+
if (clause.name) this.#imports.set(clause.name.text, { specifier, imported: "default" });
|
|
190
|
+
const bindings = clause.namedBindings;
|
|
191
|
+
if (!bindings) return;
|
|
192
|
+
if (ts.isNamespaceImport(bindings)) return this.#imports.set(bindings.name.text, { specifier, imported: "*" });
|
|
193
|
+
for (const element of bindings.elements)
|
|
194
|
+
if (!element.isTypeOnly)
|
|
195
|
+
this.#imports.set(element.name.text, { specifier, imported: (element.propertyName ?? element.name).text });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#collectImportEquals(statement: ts.ImportEqualsDeclaration) {
|
|
199
|
+
if (statement.isTypeOnly) return;
|
|
200
|
+
const reference = statement.moduleReference;
|
|
201
|
+
if (ts.isExternalModuleReference(reference) && ts.isStringLiteral(reference.expression))
|
|
202
|
+
this.#imports.set(statement.name.text, { specifier: reference.expression.text, imported: "*" });
|
|
203
|
+
else this.#declare(statement.name.text, ExportReach.#unitOf(statement));
|
|
204
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword))
|
|
205
|
+
this.#exported.set(statement.name.text, statement.name.text);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#collectExportList(statement: ts.ExportDeclaration) {
|
|
209
|
+
const from = statement.moduleSpecifier;
|
|
210
|
+
const specifier = from && ts.isStringLiteral(from) ? from.text : null;
|
|
211
|
+
const clause = statement.exportClause;
|
|
212
|
+
if (!clause) {
|
|
213
|
+
if (specifier && !statement.isTypeOnly) this.stars.push(specifier);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (ts.isNamespaceExport(clause)) {
|
|
217
|
+
if (statement.isTypeOnly) this.#types.add(clause.name.text);
|
|
218
|
+
else if (specifier) this.#reexports.set(clause.name.text, { specifier, imported: "*" });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
for (const element of clause.elements) {
|
|
222
|
+
const exported = element.name.text;
|
|
223
|
+
const local = (element.propertyName ?? element.name).text;
|
|
224
|
+
if (statement.isTypeOnly || element.isTypeOnly) this.#types.add(exported);
|
|
225
|
+
else if (specifier) this.#reexports.set(exported, { specifier, imported: local });
|
|
226
|
+
else this.#exported.set(exported, local);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#collectVariables(statement: ts.VariableStatement) {
|
|
231
|
+
const exported = ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword);
|
|
232
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
233
|
+
const unit = ExportReach.#unitOf(declaration);
|
|
234
|
+
for (const name of ExportReach.#boundNames(declaration.name)) {
|
|
235
|
+
this.#declare(name, unit);
|
|
236
|
+
if (exported) this.#exported.set(name, name);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static #boundNames(name: ts.BindingName): string[] {
|
|
242
|
+
if (ts.isIdentifier(name)) return [name.text];
|
|
243
|
+
return name.elements.flatMap((element) =>
|
|
244
|
+
ts.isOmittedExpression(element) ? [] : ExportReach.#boundNames(element.name),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
static namesReadBy(root: ts.Node): Set<string> {
|
|
249
|
+
return new Set(ExportReach.#unitOf(root).reads.keys());
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static #unitOf(root: ts.Node): Unit {
|
|
253
|
+
const unit: Unit = { reads: new Map(), dynamic: [] };
|
|
254
|
+
const read = (name: string, member: string | null) => {
|
|
255
|
+
const known = unit.reads.get(name);
|
|
256
|
+
if (member === null || known === null) unit.reads.set(name, null);
|
|
257
|
+
else unit.reads.set(name, (known ?? new Set<string>()).add(member));
|
|
258
|
+
};
|
|
259
|
+
const visit = (node: ts.Node): void => {
|
|
260
|
+
if (ts.isExpressionWithTypeArguments(node)) {
|
|
261
|
+
visit(node.expression);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (ts.isTypeNode(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) return;
|
|
265
|
+
if (ts.isIdentifier(node)) {
|
|
266
|
+
read(node.text, null);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
270
|
+
if (ts.isIdentifier(node.expression)) read(node.expression.text, node.name.text);
|
|
271
|
+
else visit(node.expression);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (ts.isCallExpression(node)) {
|
|
275
|
+
const [first] = node.arguments;
|
|
276
|
+
const loads =
|
|
277
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
278
|
+
(ts.isIdentifier(node.expression) && node.expression.text === "require");
|
|
279
|
+
if (loads && first && ts.isStringLiteralLike(first)) unit.dynamic.push(first.text);
|
|
280
|
+
}
|
|
281
|
+
if (ts.isPropertyAssignment(node) || ts.isJsxAttribute(node)) {
|
|
282
|
+
if (ts.isComputedPropertyName(node.name)) visit(node.name);
|
|
283
|
+
if (node.initializer) visit(node.initializer);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (ts.isBindingElement(node)) {
|
|
287
|
+
if (node.propertyName && ts.isComputedPropertyName(node.propertyName)) visit(node.propertyName);
|
|
288
|
+
visit(node.name);
|
|
289
|
+
if (node.initializer) visit(node.initializer);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (
|
|
293
|
+
(ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isAccessor(node)) &&
|
|
294
|
+
!ts.isComputedPropertyName(node.name)
|
|
295
|
+
) {
|
|
296
|
+
node.forEachChild((child) => (child === node.name ? undefined : visit(child)));
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
node.forEachChild(visit);
|
|
300
|
+
};
|
|
301
|
+
visit(root);
|
|
302
|
+
return unit;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
static #has(node: ts.Node, kind: ts.SyntaxKind) {
|
|
306
|
+
return ts.canHaveModifiers(node) && (ts.getModifiers(node) ?? []).some((modifier) => modifier.kind === kind);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { plugin } from "bun";
|
|
3
|
+
import { transformUseClient } from "./rscUseClientTransform";
|
|
4
|
+
|
|
5
|
+
const workspaceRoot = path.resolve(import.meta.dir, "../../../..");
|
|
6
|
+
const ui = path.join(workspaceRoot, "pkgs/akanjs/ui");
|
|
7
|
+
//? The build narrows these barrel imports to one file; each facet index loads react-dom/client, which react-server refuses.
|
|
8
|
+
const narrowed: Record<string, string> = {
|
|
9
|
+
"akanjs/client": path.join(workspaceRoot, "pkgs/akanjs/client/cn.ts"),
|
|
10
|
+
"akanjs/webkit": path.join(workspaceRoot, "pkgs/akanjs/webkit/lazy.tsx"),
|
|
11
|
+
};
|
|
12
|
+
plugin({
|
|
13
|
+
name: "framework-objects-use-client",
|
|
14
|
+
setup(build) {
|
|
15
|
+
build.onLoad({ filter: /[\\/]pkgs[\\/]akanjs[\\/]ui[\\/].*\.tsx?$/ }, async (args) => {
|
|
16
|
+
let source = await Bun.file(args.path).text();
|
|
17
|
+
for (const [specifier, file] of Object.entries(narrowed))
|
|
18
|
+
source = source.replaceAll(`from "${specifier}"`, `from ${JSON.stringify(file)}`);
|
|
19
|
+
return {
|
|
20
|
+
contents: transformUseClient(source, { path: args.path, workspaceRoot }) ?? source,
|
|
21
|
+
loader: args.path.endsWith(".tsx") ? "tsx" : "ts",
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const { renderToReadableStream } = await import("react-server-dom-webpack/server.node");
|
|
28
|
+
const { Field } = await import(path.join(ui, "Field/index.tsx"));
|
|
29
|
+
const { Layout } = await import(path.join(ui, "Layout/index.ts"));
|
|
30
|
+
const { Model } = await import(path.join(ui, "Model/index.ts"));
|
|
31
|
+
const { Dialog } = await import(path.join(ui, "Dialog/index.tsx"));
|
|
32
|
+
|
|
33
|
+
const references = new Set<string>();
|
|
34
|
+
const clientManifest = new Proxy(
|
|
35
|
+
{},
|
|
36
|
+
{
|
|
37
|
+
get: (_, key) => {
|
|
38
|
+
const [id = "", name = ""] = String(key).split("#");
|
|
39
|
+
references.add(String(key));
|
|
40
|
+
return { id, chunks: [], name };
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
const errors: string[] = [];
|
|
45
|
+
const stream = await renderToReadableStream(
|
|
46
|
+
<main>
|
|
47
|
+
<Field label="Name" />
|
|
48
|
+
<Layout.Header>Head</Layout.Header>
|
|
49
|
+
<Model.View>Body</Model.View>
|
|
50
|
+
<Dialog open>
|
|
51
|
+
<Dialog.LegacyModal>Legacy</Dialog.LegacyModal>
|
|
52
|
+
</Dialog>
|
|
53
|
+
</main>,
|
|
54
|
+
clientManifest,
|
|
55
|
+
{ onError: (error: unknown) => void errors.push(String(error)) },
|
|
56
|
+
);
|
|
57
|
+
await new Response(stream).text();
|
|
58
|
+
process.stdout.write(JSON.stringify({ references: [...references].sort(), errors }));
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ObjectMemberImports } from "./objectMemberImports";
|
|
4
|
+
import { hasUseClientDirective } from "./rscUseClientTransform";
|
|
5
|
+
|
|
6
|
+
const ui = path.resolve(import.meta.dir, "../../../akanjs/ui");
|
|
7
|
+
|
|
8
|
+
describe("framework objects on the server", () => {
|
|
9
|
+
test("a server component renders Field, Layout.Header, Model.View and Dialog.LegacyModal, each a client reference", async () => {
|
|
10
|
+
const proc = Bun.spawn(
|
|
11
|
+
[process.execPath, "--conditions", "react-server", `${import.meta.dir}/frameworkObjects.fixture.tsx`],
|
|
12
|
+
{ cwd: import.meta.dir, env: { ...process.env, NODE_ENV: "production" }, stdout: "pipe", stderr: "pipe" },
|
|
13
|
+
);
|
|
14
|
+
const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]);
|
|
15
|
+
expect({ exit: await proc.exited, stderr }).toEqual({ exit: 0, stderr: "" });
|
|
16
|
+
|
|
17
|
+
expect(JSON.parse(stdout)).toEqual({
|
|
18
|
+
references: [
|
|
19
|
+
"pkgs/akanjs/ui/Dialog/LegacyModal.tsx#LegacyModal",
|
|
20
|
+
"pkgs/akanjs/ui/Dialog/Provider.tsx#Provider",
|
|
21
|
+
"pkgs/akanjs/ui/Field/index.tsx#Field",
|
|
22
|
+
"pkgs/akanjs/ui/Layout/Header.tsx#Header",
|
|
23
|
+
"pkgs/akanjs/ui/Model/View.tsx#default",
|
|
24
|
+
],
|
|
25
|
+
errors: [],
|
|
26
|
+
});
|
|
27
|
+
}, 60_000);
|
|
28
|
+
|
|
29
|
+
test("a server file naming Field.Text and Field.Switch imports each from its own client file", async () => {
|
|
30
|
+
const objects = new ObjectMemberImports({ resolve: ObjectMemberImports.resolverOf(async () => null) });
|
|
31
|
+
const result = await objects.rewrite(
|
|
32
|
+
[
|
|
33
|
+
`import { Field } from ${JSON.stringify(path.join(ui, "Field/index.tsx"))};`,
|
|
34
|
+
`export const Page = () => <><Field.Text value="Ada" /><Field.Switch value /></>;`,
|
|
35
|
+
].join("\n"),
|
|
36
|
+
path.join(import.meta.dir, "page.tsx"),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const imported = await Promise.all(
|
|
40
|
+
[...(result?.contents ?? "").matchAll(/import \{ (\w+) as [\w$]+ \} from "([^"]+)";/g)].map(
|
|
41
|
+
async ([, name, file]) => [
|
|
42
|
+
name,
|
|
43
|
+
path.relative(ui, file as string),
|
|
44
|
+
hasUseClientDirective(await Bun.file(file as string).text()),
|
|
45
|
+
],
|
|
46
|
+
),
|
|
47
|
+
);
|
|
48
|
+
expect(imported).toEqual([
|
|
49
|
+
["Text", "Field/Text.tsx", true],
|
|
50
|
+
["Switch", "Field/Choice.tsx", true],
|
|
51
|
+
]);
|
|
52
|
+
expect(result?.kept).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { RuntimeNameTable } from "../quality/runtimeNameTable";
|
|
4
|
+
import { runtimeFixtureTables } from "../quality/runtimeTable.fixture";
|
|
5
|
+
import { tempDirs, writeText as write } from "../test/testHelpers";
|
|
6
|
+
import { ModelReference } from "./modelReference";
|
|
7
|
+
import { RouteRuntime } from "./routeRuntime";
|
|
8
|
+
|
|
9
|
+
const makeTempRoot = tempDirs("akan-model-reference-");
|
|
10
|
+
const root = "/ws";
|
|
11
|
+
const onDisk = (model: string) => `\n"use client";\n\nexport const reference = "${model}";\n`;
|
|
12
|
+
const referenceOf = (model: string) => `${root}/apps/demo/lib/__client/${model}.ref.ts`;
|
|
13
|
+
const uiFile = (name: string) => `${root}/apps/demo/ui/${name}.tsx`;
|
|
14
|
+
const naming = (model: string) =>
|
|
15
|
+
`"use client";\nimport { st } from "@apps/demo/client";\nexport const read = () => st.use.${model}();\n`;
|
|
16
|
+
const pickingRun = 'import { st } from "@apps/demo/client";\nexport const run = (name: string) => st.do[name]();\n';
|
|
17
|
+
const pickingStore = `import { store } from "akanjs/store";
|
|
18
|
+
|
|
19
|
+
import { st } from "../st";
|
|
20
|
+
import { fetch, sig } from "../useClient";
|
|
21
|
+
|
|
22
|
+
export class MemoStore extends store(sig.memo, () => ({ draft: "" })) {
|
|
23
|
+
run(name: string) {
|
|
24
|
+
return [fetch[name](), st.get().draft];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
const appPieces = ["_useClient", "_st", "_base.fetch", "memo.fetch", "memo.st", "note.fetch", "note.st", "tag.fetch"];
|
|
29
|
+
const kitPieces = ["_useClient", "_st", "note.fetch", "note.st", "tag.fetch", "tag.st", "util.st"];
|
|
30
|
+
const pieces = new Set([
|
|
31
|
+
...appPieces.map((piece) => `apps/demo/lib/__client/${piece}`),
|
|
32
|
+
...kitPieces.map((piece) => `libs/kit/lib/__client/${piece}`),
|
|
33
|
+
]);
|
|
34
|
+
const memoPieces = ["apps/demo/lib/__client/memo.fetch", "apps/demo/lib/__client/memo.st"];
|
|
35
|
+
const wholeLoads =
|
|
36
|
+
'import { loadedAfter as __akanLoadedAfter } from "akanjs/base"; import * as __akanLoaded0 from "/ws/apps/demo/lib/cnst.ts"; import { sig as __akanLoaded1 } from "/ws/apps/demo/lib/__client/_useClient.ts"; import { st as __akanLoaded2 } from "/ws/apps/demo/lib/__client/_st.ts"; __akanLoadedAfter(__akanLoaded0, __akanLoaded1, __akanLoaded2);';
|
|
37
|
+
|
|
38
|
+
let demo: RuntimeNameTable;
|
|
39
|
+
const runtimeOf = (files: Record<string, string> = {}) =>
|
|
40
|
+
new RouteRuntime(root, demo, pieces, (absPath) => files[absPath] ?? null);
|
|
41
|
+
const transformed = (model: string, files?: Record<string, string>) =>
|
|
42
|
+
runtimeOf(files).transform(onDisk(model), referenceOf(model));
|
|
43
|
+
|
|
44
|
+
beforeAll(async () => {
|
|
45
|
+
demo = (await runtimeFixtureTables()).demo;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("a model's reference file in the per-route build", () => {
|
|
49
|
+
test("imports the model's fetch piece as `loaded` and its store piece by the model's name, and passes both to `loadedAfter`", () => {
|
|
50
|
+
expect(transformed("memo")).toEqual({
|
|
51
|
+
source: `
|
|
52
|
+
"use client";
|
|
53
|
+
import { loadedAfter as __akanLoadedAfter } from "akanjs/base";
|
|
54
|
+
import { loaded as __akanLoaded0 } from "@apps/demo/lib/__client/memo.fetch";
|
|
55
|
+
import { memo as __akanLoaded1 } from "@apps/demo/lib/__client/memo.st";
|
|
56
|
+
__akanLoadedAfter(__akanLoaded0, __akanLoaded1);
|
|
57
|
+
|
|
58
|
+
export const reference = "memo";
|
|
59
|
+
`,
|
|
60
|
+
perModule: true,
|
|
61
|
+
pieces: memoPieces,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("loads the pieces a client file reading the model's store loads, for a model of the app, of a lib, and one the app extends", () => {
|
|
66
|
+
for (const model of ["memo", "tag", "note"]) {
|
|
67
|
+
const named = runtimeOf().transform(naming(model), uiFile("Card"));
|
|
68
|
+
expect(named?.pieces.some((piece) => piece.endsWith(`/${model}.st`))).toBe(true);
|
|
69
|
+
expect(transformed(model)?.pieces).toEqual(named?.pieces ?? []);
|
|
70
|
+
}
|
|
71
|
+
expect(transformed("tag")?.pieces).toEqual(["apps/demo/lib/__client/tag.fetch", "libs/kit/lib/__client/tag.st"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("loads the stores the model's own store names too, as a file reading that store does", () => {
|
|
75
|
+
const files = {
|
|
76
|
+
[`${root}/apps/demo/lib/memo/memo.store.ts`]: `import { store } from "akanjs/store";
|
|
77
|
+
|
|
78
|
+
import { st } from "../st";
|
|
79
|
+
import { sig } from "../useClient";
|
|
80
|
+
|
|
81
|
+
export class MemoStore extends store(sig.memo, () => ({ draft: "" })) {
|
|
82
|
+
relabel() {
|
|
83
|
+
st.do.pickLabel(st.get().tagFilter);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
`,
|
|
87
|
+
};
|
|
88
|
+
expect(transformed("memo", files)?.pieces).toEqual([
|
|
89
|
+
...memoPieces,
|
|
90
|
+
"libs/kit/lib/__client/tag.st",
|
|
91
|
+
"libs/kit/lib/__client/util.st",
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("loads the whole runtime where the model's store picks a name at run time", () => {
|
|
96
|
+
const whole = transformed("memo", { [`${root}/apps/demo/lib/memo/memo.store.ts`]: pickingStore });
|
|
97
|
+
expect(whole).toEqual({
|
|
98
|
+
source: `\n"use client";\n${wholeLoads.split("; ").join(";\n")}\n\nexport const reference = "memo";\n`,
|
|
99
|
+
perModule: false,
|
|
100
|
+
pieces: [],
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("holds no import made only for its effect, which a split bundle would move away from it", () => {
|
|
105
|
+
for (const model of ["memo", "tag", "note"]) expect(transformed(model)?.source).not.toMatch(/import\s*"/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("stays as written for a model the app does not have, and is no reference file anywhere else", () => {
|
|
109
|
+
expect(transformed("ghost")).toEqual({ source: onDisk("ghost"), perModule: true, pieces: [] });
|
|
110
|
+
expect(ModelReference.modelOf("apps/demo/lib/__client/note.ref.ts")).toBe("note");
|
|
111
|
+
expect(ModelReference.modelOf(referenceOf("note"))).toBe("note");
|
|
112
|
+
for (const file of ["apps/demo/lib/__client/note.st.ts", "apps/demo/ui/note.ref.ts", "apps/demo/lib/refs.ts"]) {
|
|
113
|
+
expect(ModelReference.modelOf(file)).toBeNull();
|
|
114
|
+
expect(ModelReference.isFile(file)).toBe(false);
|
|
115
|
+
}
|
|
116
|
+
expect(ModelReference.sourceOf(onDisk("note"), [])).toBe(onDisk("note"));
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("a model's reference file in a build planned over the whole-runtime bundle", () => {
|
|
121
|
+
const plan = (routes: Record<string, string[]>, files: Record<string, string>) => {
|
|
122
|
+
const target = runtimeOf(files);
|
|
123
|
+
const kept = target.plan({
|
|
124
|
+
importsByModule: new Map(Object.keys(files).map((file) => [file, []])),
|
|
125
|
+
entries: Object.keys(files),
|
|
126
|
+
entriesByRoute: new Map(Object.entries(routes)),
|
|
127
|
+
});
|
|
128
|
+
const piecesOf = (file: string) => target.transform(files[file] ?? "", file)?.pieces ?? "as written";
|
|
129
|
+
return { kept: [...kept].sort(), piecesOf };
|
|
130
|
+
};
|
|
131
|
+
const files = {
|
|
132
|
+
[referenceOf("memo")]: onDisk("memo"),
|
|
133
|
+
[uiFile("Plain")]: "export const Plain = 1;\n",
|
|
134
|
+
[uiFile("Run")]: pickingRun,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
test("is an entry narrowed to the model's pieces on a route that loads no whole runtime", () => {
|
|
138
|
+
const { kept, piecesOf } = plan(
|
|
139
|
+
{ "/list": [uiFile("Plain"), referenceOf("memo")], "/run": [uiFile("Run")] },
|
|
140
|
+
files,
|
|
141
|
+
);
|
|
142
|
+
expect(kept).toEqual([uiFile("Run")]);
|
|
143
|
+
expect(piecesOf(referenceOf("memo"))).toEqual(memoPieces);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("keeps its pieces beside the whole runtime of a page, where more routes without one hold it", () => {
|
|
147
|
+
const routes = {
|
|
148
|
+
"/list": [uiFile("Plain"), referenceOf("memo")],
|
|
149
|
+
"/view": [uiFile("Plain"), referenceOf("memo")],
|
|
150
|
+
"/run": [uiFile("Run"), referenceOf("memo")],
|
|
151
|
+
};
|
|
152
|
+
const { kept, piecesOf } = plan(routes, files);
|
|
153
|
+
expect(kept).toEqual([uiFile("Run")]);
|
|
154
|
+
expect(piecesOf(referenceOf("memo"))).toEqual(memoPieces);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("stays as written where only whole-runtime routes hold it, whose pages already load every model", () => {
|
|
158
|
+
const { kept, piecesOf } = plan(
|
|
159
|
+
{ "/run": [uiFile("Run"), referenceOf("memo")], "/plain": [uiFile("Plain")] },
|
|
160
|
+
files,
|
|
161
|
+
);
|
|
162
|
+
expect(kept).toEqual([referenceOf("memo"), uiFile("Run")].sort());
|
|
163
|
+
expect(piecesOf(referenceOf("memo"))).toBe("as written");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("keeps the whole runtime as an entry where its model's store picks a name at run time", () => {
|
|
167
|
+
const picking = { ...files, [`${root}/apps/demo/lib/memo/memo.store.ts`]: pickingStore };
|
|
168
|
+
const target = runtimeOf(picking);
|
|
169
|
+
const kept = target.plan({
|
|
170
|
+
importsByModule: new Map([referenceOf("memo"), uiFile("Plain")].map((file) => [file, []])),
|
|
171
|
+
entries: [referenceOf("memo"), uiFile("Plain")],
|
|
172
|
+
entriesByRoute: new Map([["/list", [uiFile("Plain"), referenceOf("memo")]]]),
|
|
173
|
+
});
|
|
174
|
+
expect([...kept].sort()).toEqual([referenceOf("memo"), uiFile("Plain")]);
|
|
175
|
+
expect(target.transform(onDisk("memo"), referenceOf("memo"))).toBeNull();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("the reference files of an app", () => {
|
|
180
|
+
test("are listed by model from the app's own folder, and are none before a sync wrote them", async () => {
|
|
181
|
+
const tempRoot = await makeTempRoot();
|
|
182
|
+
const dir = path.join(tempRoot, "apps/demo/lib/__client");
|
|
183
|
+
for (const file of ["note.ref.ts", "memo.ref.ts", "memo.st.ts", "refs.ts"]) await write(path.join(dir, file), "");
|
|
184
|
+
await write(path.join(tempRoot, "libs/kit/lib/__client/tag.ref.ts"), "");
|
|
185
|
+
|
|
186
|
+
expect([...(await ModelReference.filesOf(tempRoot, "apps/demo"))]).toEqual([
|
|
187
|
+
["memo", path.join(dir, "memo.ref.ts")],
|
|
188
|
+
["note", path.join(dir, "note.ref.ts")],
|
|
189
|
+
]);
|
|
190
|
+
expect([...(await ModelReference.filesOf(tempRoot, "apps/other"))]).toEqual([]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("are registered by the file the root layout imports", () => {
|
|
194
|
+
expect(ModelReference.registrySpecifier("demo")).toBe("@apps/demo/lib/__client/refs");
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const referenceFile = /(?:^|[\\/])lib[\\/]__client[\\/]([^\\/]+)\.ref\.ts$/;
|
|
5
|
+
const directive = /^"use client";$/m;
|
|
6
|
+
const suffix = ".ref.ts";
|
|
7
|
+
|
|
8
|
+
//? On disk a reference file names no piece: a bundle holding the whole runtime reads it too and would load each twice.
|
|
9
|
+
export class ModelReference {
|
|
10
|
+
static readonly registry = "__client/refs";
|
|
11
|
+
|
|
12
|
+
static registrySpecifier(appName: string): string {
|
|
13
|
+
return `@apps/${appName}/lib/${ModelReference.registry}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static async filesOf(workspaceRoot: string, appScope: string): Promise<Map<string, string>> {
|
|
17
|
+
const dir = path.join(workspaceRoot, appScope, "lib/__client");
|
|
18
|
+
const files = await readdir(dir).catch(() => []);
|
|
19
|
+
return new Map(
|
|
20
|
+
files
|
|
21
|
+
.filter((file) => file.endsWith(suffix))
|
|
22
|
+
.sort()
|
|
23
|
+
.map((file) => [file.slice(0, -suffix.length), path.join(dir, file)]),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static modelOf(file: string): string | null {
|
|
28
|
+
return file.match(referenceFile)?.[1] ?? null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static isFile(file: string): boolean {
|
|
32
|
+
return referenceFile.test(file);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//? `loads` imports each piece by name for `loadedAfter`: Bun's chunk splitting moves an effect-only import away.
|
|
36
|
+
static sourceOf(source: string, loads: string[]): string {
|
|
37
|
+
return loads.length ? source.replace(directive, (line) => [line, ...loads].join("\n")) : source;
|
|
38
|
+
}
|
|
39
|
+
}
|