@ecopages/core 0.2.0-rc.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +39 -38
  2. package/package.json +22 -152
  3. package/src/adapters/README.md +11 -3
  4. package/src/adapters/abstract/application-adapter.d.ts +32 -36
  5. package/src/adapters/abstract/application-adapter.js +61 -47
  6. package/src/adapters/abstract/server-adapter.d.ts +14 -0
  7. package/src/adapters/abstract/server-adapter.js +10 -0
  8. package/src/adapters/abstract/ws-pattern-matcher.d.ts +0 -5
  9. package/src/adapters/abstract/ws-pattern-matcher.js +0 -9
  10. package/src/adapters/bun/create-app.d.ts +12 -16
  11. package/src/adapters/bun/create-app.js +25 -55
  12. package/src/adapters/bun/hmr-manager.d.ts +0 -1
  13. package/src/adapters/bun/hmr-manager.js +2 -4
  14. package/src/adapters/bun/server-adapter.d.ts +15 -21
  15. package/src/adapters/bun/server-adapter.js +33 -67
  16. package/src/adapters/bun/static-preview-host.d.ts +1 -0
  17. package/src/adapters/bun/static-preview-host.js +1 -1
  18. package/src/adapters/create-app.d.ts +63 -8
  19. package/src/adapters/create-app.js +62 -5
  20. package/src/adapters/node/create-app.d.ts +8 -12
  21. package/src/adapters/node/create-app.js +22 -14
  22. package/src/adapters/node/runtime-host.d.ts +10 -10
  23. package/src/adapters/node/runtime-host.js +13 -8
  24. package/src/adapters/node/server-adapter-dependencies.d.ts +2 -10
  25. package/src/adapters/node/server-adapter-dependencies.js +8 -13
  26. package/src/adapters/node/server-adapter.d.ts +30 -32
  27. package/src/adapters/node/server-adapter.js +59 -65
  28. package/src/adapters/shared/hmr/shared-hmr-manager.d.ts +0 -4
  29. package/src/adapters/shared/hmr/shared-hmr-manager.js +0 -12
  30. package/src/adapters/shared/http/api-request-pipeline.d.ts +27 -1
  31. package/src/adapters/shared/http/api-request-pipeline.js +31 -16
  32. package/src/adapters/shared/http/explicit-static-route-matcher.js +5 -2
  33. package/src/adapters/shared/http/fs-server-response-factory.d.ts +2 -9
  34. package/src/adapters/shared/http/fs-server-response-factory.js +5 -33
  35. package/src/adapters/shared/http/fs-server-response-matcher.d.ts +24 -24
  36. package/src/adapters/shared/http/fs-server-response-matcher.js +59 -83
  37. package/src/adapters/shared/runtime/application-adapter.d.ts +1 -0
  38. package/src/adapters/shared/runtime/application-adapter.js +12 -0
  39. package/src/adapters/shared/runtime/bind-runtime-server.d.ts +17 -0
  40. package/src/adapters/shared/runtime/bind-runtime-server.js +50 -0
  41. package/src/adapters/shared/runtime/collect-dev-prewarm-plan.d.ts +1 -3
  42. package/src/adapters/shared/runtime/collect-dev-prewarm-plan.js +1 -4
  43. package/src/adapters/shared/runtime/port-manager-default-prompt.d.ts +6 -0
  44. package/src/adapters/shared/runtime/port-manager-default-prompt.js +41 -0
  45. package/src/adapters/shared/runtime/port-manager.d.ts +3 -3
  46. package/src/adapters/shared/runtime/port-manager.js +14 -45
  47. package/src/adapters/shared/runtime/runtime-app-bootstrap.d.ts +0 -10
  48. package/src/adapters/shared/runtime/runtime-app-bootstrap.js +0 -7
  49. package/src/adapters/shared/runtime/runtime-host.d.ts +5 -2
  50. package/src/adapters/shared/runtime/runtime-server-lifecycle.d.ts +1 -1
  51. package/src/adapters/shared/runtime/runtime-server-lifecycle.js +1 -1
  52. package/src/adapters/shared/runtime/server-adapter.d.ts +26 -5
  53. package/src/adapters/shared/runtime/server-adapter.js +35 -8
  54. package/src/adapters/shared/runtime/server-route-handler.js +11 -3
  55. package/src/adapters/shared/runtime/server-static-builder.d.ts +9 -3
  56. package/src/adapters/shared/runtime/server-static-builder.js +59 -37
  57. package/src/adapters/shared/ws/node-http-websocket-upgrades.d.ts +9 -1
  58. package/src/adapters/shared/ws/node-http-websocket-upgrades.js +8 -2
  59. package/src/build/README.md +12 -8
  60. package/src/build/cache/cache-constants.d.ts +0 -2
  61. package/src/build/cache/cache-constants.js +0 -2
  62. package/src/build/cache/cache-keys.d.ts +0 -2
  63. package/src/build/cache/cache-keys.js +0 -2
  64. package/src/build/cache/output-imports.d.ts +35 -0
  65. package/src/build/cache/output-imports.js +87 -0
  66. package/src/build/cache/pages-unified-graph-build.d.ts +15 -1
  67. package/src/build/cache/pages-unified-graph-build.js +73 -22
  68. package/src/build/cache/production-build-cache.d.ts +2 -2
  69. package/src/build/cache/production-build-cache.js +2 -2
  70. package/src/build/cache/server-entry-build-cache.d.ts +25 -3
  71. package/src/build/cache/server-entry-build-cache.js +129 -13
  72. package/src/build/contracts/build-contracts.d.ts +1 -13
  73. package/src/build/rolldown/runtime-build-output-normalizer.js +3 -6
  74. package/src/build/runtime/build-request-identity.d.ts +2 -6
  75. package/src/build/runtime/build-request-identity.js +2 -8
  76. package/src/build/runtime/build-request-policy.js +1 -1
  77. package/src/build/runtime/deduping-build-executor.d.ts +0 -1
  78. package/src/build/runtime/deduping-build-executor.js +0 -1
  79. package/src/build/server-bundle-publication.d.ts +14 -0
  80. package/src/build/server-bundle-publication.js +49 -0
  81. package/src/cache/index.d.ts +1 -1
  82. package/src/cache/index.js +1 -1
  83. package/src/cache/module-parse-cache.d.ts +2 -29
  84. package/src/cache/module-parse-cache.js +6 -39
  85. package/src/config/README.md +17 -2
  86. package/src/config/apply-user-config.d.ts +8 -0
  87. package/src/config/apply-user-config.js +68 -0
  88. package/src/config/config-builder.d.ts +11 -1
  89. package/src/config/config-builder.js +53 -12
  90. package/src/config/constants.d.ts +4 -4
  91. package/src/config/constants.js +4 -4
  92. package/src/config/define-config.d.ts +9 -0
  93. package/src/config/define-config.js +10 -0
  94. package/src/config/index.d.ts +5 -0
  95. package/src/config/index.js +4 -0
  96. package/src/config/is-finalized-app-config.d.ts +6 -0
  97. package/src/config/is-finalized-app-config.js +11 -0
  98. package/src/config/load-eco-config.d.ts +21 -0
  99. package/src/config/load-eco-config.js +107 -0
  100. package/src/config/resolve-eco-config-path.d.ts +32 -0
  101. package/src/config/resolve-eco-config-path.js +58 -0
  102. package/src/config/server-config-bundle.d.ts +25 -0
  103. package/src/config/server-config-bundle.js +148 -0
  104. package/src/config/user-config-types.d.ts +170 -0
  105. package/src/config/user-config-types.js +1 -0
  106. package/src/dev/README.md +4 -0
  107. package/src/dev/development-restart-watch-paths.d.ts +13 -0
  108. package/src/dev/development-restart-watch-paths.js +37 -0
  109. package/src/dev/development-restart.d.ts +13 -0
  110. package/src/dev/development-restart.js +21 -0
  111. package/src/dev/host-runtime.d.ts +0 -1
  112. package/src/dev/host-runtime.js +0 -3
  113. package/src/dev/sc-server.d.ts +4 -1
  114. package/src/dev/sc-server.js +11 -20
  115. package/src/dev/transform-server/dev-transform-import-rewriter.js +2 -2
  116. package/src/eco/eco.js +4 -0
  117. package/src/errors/http-error-page-contract.d.ts +77 -0
  118. package/src/errors/http-error-page-contract.js +106 -0
  119. package/src/hmr/hmr-file-change-prep.d.ts +0 -1
  120. package/src/hmr/hmr-file-change-prep.js +2 -5
  121. package/src/hmr/hmr-strategy.d.ts +3 -8
  122. package/src/hmr/hmr-strategy.js +3 -8
  123. package/src/index.browser.d.ts +1 -2
  124. package/src/index.browser.js +1 -2
  125. package/src/index.d.ts +5 -6
  126. package/src/index.js +5 -6
  127. package/src/islands/island-host.d.ts +0 -9
  128. package/src/islands/island-host.js +1 -13
  129. package/src/plugins/component-import-discovery.js +2 -2
  130. package/src/plugins/eco-component-meta-plugin.js +5 -5
  131. package/src/plugins/integration-plugin.d.ts +0 -1
  132. package/src/plugins/integration-plugin.js +0 -4
  133. package/src/plugins/processor.d.ts +6 -2
  134. package/src/route-renderer/README.md +4 -0
  135. package/src/{adapters/shared/http/explicit-static-render-preparation.d.ts → route-renderer/explicit-view-render-preparation.d.ts} +3 -3
  136. package/src/route-renderer/orchestration/document-shell/document-shell-render.service.js +1 -4
  137. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +25 -24
  138. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +18 -20
  139. package/src/route-renderer/orchestration/integration-renderer.d.ts +24 -7
  140. package/src/route-renderer/orchestration/integration-renderer.js +44 -33
  141. package/src/route-renderer/orchestration/integration-renderer.test-fixtures.js +1 -1
  142. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.d.ts +1 -1
  143. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.js +1 -1
  144. package/src/route-renderer/route-renderer.d.ts +9 -0
  145. package/src/route-renderer/route-renderer.js +18 -0
  146. package/src/services/README.md +2 -1
  147. package/src/services/assets/browser-bundle.service.d.ts +1 -6
  148. package/src/services/error-pages/README.md +9 -0
  149. package/src/services/error-pages/default-error-pages.d.ts +39 -0
  150. package/src/services/error-pages/default-error-pages.js +199 -0
  151. package/src/services/error-pages/error-page-renderer.d.ts +58 -0
  152. package/src/services/error-pages/error-page-renderer.js +146 -0
  153. package/src/services/error-pages/semantic-error-page-exporter.d.ts +23 -0
  154. package/src/services/error-pages/semantic-error-page-exporter.js +35 -0
  155. package/src/services/html/html-rewriter.d.ts +77 -0
  156. package/src/services/html/html-rewriter.js +751 -0
  157. package/src/services/html/html-rewriter.parity.test.bun.d.ts +1 -0
  158. package/src/services/html/html-rewriter.parity.test.bun.js +74 -0
  159. package/src/services/html/html-transformer.service.d.ts +29 -42
  160. package/src/services/html/html-transformer.service.js +85 -157
  161. package/src/services/invalidation/development-invalidation.service.d.ts +9 -20
  162. package/src/services/invalidation/development-invalidation.service.js +26 -39
  163. package/src/services/module-loading/README.md +20 -15
  164. package/src/services/module-loading/app-module-loader.service.d.ts +0 -2
  165. package/src/services/module-loading/app-server-module-transpiler.service.d.ts +1 -1
  166. package/src/services/module-loading/app-server-module-transpiler.service.js +2 -10
  167. package/src/services/module-loading/collection-server-module-build.service.d.ts +0 -1
  168. package/src/services/module-loading/collection-server-module-build.service.js +0 -3
  169. package/src/services/module-loading/host-module-loader-registry.d.ts +0 -1
  170. package/src/services/module-loading/host-module-loader-registry.js +0 -3
  171. package/src/services/module-loading/route-module-build-cache-registry.d.ts +0 -4
  172. package/src/services/module-loading/route-module-build-cache-registry.js +0 -4
  173. package/src/services/module-loading/route-module-build-cache.store.d.ts +8 -0
  174. package/src/services/module-loading/route-module-build-cache.store.js +25 -7
  175. package/src/services/module-loading/route-module-build-manifest.d.ts +12 -1
  176. package/src/services/module-loading/route-module-build-manifest.js +7 -3
  177. package/src/services/module-loading/route-module-dependency-hasher.d.ts +3 -2
  178. package/src/services/module-loading/route-module-dependency-hasher.js +13 -1
  179. package/src/services/module-loading/server-module-transpiler.service.d.ts +1 -15
  180. package/src/services/module-loading/server-module-transpiler.service.js +0 -24
  181. package/src/services/module-loading/view-module-loader.d.ts +10 -0
  182. package/src/services/module-loading/view-module-loader.js +46 -0
  183. package/src/services/runtime-state/entrypoint-dependency-graph.service.js +0 -12
  184. package/src/services/runtime-state/server-invalidation-state.service.d.ts +4 -2
  185. package/src/services/runtime-state/server-invalidation-state.service.js +7 -14
  186. package/src/static-site-generator/README.md +12 -8
  187. package/src/static-site-generator/static-site-generator.d.ts +3 -2
  188. package/src/static-site-generator/static-site-generator.js +17 -3
  189. package/src/types/internal-types.d.ts +1 -38
  190. package/src/types/public-types.d.ts +28 -130
  191. package/src/watchers/project-watcher.d.ts +9 -16
  192. package/src/watchers/project-watcher.js +35 -28
  193. package/src/watchers/project-watcher.test-helpers.d.ts +10 -1
  194. package/src/watchers/project-watcher.test-helpers.js +12 -0
  195. package/src/adapters/abstract/router-adapter.d.ts +0 -26
  196. package/src/adapters/abstract/router-adapter.js +0 -13
  197. package/src/adapters/bun/index.d.ts +0 -2
  198. package/src/adapters/bun/index.js +0 -2
  199. package/src/adapters/index.d.ts +0 -2
  200. package/src/adapters/index.js +0 -2
  201. package/src/eco/eco.utils.d.ts +0 -1
  202. package/src/eco/eco.utils.js +0 -1
  203. package/src/services/cache/index.d.ts +0 -9
  204. package/src/services/cache/index.js +0 -8
  205. package/src/services/html/html-rewriter-provider.service.d.ts +0 -40
  206. package/src/services/html/html-rewriter-provider.service.js +0 -68
  207. package/src/services/module-loading/route-module-build-cache.d.ts +0 -4
  208. package/src/services/module-loading/route-module-build-cache.js +0 -3
  209. package/src/services/runtime-state/dev-graph.service.d.ts +0 -118
  210. package/src/services/runtime-state/dev-graph.service.js +0 -170
  211. /package/src/{adapters/shared/http/explicit-static-render-preparation.js → route-renderer/explicit-view-render-preparation.js} +0 -0
@@ -45,6 +45,7 @@ build/
45
45
  app-build-manifest-runtime.ts contributor collection at startup
46
46
  contracts/ EcoBuildPlugin, BuildOptions, AppBuildManifest
47
47
  runtime/ profiles, executors, request policy/identity
48
+ server-bundle-publication.ts staged server/config artifact publication
48
49
  rolldown/ bundler adapter, plugin bridge, output normalization
49
50
  cache/ persisted caches, fingerprints, unified pages graph
50
51
  browser/ client runtime rewrites, JSX ownership, Lit worker guard
@@ -60,7 +61,9 @@ build/
60
61
  - `rolldown/rolldown-plugin-bridge.ts`: `EcoBuildPlugin[]` → bundler-plugin translation.
61
62
  - `runtime/serialized-build-executor.ts`: FIFO queue primitive.
62
63
  - `cache/server-entry-build-cache.ts`: production server-entry bundle cache (`.eco/.server-entry/.build-cache.json` + `dist/.server/manifest.json`).
64
+ - `server-bundle-publication.ts`: stages the server entry, emitted config, and deploy manifest together, then publishes the complete directory with rollback.
63
65
  - `cache/cache-constants.ts`: shared `.build-cache.json` filename for persisted production caches.
66
+ - `cache/output-imports.ts`: lists the local files reachable from a compiled module, including shared chunks, so a cached module is reused only while they exist.
64
67
  - `*.test.ts`: regression coverage colocated with each module.
65
68
 
66
69
  ## Default Flow
@@ -137,13 +140,9 @@ App-manifest plugins keep canonical registration order and cannot be silently re
137
140
 
138
141
  ## BuildOptions Caveats
139
142
 
140
- `BuildOptions` is modeled on the bundler's options shape. Most fields map cleanly. The exceptions:
143
+ `BuildOptions` is modeled on the bundler's options shape. Most fields map cleanly. The exception:
141
144
 
142
145
  - `splitting` — when `false` with a single entrypoint, maps to Rolldown `codeSplitting: false` so dynamic imports stay in one file. Multi-entrypoint builds ignore `splitting: false` because Rolldown cannot inline across multiple inputs.
143
- - `bundle` — accepted but ignored. The bundler always bundles.
144
- - `outbase` — accepted but ignored. The adapter derives the base from `options.root` directly.
145
-
146
- These fields are kept in the type so existing call-sites compile. The proper fix is a more focused `BuildOptions` schema in a follow-up.
147
146
 
148
147
  ## Dev / watch path
149
148
 
@@ -167,14 +166,17 @@ Set `ECOPAGES_ROLLDOWN_BUILD_METRICS=1` to log Rolldown invocation counts during
167
166
 
168
167
  ## Production build caches
169
168
 
170
- Two persisted cache layers accelerate production builds. Both use `.build-cache.json` manifests keyed by dependency hashes and a build-inputs fingerprint.
169
+ Three persisted cache layers accelerate production builds. All use `.build-cache.json` manifests keyed by dependency hashes and build-input fingerprints.
171
170
 
172
171
  | Cache | On-disk location | Module |
173
172
  | -------------------------------------- | --------------------------------------------------- | ---------------------------------------------------- |
174
173
  | Server-entry bundle | `.eco/.server-entry/.build-cache.json` | `cache/server-entry-build-cache.ts` |
175
174
  | Route-module transpile + static render | `<server-outdir>/.server-modules/.build-cache.json` | `route-module-build-cache.store.ts` (module-loading) |
175
+ | Unified pages graph | `.eco/.server-pages-graph/.build-cache.json` | `cache/pages-unified-graph-build.ts` |
176
+
177
+ `requireBuildRuntime(appConfig).getProfile('server-entry')` serves server-entry bundling. `clearProductionBuildCaches()` wipes all three manifest trees, resets in-memory route-module state, and clears `buildRuntime`.
176
178
 
177
- `requireBuildRuntime(appConfig).getProfile('server-entry')` serves server-entry bundling. `clearProductionBuildCaches()` wipes both manifest trees, resets in-memory route-module state, and clears `buildRuntime`.
179
+ Server-entry cache hits require every recorded runtime artifact. Cache misses build the server entry and emitted config in a sibling staging directory; the deploy manifest joins that generation before the directory is published. A failed build therefore leaves the previous server generation intact.
178
180
 
179
181
  The route-module registry (`route-module-build-cache-registry.ts`) shares one `RouteModuleBuildCache` per `(app, outdir)` pair. Legacy `.server-route-modules` outdirs are still read for migration but new writes go to `.server-modules`.
180
182
 
@@ -187,7 +189,9 @@ Production static exports compile all template pages in one Rolldown invocation
187
189
  | Graph manifest | `.eco/.server-pages-graph/.build-cache.json` |
188
190
  | Chunk outputs | `.eco/.server-modules/` (shared with per-route cache) |
189
191
 
190
- `StaticSiteGenerator` calls `ensurePagesUnifiedGraphBuilt()` before the export loop. `PageModuleImportService` imports prebuilt chunks via `importPagesUnifiedGraphModule()` and falls back to per-page Rolldown on miss. This is separate from production Page Browser Graph prebuild (`production-page-browser-graph-prebuild.ts`), which warms browser assets in `page-browser-graph-session`.
192
+ `StaticSiteGenerator` calls `ensurePagesUnifiedGraphBuilt()` before the export loop. `PageModuleImportService` imports prebuilt chunks via `importPagesUnifiedGraphModule()` and falls back to per-page Rolldown on miss.
193
+
194
+ A persisted graph is reused only when it matches the core package version, the build inputs and the build key, every local file reachable from its outputs still exists, every recorded source-file hash still matches, and the exact active template route set matches recorded outputs. Manifests without `dependencyHashes` are treated as absent, so a deleted layout or component cannot keep serving its compiled chunk. `importPagesUnifiedGraphModule()` applies the same check, because pages can be imported before the export loop runs. This is separate from production Page Browser Graph prebuild (`production-page-browser-graph-prebuild.ts`), which warms browser assets in `page-browser-graph-session`.
191
195
 
192
196
  `ECOPAGES_ROLLDOWN_BUILD_METRICS=1` enables `rolldown/rolldown-build-invocation-metrics.ts` counters used by bench and parity tests.
193
197
 
@@ -1,4 +1,2 @@
1
1
  /** Filename written beside persisted production build artifacts that store incremental metadata. */
2
2
  export declare const ROUTE_MODULE_BUILD_CACHE_FILENAME = ".build-cache.json";
3
- /** @deprecated Use {@link ROUTE_MODULE_BUILD_CACHE_FILENAME}; server-entry caches share the same filename. */
4
- export declare const SERVER_ENTRY_BUILD_CACHE_FILENAME = ".build-cache.json";
@@ -1,4 +1,2 @@
1
1
  /** Filename written beside persisted production build artifacts that store incremental metadata. */
2
2
  export const ROUTE_MODULE_BUILD_CACHE_FILENAME = '.build-cache.json';
3
- /** @deprecated Use {@link ROUTE_MODULE_BUILD_CACHE_FILENAME}; server-entry caches share the same filename. */
4
- export const SERVER_ENTRY_BUILD_CACHE_FILENAME = ROUTE_MODULE_BUILD_CACHE_FILENAME;
@@ -18,5 +18,3 @@ export declare function createSourceTransformCacheKey(sourceTransforms: BuildOpt
18
18
  * agree within one install; not stable across minifiers or renames.
19
19
  */
20
20
  export declare function hashFunctionIdentity(fn: (...args: never[]) => unknown): string;
21
- /** @deprecated Use {@link hashFunctionIdentity}. */
22
- export declare const hashPluginSetup: typeof hashFunctionIdentity;
@@ -67,5 +67,3 @@ export function hashFunctionIdentity(fn) {
67
67
  }
68
68
  return (hash >>> 0).toString(36);
69
69
  }
70
- /** @deprecated Use {@link hashFunctionIdentity}. */
71
- export const hashPluginSetup = hashFunctionIdentity;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Lists the local files a compiled module imports: relative, absolute and
3
+ * `file:` specifiers in static imports, re-exports and literal dynamic imports.
4
+ *
5
+ * @remarks
6
+ * {@link collectReachableLocalImports} follows these edges so persisted build
7
+ * caches can check that the shared chunks and generated modules a cached module
8
+ * loads (for example `.server-collections`) still exist. Uses `parseSync`
9
+ * directly rather than the shared parse cache, so large compiled bundles are not
10
+ * retained in memory.
11
+ */
12
+ export declare function collectLocalImports(code: string, modulePath: string): string[];
13
+ export type LocalImportFileAccess = {
14
+ exists: (filePath: string) => boolean;
15
+ readFile: (filePath: string) => string;
16
+ };
17
+ /**
18
+ * Lists every local file reachable from `modulePath` through local imports.
19
+ *
20
+ * @remarks
21
+ * {@link collectLocalImports} alone misses shared chunks that a page output only
22
+ * reaches through another chunk. Persisted caches record this full set and are
23
+ * reused only while every file in it exists. `modulePath` itself is omitted,
24
+ * because callers already check that the compiled output exists.
25
+ *
26
+ * A missing file is listed but not followed, so a cache recorded against it
27
+ * never validates.
28
+ *
29
+ * Pass one `directImportsCache` to every walk of a single build so shared chunks
30
+ * are read and parsed once instead of once per output.
31
+ */
32
+ export declare function collectReachableLocalImports(modulePath: string, options?: {
33
+ fileAccess?: LocalImportFileAccess;
34
+ directImportsCache?: Map<string, string[]>;
35
+ }): string[];
@@ -0,0 +1,87 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { fileSystem } from '@ecopages/file-system';
4
+ import { parseSync } from 'oxc-parser';
5
+ /**
6
+ * Lists the local files a compiled module imports: relative, absolute and
7
+ * `file:` specifiers in static imports, re-exports and literal dynamic imports.
8
+ *
9
+ * @remarks
10
+ * {@link collectReachableLocalImports} follows these edges so persisted build
11
+ * caches can check that the shared chunks and generated modules a cached module
12
+ * loads (for example `.server-collections`) still exist. Uses `parseSync`
13
+ * directly rather than the shared parse cache, so large compiled bundles are not
14
+ * retained in memory.
15
+ */
16
+ export function collectLocalImports(code, modulePath) {
17
+ const { module } = parseSync(modulePath, code, { lang: 'js', sourceType: 'module' });
18
+ const specifiers = [
19
+ ...module.staticImports.map((entry) => entry.moduleRequest.value),
20
+ ...module.staticExports.flatMap((entry) => entry.entries.map((exportEntry) => exportEntry.moduleRequest?.value)),
21
+ ...module.dynamicImports.map((entry) => readStringLiteral(code.slice(entry.moduleRequest.start, entry.moduleRequest.end))),
22
+ ];
23
+ const directory = path.dirname(modulePath);
24
+ const localImports = new Set();
25
+ for (const specifier of specifiers) {
26
+ const resolved = specifier ? resolveLocalSpecifier(specifier, directory) : undefined;
27
+ if (resolved)
28
+ localImports.add(resolved);
29
+ }
30
+ return [...localImports];
31
+ }
32
+ const defaultLocalImportFileAccess = {
33
+ exists: (filePath) => fileSystem.exists(filePath),
34
+ readFile: (filePath) => fileSystem.readFileSync(filePath),
35
+ };
36
+ /**
37
+ * Lists every local file reachable from `modulePath` through local imports.
38
+ *
39
+ * @remarks
40
+ * {@link collectLocalImports} alone misses shared chunks that a page output only
41
+ * reaches through another chunk. Persisted caches record this full set and are
42
+ * reused only while every file in it exists. `modulePath` itself is omitted,
43
+ * because callers already check that the compiled output exists.
44
+ *
45
+ * A missing file is listed but not followed, so a cache recorded against it
46
+ * never validates.
47
+ *
48
+ * Pass one `directImportsCache` to every walk of a single build so shared chunks
49
+ * are read and parsed once instead of once per output.
50
+ */
51
+ export function collectReachableLocalImports(modulePath, options = {}) {
52
+ const { fileAccess = defaultLocalImportFileAccess, directImportsCache = new Map() } = options;
53
+ const reachable = new Set();
54
+ const pending = [modulePath];
55
+ const visited = new Set();
56
+ while (pending.length > 0) {
57
+ const current = pending.pop();
58
+ if (!current || visited.has(current)) {
59
+ continue;
60
+ }
61
+ visited.add(current);
62
+ if (current !== modulePath) {
63
+ reachable.add(current);
64
+ }
65
+ if (current !== modulePath && !fileAccess.exists(current)) {
66
+ continue;
67
+ }
68
+ let directImports = directImportsCache.get(current);
69
+ if (!directImports) {
70
+ directImports = collectLocalImports(fileAccess.readFile(current), current);
71
+ directImportsCache.set(current, directImports);
72
+ }
73
+ pending.push(...directImports);
74
+ }
75
+ return [...reachable];
76
+ }
77
+ function resolveLocalSpecifier(specifier, directory) {
78
+ const bare = specifier.replace(/[?#].*$/, '');
79
+ if (bare.startsWith('file:'))
80
+ return fileURLToPath(bare);
81
+ if (bare.startsWith('.'))
82
+ return path.resolve(directory, bare);
83
+ return path.isAbsolute(bare) ? bare : undefined;
84
+ }
85
+ function readStringLiteral(source) {
86
+ return /^(['"`])([^'"`$]*)\1$/.exec(source.trim())?.[2];
87
+ }
@@ -6,14 +6,28 @@
6
6
  * registered on the app config.
7
7
  */
8
8
  import type { EcoPagesAppConfig } from '../../types/internal-types.js';
9
+ import { type RouteModuleDependencyHashes } from '../../services/module-loading/route-module-dependency-hasher.js';
9
10
  export declare const PAGES_UNIFIED_GRAPH_CACHE_DIR = ".server-pages-graph";
10
11
  export declare const PAGES_UNIFIED_GRAPH_CACHE_FILENAME = ".build-cache.json";
11
12
  export interface PagesUnifiedGraphCacheManifest {
12
- invalidationVersion: string;
13
+ corePackageVersion: string;
13
14
  buildInputsFingerprint: string;
14
15
  buildKey: string;
15
16
  builtAt: number;
16
17
  outputs: Record<string, string>;
18
+ /**
19
+ * Local files reachable from the outputs through local imports, including
20
+ * shared chunks. The graph is reused only while all of them exist.
21
+ */
22
+ outputImports: string[];
23
+ /**
24
+ * Content hashes for every trackable source file in the graph.
25
+ *
26
+ * @remarks
27
+ * Compiled chunks can outlive a deleted layout or component. Reuse also
28
+ * requires these hashes so a source edit or deletion rebuilds the graph.
29
+ */
30
+ dependencyHashes: RouteModuleDependencyHashes;
17
31
  }
18
32
  export declare function isPagesUnifiedGraphEnabled(): boolean;
19
33
  /** True when the unified graph should build or serve prebuilt page modules. */
@@ -15,9 +15,10 @@ import { createServerBuildRequest, resolveServerAppBuildPlugins } from '../runti
15
15
  import { createBuildInputsFingerprint, hashAppConfigFile } from './build-input-fingerprint.js';
16
16
  import { isProductionCacheManifestCurrent, matchesProductionCacheBuildKey, matchesProductionCacheFingerprint, readProductionCacheManifest, writeProductionCacheManifest, } from './production-build-cache.js';
17
17
  import { getCorePackageVersion } from './cache-keys.js';
18
+ import { collectReachableLocalImports } from './output-imports.js';
18
19
  import { resolveInternalExecutionDir } from '../../utils/resolve-work-dir.js';
19
- import { getSharedRouteModuleBuildCache } from '../../services/module-loading/route-module-build-cache-registry.js';
20
- import { resolveRouteModuleDependencyPaths } from '../../services/module-loading/route-module-dependency-hasher.js';
20
+ import { getServerModuleBuildCacheOutdir, getSharedRouteModuleBuildCache, } from '../../services/module-loading/route-module-build-cache-registry.js';
21
+ import { RouteModuleDependencyHasher, resolveRouteModuleDependencyPaths, } from '../../services/module-loading/route-module-dependency-hasher.js';
21
22
  export const PAGES_UNIFIED_GRAPH_CACHE_DIR = '.server-pages-graph';
22
23
  export const PAGES_UNIFIED_GRAPH_CACHE_FILENAME = '.build-cache.json';
23
24
  export function isPagesUnifiedGraphEnabled() {
@@ -56,13 +57,24 @@ function createPagesUnifiedGraphBuildKey(appConfig, outdir) {
56
57
  'external-packages',
57
58
  ].join('::');
58
59
  }
60
+ /**
61
+ * @remarks
62
+ * Manifests without `outputImports` or `dependencyHashes` predate those
63
+ * checks, so they cannot prove their chunks and sources are still current.
64
+ */
59
65
  function readPagesUnifiedGraphManifest(appConfig) {
60
66
  const manifest = readProductionCacheManifest(getPagesUnifiedGraphCachePath(appConfig));
61
- if (!manifest?.outputs) {
67
+ if (!manifest?.outputs ||
68
+ !Array.isArray(manifest.outputImports) ||
69
+ !manifest.dependencyHashes ||
70
+ Object.keys(manifest.dependencyHashes).length === 0) {
62
71
  return undefined;
63
72
  }
64
73
  return manifest;
65
74
  }
75
+ function sourceDependenciesAreCurrent(manifest) {
76
+ return new RouteModuleDependencyHasher().matchesStoredHashes(manifest.dependencyHashes);
77
+ }
66
78
  function writePagesUnifiedGraphManifest(appConfig, manifest) {
67
79
  writeProductionCacheManifest(getPagesUnifiedGraphCachePath(appConfig), manifest);
68
80
  }
@@ -95,21 +107,47 @@ function resolveOutputForEntrypoint(entryPath, entryKey, buildResult) {
95
107
  }
96
108
  return undefined;
97
109
  }
110
+ /**
111
+ * Whether `manifest` was written by the current core version, build inputs and graph
112
+ * setup, every file its outputs import still exists, and source hashes still match.
113
+ */
114
+ function isGraphManifestCurrent(manifest, appConfig, outdir) {
115
+ return (isProductionCacheManifestCurrent(manifest, getCorePackageVersion()) &&
116
+ matchesProductionCacheFingerprint(manifest, createBuildInputsFingerprint(appConfig)) &&
117
+ matchesProductionCacheBuildKey(manifest, createPagesUnifiedGraphBuildKey(appConfig, outdir)) &&
118
+ manifest.outputImports.every((filePath) => fileSystem.exists(filePath)) &&
119
+ sourceDependenciesAreCurrent(manifest));
120
+ }
121
+ /**
122
+ * @remarks
123
+ * In addition to graph freshness and chunk existence on disk, the manifest must
124
+ * match the exact count of active template entries so removing or adding a page
125
+ * invalidates the graph.
126
+ */
98
127
  function isManifestValidForEntries(manifest, appConfig, outdir, entryPaths) {
99
- if (!isProductionCacheManifestCurrent(manifest, getCorePackageVersion())) {
100
- return false;
101
- }
102
- if (!matchesProductionCacheFingerprint(manifest, createBuildInputsFingerprint(appConfig))) {
103
- return false;
104
- }
105
- if (!matchesProductionCacheBuildKey(manifest, createPagesUnifiedGraphBuildKey(appConfig, outdir))) {
106
- return false;
107
- }
108
- return entryPaths.every((entryPath) => {
109
- const resolvedEntryPath = path.resolve(entryPath);
110
- const outputPath = manifest.outputs[resolvedEntryPath];
111
- return outputPath ? fileSystem.exists(outputPath) : false;
112
- });
128
+ return (isGraphManifestCurrent(manifest, appConfig, outdir) &&
129
+ entryPaths.length === Object.keys(manifest.outputs).length &&
130
+ entryPaths.every((entryPath) => {
131
+ const outputPath = manifest.outputs[path.resolve(entryPath)];
132
+ return outputPath ? fileSystem.exists(outputPath) : false;
133
+ }));
134
+ }
135
+ const importableGraphs = new WeakMap();
136
+ /**
137
+ * {@link isGraphManifestCurrent} for page imports, memoized per manifest write.
138
+ *
139
+ * @remarks
140
+ * Page modules can be imported before the static site generator calls
141
+ * {@link ensurePagesUnifiedGraphBuilt} (for example while collecting static
142
+ * paths), so the import path must not trust a manifest left by an older build.
143
+ */
144
+ function isGraphImportable(manifest, appConfig) {
145
+ const known = importableGraphs.get(appConfig);
146
+ if (known?.builtAt === manifest.builtAt)
147
+ return known.current;
148
+ const current = isGraphManifestCurrent(manifest, appConfig, getServerModuleBuildCacheOutdir(appConfig));
149
+ importableGraphs.set(appConfig, { builtAt: manifest.builtAt, current });
150
+ return current;
113
151
  }
114
152
  /**
115
153
  * Builds eligible static pages in one Rolldown invocation and seeds route-module disk cache.
@@ -118,9 +156,9 @@ export async function ensurePagesUnifiedGraphBuilt(options) {
118
156
  if (!shouldBuildPagesUnifiedGraph()) {
119
157
  return undefined;
120
158
  }
121
- const eligibleEntryPaths = options.entryPaths
159
+ const eligibleEntryPaths = Array.from(new Set(options.entryPaths
122
160
  .map((entryPath) => path.resolve(entryPath))
123
- .filter((entryPath) => isPagesUnifiedGraphPage(entryPath, options.appConfig));
161
+ .filter((entryPath) => isPagesUnifiedGraphPage(entryPath, options.appConfig))));
124
162
  if (eligibleEntryPaths.length === 0) {
125
163
  return undefined;
126
164
  }
@@ -155,6 +193,10 @@ export async function ensurePagesUnifiedGraphBuilt(options) {
155
193
  throw new Error(`Pages unified graph build failed: ${details}`);
156
194
  }
157
195
  const outputs = {};
196
+ const outputImports = new Set();
197
+ const directImportsCache = new Map();
198
+ const dependencyHasher = new RouteModuleDependencyHasher();
199
+ const dependencyHashes = {};
158
200
  for (const entryPath of eligibleEntryPaths) {
159
201
  const fileHash = fileSystem.hash(entryPath);
160
202
  const entryKey = entryKeysByPath.get(entryPath);
@@ -163,23 +205,32 @@ export async function ensurePagesUnifiedGraphBuilt(options) {
163
205
  throw new Error(`Pages unified graph build produced no output for ${entryPath}`);
164
206
  }
165
207
  outputs[entryPath] = compiledOutput;
208
+ const compiledOutputImports = collectReachableLocalImports(compiledOutput, { directImportsCache });
209
+ for (const importPath of compiledOutputImports)
210
+ outputImports.add(importPath);
211
+ const dependencyModulePaths = resolveRouteModuleDependencyPaths(buildResult, entryPath, options.appConfig.rootDir);
212
+ Object.assign(dependencyHashes, dependencyHasher.createDependencyHashes(dependencyModulePaths));
213
+ dependencyHashes[path.normalize(entryPath)] = fileHash;
166
214
  routeModuleBuildCache.recordBuild({
167
215
  filePath: entryPath,
168
216
  rootDir: options.appConfig.rootDir,
169
217
  outdir,
170
218
  fileHash,
171
219
  outputPath: compiledOutput,
172
- dependencyModulePaths: resolveRouteModuleDependencyPaths(buildResult, entryPath, options.appConfig.rootDir),
220
+ dependencyModulePaths,
173
221
  externalPackages: true,
174
222
  plugins,
223
+ outputImports: compiledOutputImports,
175
224
  });
176
225
  }
177
226
  const manifest = {
178
- invalidationVersion: getCorePackageVersion(),
227
+ corePackageVersion: getCorePackageVersion(),
179
228
  buildInputsFingerprint: createBuildInputsFingerprint(options.appConfig),
180
229
  buildKey: createPagesUnifiedGraphBuildKey(options.appConfig, outdir),
181
230
  builtAt: Date.now(),
182
231
  outputs,
232
+ outputImports: [...outputImports],
233
+ dependencyHashes,
183
234
  };
184
235
  writePagesUnifiedGraphManifest(options.appConfig, manifest);
185
236
  appLogger.debug(`Built pages unified graph for ${eligibleEntryPaths.length} entries`);
@@ -187,7 +238,7 @@ export async function ensurePagesUnifiedGraphBuilt(options) {
187
238
  }
188
239
  export async function importPagesUnifiedGraphModule(appConfig, filePath) {
189
240
  const manifest = readPagesUnifiedGraphManifest(appConfig);
190
- if (!manifest) {
241
+ if (!manifest || !isGraphImportable(manifest, appConfig)) {
191
242
  return undefined;
192
243
  }
193
244
  const resolvedFilePath = path.resolve(filePath);
@@ -3,7 +3,7 @@ import type { EcoPagesAppConfig } from '../../types/internal-types.js';
3
3
  * Shared manifest fields for persisted production build caches.
4
4
  */
5
5
  export interface ProductionCacheManifestBase {
6
- invalidationVersion?: string;
6
+ /** `getCorePackageVersion()` of the build that wrote the manifest. */
7
7
  corePackageVersion?: string;
8
8
  buildInputsFingerprint?: string;
9
9
  buildKey?: string;
@@ -16,6 +16,6 @@ export declare function getProductionBuildCacheManifestPaths(appConfig: EcoPages
16
16
  export declare function clearPersistedProductionBuildCacheManifests(appConfig: EcoPagesAppConfig): void;
17
17
  export declare function readProductionCacheManifest<T extends ProductionCacheManifestBase>(manifestPath: string): T | undefined;
18
18
  export declare function writeProductionCacheManifest(manifestPath: string, manifest: unknown): void;
19
- export declare function isProductionCacheManifestCurrent(manifest: ProductionCacheManifestBase | undefined, packageVersion: string): boolean;
19
+ export declare function isProductionCacheManifestCurrent(manifest: ProductionCacheManifestBase | undefined, corePackageVersion: string): boolean;
20
20
  export declare function matchesProductionCacheFingerprint(manifest: ProductionCacheManifestBase | undefined, buildInputsFingerprint: string): boolean;
21
21
  export declare function matchesProductionCacheBuildKey(manifest: ProductionCacheManifestBase | undefined, buildKey: string): boolean;
@@ -45,8 +45,8 @@ export function writeProductionCacheManifest(manifestPath, manifest) {
45
45
  fileSystem.ensureDir(path.dirname(manifestPath));
46
46
  fileSystem.write(manifestPath, `${JSON.stringify(manifest, null, '\t')}\n`);
47
47
  }
48
- export function isProductionCacheManifestCurrent(manifest, packageVersion) {
49
- return (manifest?.corePackageVersion ?? manifest?.invalidationVersion) === packageVersion;
48
+ export function isProductionCacheManifestCurrent(manifest, corePackageVersion) {
49
+ return manifest?.corePackageVersion === corePackageVersion;
50
50
  }
51
51
  export function matchesProductionCacheFingerprint(manifest, buildInputsFingerprint) {
52
52
  return manifest?.buildInputsFingerprint === buildInputsFingerprint;
@@ -1,16 +1,26 @@
1
1
  import type { BuildResult } from '../build-adapter.js';
2
2
  import type { EcoPagesAppConfig } from '../../types/internal-types.js';
3
- export { SERVER_ENTRY_BUILD_CACHE_FILENAME } from './cache-constants.js';
4
3
  export declare const SERVER_BUNDLE_MANIFEST_FILENAME = "manifest.json";
5
4
  export interface ServerBundleDeployManifest {
6
5
  serverEntry: string;
7
6
  distDir: string;
8
7
  builtAt: number;
8
+ /** Canonical source config path compiled into the server bundle. */
9
+ sourceConfigPath?: string;
10
+ /** Relative source config path from project root. */
11
+ sourceConfigRelativePath?: string;
12
+ /** Deployed config module path relative to the server bundle directory. */
13
+ emittedConfigModule?: string;
14
+ /** Content hash of the source config file when built. */
15
+ configHash?: string;
16
+ /** Content hash of the emitted production config artifact. */
17
+ emittedConfigHash?: string;
9
18
  }
10
19
  export interface ServerEntryBuildCacheManifest {
11
- invalidationVersion: string;
20
+ corePackageVersion: string;
12
21
  entryPath: string;
13
22
  entryHash: string;
23
+ configHash: string;
14
24
  buildInputsFingerprint: string;
15
25
  buildKey: string;
16
26
  outputPaths: string[];
@@ -28,7 +38,18 @@ export declare function getServerBundleOutputPaths(appConfig: EcoPagesAppConfig)
28
38
  manifestPath: string;
29
39
  };
30
40
  export declare function readServerEntryBuildCacheManifest(appConfig: EcoPagesAppConfig): ServerEntryBuildCacheManifest | undefined;
31
- export declare function writeServerBundleDeployManifest(appConfig: EcoPagesAppConfig, serverEntryPath: string): void;
41
+ export declare function writeServerBundleDeployManifest(appConfig: EcoPagesAppConfig, serverEntryPath: string, options?: Pick<ServerBundleDeployManifest, 'sourceConfigPath' | 'emittedConfigModule'> & {
42
+ outputDir?: string;
43
+ }): void;
44
+ export declare function readServerBundleDeployManifest(appConfig: EcoPagesAppConfig): ServerBundleDeployManifest | undefined;
45
+ /**
46
+ * @remarks
47
+ * Production `start` must not load a different config than the one compiled into the bundle.
48
+ */
49
+ export declare function assertProductionConfigIdentity(appConfigOrCwd: EcoPagesAppConfig | string, requestedConfigPath: string | undefined, options?: {
50
+ isExplicitOverride?: boolean;
51
+ }): void;
52
+ export declare function readServerBundleDeployManifestFromDir(cwd?: string): ServerBundleDeployManifest | undefined;
32
53
  export declare function lookupServerEntryBuildCache(options: {
33
54
  appConfig: EcoPagesAppConfig;
34
55
  entryPath: string;
@@ -38,6 +59,7 @@ export declare function recordServerEntryBuildCache(options: {
38
59
  appConfig: EcoPagesAppConfig;
39
60
  entryPath: string;
40
61
  buildResult: BuildResult;
62
+ configBuildResult?: BuildResult;
41
63
  outputPaths: string[];
42
64
  }): void;
43
65
  export declare function resolveProductionServerEntry(cwd?: string): string | undefined;