@ecopages/core 0.2.0-beta.7 → 0.2.0-beta.8

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 (102) hide show
  1. package/package.json +26 -2
  2. package/src/adapters/abstract/application-adapter.d.ts +12 -1
  3. package/src/adapters/abstract/application-adapter.js +13 -0
  4. package/src/adapters/abstract/server-adapter.d.ts +3 -0
  5. package/src/adapters/bun/client-bridge.d.ts +1 -0
  6. package/src/adapters/bun/client-bridge.js +3 -0
  7. package/src/adapters/bun/create-app.d.ts +2 -0
  8. package/src/adapters/bun/create-app.js +28 -9
  9. package/src/adapters/bun/server-adapter.d.ts +13 -0
  10. package/src/adapters/bun/server-adapter.js +29 -4
  11. package/src/adapters/create-app.d.ts +13 -0
  12. package/src/adapters/create-app.js +10 -1
  13. package/src/adapters/node/create-app.d.ts +1 -0
  14. package/src/adapters/node/create-app.js +17 -7
  15. package/src/adapters/node/node-client-bridge.d.ts +1 -0
  16. package/src/adapters/node/node-client-bridge.js +3 -0
  17. package/src/adapters/node/server-adapter.d.ts +10 -13
  18. package/src/adapters/node/server-adapter.js +37 -82
  19. package/src/adapters/node/static-content-server.d.ts +1 -0
  20. package/src/adapters/node/static-content-server.js +3 -0
  21. package/src/adapters/shared/copy-runtime-public-dir.d.ts +6 -0
  22. package/src/adapters/shared/copy-runtime-public-dir.js +24 -0
  23. package/src/adapters/shared/runtime-app-bootstrap.d.ts +0 -1
  24. package/src/adapters/shared/runtime-app-bootstrap.js +1 -6
  25. package/src/adapters/shared/server-static-builder.d.ts +4 -6
  26. package/src/adapters/shared/server-static-builder.js +90 -34
  27. package/src/adapters/shared/shared-hmr-manager.d.ts +1 -0
  28. package/src/adapters/shared/shared-hmr-manager.js +15 -2
  29. package/src/build/README.md +44 -4
  30. package/src/build/build-adapter.d.ts +8 -2
  31. package/src/build/build-adapter.js +71 -33
  32. package/src/build/build-input-fingerprint.d.ts +28 -0
  33. package/src/build/build-input-fingerprint.js +31 -0
  34. package/src/build/jsx-ownership-plugins.d.ts +10 -0
  35. package/src/build/jsx-ownership-plugins.js +17 -0
  36. package/src/build/pages-unified-graph-build.d.ts +31 -0
  37. package/src/build/pages-unified-graph-build.js +208 -0
  38. package/src/build/parallel-build-executor.d.ts +21 -0
  39. package/src/build/parallel-build-executor.js +52 -0
  40. package/src/build/rolldown-adapter-helpers.js +5 -0
  41. package/src/build/rolldown-build-adapter.js +2 -0
  42. package/src/build/rolldown-build-invocation-metrics.d.ts +12 -0
  43. package/src/build/rolldown-build-invocation-metrics.js +43 -0
  44. package/src/build/rolldown-dev-build-adapter.d.ts +2 -2
  45. package/src/build/rolldown-dev-build-adapter.js +30 -14
  46. package/src/build/runtime-build-executor.d.ts +7 -13
  47. package/src/build/runtime-build-executor.js +31 -7
  48. package/src/build/server-entry-build-cache.d.ts +44 -0
  49. package/src/build/server-entry-build-cache.js +169 -0
  50. package/src/hmr/hmr.test.e2e.js +15 -4
  51. package/src/plugins/integration-plugin.d.ts +19 -8
  52. package/src/plugins/integration-plugin.js +7 -2
  53. package/src/plugins/processor.d.ts +5 -0
  54. package/src/plugins/processor.js +7 -0
  55. package/src/route-renderer/GRAPH.md +4 -4
  56. package/src/route-renderer/README.md +6 -5
  57. package/src/route-renderer/orchestration/foreign-subtree-execution.service.js +1 -3
  58. package/src/route-renderer/orchestration/integration-renderer.d.ts +10 -2
  59. package/src/route-renderer/orchestration/integration-renderer.js +32 -12
  60. package/src/route-renderer/orchestration/render-output.utils.d.ts +2 -0
  61. package/src/route-renderer/orchestration/render-output.utils.js +4 -0
  62. package/src/route-renderer/orchestration/route-render-orchestrator.d.ts +2 -2
  63. package/src/route-renderer/orchestration/route-render-orchestrator.js +17 -16
  64. package/src/router/client/navigation-coordinator.js +10 -0
  65. package/src/services/assets/asset-processing-service/browser-runtime-asset.factory.d.ts +1 -0
  66. package/src/services/assets/asset-processing-service/browser-runtime-asset.factory.js +1 -0
  67. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.d.ts +4 -2
  68. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.js +27 -7
  69. package/src/services/assets/asset-processing-service/processors/script/node-module-script.processor.js +17 -3
  70. package/src/services/assets/browser-bundle.service.d.ts +1 -0
  71. package/src/services/assets/browser-bundle.service.js +9 -3
  72. package/src/services/html/html-transformer.service.d.ts +1 -1
  73. package/src/services/html/html-transformer.service.js +4 -4
  74. package/src/services/invalidation/development-invalidation.service.d.ts +10 -1
  75. package/src/services/invalidation/development-invalidation.service.js +29 -0
  76. package/src/services/module-loading/README.md +49 -0
  77. package/src/services/module-loading/app-server-module-transpiler.service.js +4 -17
  78. package/src/services/module-loading/page-module-import.service.d.ts +4 -1
  79. package/src/services/module-loading/page-module-import.service.js +52 -29
  80. package/src/services/module-loading/route-module-build-cache-registry.d.ts +24 -0
  81. package/src/services/module-loading/route-module-build-cache-registry.js +37 -0
  82. package/src/services/module-loading/route-module-build-cache.d.ts +4 -0
  83. package/src/services/module-loading/route-module-build-cache.js +38 -0
  84. package/src/services/module-loading/route-module-build-cache.store.d.ts +57 -0
  85. package/src/services/module-loading/route-module-build-cache.store.js +203 -0
  86. package/src/services/module-loading/route-module-build-manifest.d.ts +61 -0
  87. package/src/services/module-loading/route-module-build-manifest.js +124 -0
  88. package/src/services/module-loading/route-module-dependency-hasher.d.ts +81 -0
  89. package/src/services/module-loading/route-module-dependency-hasher.js +121 -0
  90. package/src/services/module-loading/server-module-transpiler.service.js +1 -1
  91. package/src/static-site-generator/README.md +33 -0
  92. package/src/static-site-generator/static-build-invalidation.d.ts +10 -0
  93. package/src/static-site-generator/static-build-invalidation.js +78 -0
  94. package/src/static-site-generator/static-export-context.d.ts +22 -0
  95. package/src/static-site-generator/static-export-context.js +0 -0
  96. package/src/static-site-generator/static-site-generator.d.ts +17 -8
  97. package/src/static-site-generator/static-site-generator.js +176 -62
  98. package/src/types/internal-types.d.ts +8 -0
  99. package/src/utils/parse-cli-args.d.ts +1 -0
  100. package/src/utils/parse-cli-args.js +3 -0
  101. package/src/watchers/project-watcher.d.ts +8 -0
  102. package/src/watchers/project-watcher.js +32 -12
@@ -3,12 +3,27 @@ import { pathToFileURL } from "node:url";
3
3
  import { fileSystem } from "@ecopages/file-system";
4
4
  import { build } from "../../build/build-adapter.js";
5
5
  import { normalizeNodeRuntimeBuildOutputFile } from "../../build/runtime-build-output-normalizer.js";
6
+ import {
7
+ importPagesUnifiedGraphModule,
8
+ isPagesUnifiedGraphPage,
9
+ shouldBuildPagesUnifiedGraph
10
+ } from "../../build/pages-unified-graph-build.js";
11
+ import { recordPageModuleBuildInvocation } from "../../build/rolldown-build-invocation-metrics.js";
12
+ import {
13
+ resolvePageModuleOutputFileName,
14
+ createPluginCacheKey,
15
+ createJsxCacheKey
16
+ } from "./route-module-build-cache.js";
17
+ import { getSharedRouteModuleBuildCache } from "./route-module-build-cache-registry.js";
18
+ import { resolveRouteModuleDependencyPaths } from "./route-module-dependency-hasher.js";
6
19
  import { supportsSourceModuleLoading } from "./source-module-support.js";
7
20
  class PageModuleImportService {
21
+ appConfig;
8
22
  dependencies;
9
23
  importCache = /* @__PURE__ */ new Map();
10
24
  developmentInvalidationVersion = 0;
11
- constructor(dependencies) {
25
+ constructor(appConfig, dependencies) {
26
+ this.appConfig = appConfig;
12
27
  this.dependencies = {
13
28
  hashFile: dependencies?.hashFile ?? ((filePath) => fileSystem.hash(filePath)),
14
29
  buildModule: dependencies?.buildModule ?? ((options, buildExecutor) => build(options, buildExecutor)),
@@ -70,6 +85,7 @@ class PageModuleImportService {
70
85
  runtime,
71
86
  filePath,
72
87
  options.rootDir,
88
+ path.resolve(options.outdir),
73
89
  splitting ?? "default",
74
90
  externalPackages ?? "default",
75
91
  options.cacheScope ?? "default",
@@ -104,11 +120,32 @@ class PageModuleImportService {
104
120
  transpileErrorMessage = (details) => `Error transpiling page module: ${details}`,
105
121
  noOutputMessage = (targetFilePath) => `No transpiled output generated for page module: ${targetFilePath}`
106
122
  } = options;
107
- const fileBaseName = path.basename(filePath, path.extname(filePath));
108
- const cacheScopeSuffix = cacheScope ? `-${sanitizeCacheScope(cacheScope)}` : "";
109
- const invalidationSuffix = shouldVersionBuildOutputPath(invalidationVersion) ? `-v${invalidationVersion}` : "";
110
- const outputFileName = `${fileBaseName}-${fileHash}${cacheScopeSuffix}${invalidationSuffix}.mjs`;
111
- const outputNamingTemplate = `${fileBaseName}-${fileHash}${cacheScopeSuffix}${invalidationSuffix}.[ext]`;
123
+ const outputFileName = resolvePageModuleOutputFileName({
124
+ filePath,
125
+ fileHash,
126
+ cacheScope,
127
+ invalidationVersion
128
+ });
129
+ const outputNamingTemplate = outputFileName.replace(/\.mjs$/u, ".[ext]");
130
+ const preferredOutputPath = path.join(outdir, outputFileName);
131
+ const routeModuleBuildCache = this.getRouteModuleBuildCache(outdir);
132
+ const cachedBuild = routeModuleBuildCache.lookup({
133
+ ...options,
134
+ fileHash
135
+ });
136
+ if (cachedBuild) {
137
+ return await import(
138
+ /* @vite-ignore */
139
+ pathToFileURL(cachedBuild.outputPath).href
140
+ );
141
+ }
142
+ if (!cacheScope && shouldBuildPagesUnifiedGraph() && this.appConfig && isPagesUnifiedGraphPage(filePath, this.appConfig)) {
143
+ const graphModule = await importPagesUnifiedGraphModule(this.appConfig, filePath);
144
+ if (graphModule !== void 0) {
145
+ return graphModule;
146
+ }
147
+ }
148
+ recordPageModuleBuildInvocation();
112
149
  const buildResult = await this.dependencies.buildModule(
113
150
  {
114
151
  entrypoints: [filePath],
@@ -131,12 +168,17 @@ class PageModuleImportService {
131
168
  const details = buildResult.logs.map((log) => log.message).join(" | ");
132
169
  throw new Error(transpileErrorMessage(details));
133
170
  }
134
- const preferredOutputPath = path.join(outdir, outputFileName);
135
171
  const compiledOutput = buildResult.outputs.find((output) => output.path === preferredOutputPath)?.path ?? buildResult.outputs.find((output) => /\.(?:[cm]?js)$/u.test(output.path))?.path;
136
172
  if (!compiledOutput) {
137
173
  throw new Error(noOutputMessage(filePath));
138
174
  }
139
175
  normalizeNodeRuntimeBuildOutputFile(compiledOutput, rootDir);
176
+ routeModuleBuildCache.recordBuild({
177
+ ...options,
178
+ fileHash,
179
+ outputPath: compiledOutput,
180
+ dependencyModulePaths: resolveRouteModuleDependencyPaths(buildResult, filePath, rootDir)
181
+ });
140
182
  const compiledOutputUrl = pathToFileURL(compiledOutput);
141
183
  if (shouldAddRuntimeUpdateQuery(invalidationVersion, cacheScope)) {
142
184
  compiledOutputUrl.searchParams.set(
@@ -149,6 +191,9 @@ class PageModuleImportService {
149
191
  compiledOutputUrl.href
150
192
  );
151
193
  }
194
+ getRouteModuleBuildCache(outdir) {
195
+ return getSharedRouteModuleBuildCache(outdir, this.appConfig);
196
+ }
152
197
  }
153
198
  function createRuntimeModuleUrl(filePath, fileHash, invalidationVersion, cacheScope) {
154
199
  const moduleUrl = pathToFileURL(filePath);
@@ -163,31 +208,9 @@ function createRuntimeModuleUrl(filePath, fileHash, invalidationVersion, cacheSc
163
208
  function shouldAddRuntimeUpdateQuery(invalidationVersion, cacheScope) {
164
209
  return process.env.NODE_ENV === "development" || invalidationVersion > 0 || !!cacheScope;
165
210
  }
166
- function shouldVersionBuildOutputPath(invalidationVersion) {
167
- return typeof Bun !== "undefined" && invalidationVersion > 0;
168
- }
169
211
  function sanitizeCacheScope(cacheScope) {
170
212
  return cacheScope.replace(/[^a-zA-Z0-9_-]+/g, "-");
171
213
  }
172
- function createJsxCacheKey(jsx) {
173
- if (!jsx) {
174
- return "jsx:default";
175
- }
176
- return JSON.stringify({
177
- development: jsx.development ?? false,
178
- factory: jsx.factory ?? null,
179
- fragment: jsx.fragment ?? null,
180
- importSource: jsx.importSource ?? null,
181
- runtime: jsx.runtime ?? null,
182
- sideEffects: jsx.sideEffects ?? null
183
- });
184
- }
185
- function createPluginCacheKey(plugins) {
186
- if (!plugins || plugins.length === 0) {
187
- return "plugins:default";
188
- }
189
- return `plugins:${plugins.map((plugin) => plugin.name).join(",")}`;
190
- }
191
214
  export {
192
215
  PageModuleImportService
193
216
  };
@@ -0,0 +1,24 @@
1
+ import type { EcoPagesAppConfig } from '../../types/internal-types.js';
2
+ import { RouteModuleBuildCache } from './route-module-build-cache.store.js';
3
+ /**
4
+ * Returns the outdir whose transpile cache backs incremental static generation.
5
+ *
6
+ * @remarks
7
+ * Static pages render through `.eco/.server-modules`. The manifest there carries
8
+ * import-graph hashes from transpile, so static-render reuse must read the same
9
+ * directory rather than `.server-route-modules`, which only serves route-registry
10
+ * scanning.
11
+ */
12
+ export declare function getServerModuleBuildCacheOutdir(appConfig: EcoPagesAppConfig): string;
13
+ /**
14
+ * @deprecated Use {@link getServerModuleBuildCacheOutdir}.
15
+ */
16
+ export declare const getRouteModuleBuildCacheOutdir: typeof getServerModuleBuildCacheOutdir;
17
+ /**
18
+ * Returns the shared production build cache for one server-module outdir.
19
+ *
20
+ * @remarks
21
+ * One app-scoped instance prevents manifest drift between page imports and static
22
+ * generation during the same build process.
23
+ */
24
+ export declare function getSharedRouteModuleBuildCache(outdir: string, appConfig?: EcoPagesAppConfig): RouteModuleBuildCache;
@@ -0,0 +1,37 @@
1
+ import path from "node:path";
2
+ import { resolveInternalExecutionDir } from "../../utils/resolve-work-dir.js";
3
+ import { RouteModuleBuildCache } from "./route-module-build-cache.store.js";
4
+ function getServerModuleBuildCacheOutdir(appConfig) {
5
+ return path.join(resolveInternalExecutionDir(appConfig), ".server-modules");
6
+ }
7
+ const getRouteModuleBuildCacheOutdir = getServerModuleBuildCacheOutdir;
8
+ function getRouteModuleBuildCacheMap(appConfig) {
9
+ const existing = appConfig.runtime?.routeModuleBuildCaches;
10
+ if (existing) {
11
+ return existing;
12
+ }
13
+ const routeModuleBuildCaches = /* @__PURE__ */ new Map();
14
+ appConfig.runtime = {
15
+ ...appConfig.runtime ?? {},
16
+ routeModuleBuildCaches
17
+ };
18
+ return routeModuleBuildCaches;
19
+ }
20
+ function getSharedRouteModuleBuildCache(outdir, appConfig) {
21
+ if (!appConfig) {
22
+ return new RouteModuleBuildCache(outdir);
23
+ }
24
+ const caches = getRouteModuleBuildCacheMap(appConfig);
25
+ const existingCache = caches.get(outdir);
26
+ if (existingCache) {
27
+ return existingCache;
28
+ }
29
+ const routeModuleBuildCache = new RouteModuleBuildCache(outdir);
30
+ caches.set(outdir, routeModuleBuildCache);
31
+ return routeModuleBuildCache;
32
+ }
33
+ export {
34
+ getRouteModuleBuildCacheOutdir,
35
+ getServerModuleBuildCacheOutdir,
36
+ getSharedRouteModuleBuildCache
37
+ };
@@ -0,0 +1,4 @@
1
+ export type { RouteModuleDependencyHashes } from './route-module-dependency-hasher.js';
2
+ export { ROUTE_MODULE_BUILD_CACHE_FILENAME, createEmptyRouteModuleBuildCacheManifest, createPersistedRouteModuleBuildKey, createPluginCacheKey, createJsxCacheKey, hashPluginSetup, getCorePackageVersion, normalizeRouteModuleCachePath, readRouteModuleBuildCacheManifest, resolvePageModuleOutputFileName, shouldPersistRouteModuleBuildCache, writeRouteModuleBuildCacheManifest, type RouteModuleBuildCacheEntry, type RouteModuleBuildCacheLookup, type RouteModuleBuildCacheManifest, type RouteModuleStaticRenderCacheContext, type RouteModuleStaticRenderCacheEntry, } from './route-module-build-manifest.js';
3
+ export { RouteModuleBuildCache } from './route-module-build-cache.store.js';
4
+ export { getRouteModuleBuildCacheOutdir, getServerModuleBuildCacheOutdir, getSharedRouteModuleBuildCache, } from './route-module-build-cache-registry.js';
@@ -0,0 +1,38 @@
1
+ import {
2
+ ROUTE_MODULE_BUILD_CACHE_FILENAME,
3
+ createEmptyRouteModuleBuildCacheManifest,
4
+ createPersistedRouteModuleBuildKey,
5
+ createPluginCacheKey,
6
+ createJsxCacheKey,
7
+ hashPluginSetup,
8
+ getCorePackageVersion,
9
+ normalizeRouteModuleCachePath,
10
+ readRouteModuleBuildCacheManifest,
11
+ resolvePageModuleOutputFileName,
12
+ shouldPersistRouteModuleBuildCache,
13
+ writeRouteModuleBuildCacheManifest
14
+ } from "./route-module-build-manifest.js";
15
+ import { RouteModuleBuildCache } from "./route-module-build-cache.store.js";
16
+ import {
17
+ getRouteModuleBuildCacheOutdir,
18
+ getServerModuleBuildCacheOutdir,
19
+ getSharedRouteModuleBuildCache
20
+ } from "./route-module-build-cache-registry.js";
21
+ export {
22
+ ROUTE_MODULE_BUILD_CACHE_FILENAME,
23
+ RouteModuleBuildCache,
24
+ createEmptyRouteModuleBuildCacheManifest,
25
+ createJsxCacheKey,
26
+ createPersistedRouteModuleBuildKey,
27
+ createPluginCacheKey,
28
+ getCorePackageVersion,
29
+ getRouteModuleBuildCacheOutdir,
30
+ getServerModuleBuildCacheOutdir,
31
+ getSharedRouteModuleBuildCache,
32
+ hashPluginSetup,
33
+ normalizeRouteModuleCachePath,
34
+ readRouteModuleBuildCacheManifest,
35
+ resolvePageModuleOutputFileName,
36
+ shouldPersistRouteModuleBuildCache,
37
+ writeRouteModuleBuildCacheManifest
38
+ };
@@ -0,0 +1,57 @@
1
+ import type { PageModuleBuildImportOptions } from './page-module-import.service.js';
2
+ import { RouteModuleDependencyHasher, type RouteModuleDependencyHashes } from './route-module-dependency-hasher.js';
3
+ import { type RouteModuleBuildCacheLookup, type RouteModuleBuildCacheManifest, type RouteModuleStaticRenderCacheContext } from './route-module-build-manifest.js';
4
+ export type { RouteModuleDependencyHashes };
5
+ type RouteModuleBuildCacheDependencies = {
6
+ readManifest: (manifestPath: string) => RouteModuleBuildCacheManifest | undefined;
7
+ writeManifest: (manifestPath: string, manifest: RouteModuleBuildCacheManifest) => void;
8
+ exists: (filePath: string) => boolean;
9
+ getCorePackageVersion: () => string;
10
+ createDependencyHasher: () => RouteModuleDependencyHasher;
11
+ };
12
+ /**
13
+ * Persists route-module transpile and static-render metadata under one server outdir.
14
+ */
15
+ export declare class RouteModuleBuildCache {
16
+ private readonly manifestPath;
17
+ private readonly dependencies;
18
+ private readonly dependencyHasher;
19
+ private manifest;
20
+ private manifestLoaded;
21
+ constructor(outdir: string, dependencies?: Partial<RouteModuleBuildCacheDependencies>);
22
+ lookup(options: PageModuleBuildImportOptions & {
23
+ fileHash: string;
24
+ }): RouteModuleBuildCacheLookup | undefined;
25
+ recordBuild(options: PageModuleBuildImportOptions & {
26
+ fileHash: string;
27
+ outputPath: string;
28
+ dependencyModulePaths?: readonly string[];
29
+ }): void;
30
+ isIncrementalStaticGenerationAvailable(context: RouteModuleStaticRenderCacheContext): boolean;
31
+ ensureIncrementalStaticGenerationContext(context: RouteModuleStaticRenderCacheContext): void;
32
+ canReuseStaticRender(options: {
33
+ sourceFile: string;
34
+ pathname: string;
35
+ renderedOutputPath: string;
36
+ context: RouteModuleStaticRenderCacheContext;
37
+ force?: boolean;
38
+ }): boolean;
39
+ lookupStaticRender(options: {
40
+ filePath: string;
41
+ pathname: string;
42
+ sourceHash: string;
43
+ renderedOutputPath: string;
44
+ context: RouteModuleStaticRenderCacheContext;
45
+ }): boolean;
46
+ recordStaticRender(options: {
47
+ filePath: string;
48
+ pathname: string;
49
+ sourceHash: string;
50
+ renderedOutputPath: string;
51
+ context: RouteModuleStaticRenderCacheContext;
52
+ }): void;
53
+ resetMemory(): void;
54
+ pruneStaleRenderedOutputs(activePathnames: ReadonlySet<string>): string[];
55
+ private loadManifest;
56
+ private persistManifest;
57
+ }
@@ -0,0 +1,203 @@
1
+ import path from "node:path";
2
+ import { fileSystem } from "@ecopages/file-system";
3
+ import { RouteModuleDependencyHasher } from "./route-module-dependency-hasher.js";
4
+ import {
5
+ ROUTE_MODULE_BUILD_CACHE_FILENAME,
6
+ createEmptyRouteModuleBuildCacheManifest,
7
+ createPersistedRouteModuleBuildKey,
8
+ getCorePackageVersion,
9
+ normalizeRouteModuleCachePath,
10
+ readRouteModuleBuildCacheManifest,
11
+ shouldPersistRouteModuleBuildCache,
12
+ writeRouteModuleBuildCacheManifest
13
+ } from "./route-module-build-manifest.js";
14
+ class RouteModuleBuildCache {
15
+ manifestPath;
16
+ dependencies;
17
+ dependencyHasher;
18
+ manifest;
19
+ manifestLoaded = false;
20
+ constructor(outdir, dependencies) {
21
+ this.manifestPath = path.join(outdir, ROUTE_MODULE_BUILD_CACHE_FILENAME);
22
+ this.dependencies = {
23
+ readManifest: dependencies?.readManifest ?? readRouteModuleBuildCacheManifest,
24
+ writeManifest: dependencies?.writeManifest ?? writeRouteModuleBuildCacheManifest,
25
+ exists: dependencies?.exists ?? ((filePath) => fileSystem.exists(filePath)),
26
+ getCorePackageVersion: dependencies?.getCorePackageVersion ?? getCorePackageVersion,
27
+ createDependencyHasher: dependencies?.createDependencyHasher ?? (() => new RouteModuleDependencyHasher())
28
+ };
29
+ this.dependencyHasher = this.dependencies.createDependencyHasher();
30
+ }
31
+ lookup(options) {
32
+ if (!shouldPersistRouteModuleBuildCache(options)) {
33
+ return void 0;
34
+ }
35
+ const manifest = this.loadManifest();
36
+ if (manifest.invalidationVersion !== this.dependencies.getCorePackageVersion()) {
37
+ return void 0;
38
+ }
39
+ const buildKey = createPersistedRouteModuleBuildKey(options);
40
+ const cacheFilePath = normalizeRouteModuleCachePath(options.filePath);
41
+ const entry = manifest.entries[cacheFilePath];
42
+ if (!entry || entry.sourceHash !== options.fileHash || entry.buildKey !== buildKey) {
43
+ return void 0;
44
+ }
45
+ if (!this.dependencyHasher.matchesStoredHashes(entry.dependencyHashes, cacheFilePath, options.fileHash)) {
46
+ return void 0;
47
+ }
48
+ const outputPath = path.isAbsolute(entry.outputPath) ? entry.outputPath : path.join(options.outdir, entry.outputPath);
49
+ if (!this.dependencies.exists(outputPath)) {
50
+ return void 0;
51
+ }
52
+ return { outputPath, entry };
53
+ }
54
+ recordBuild(options) {
55
+ if (!shouldPersistRouteModuleBuildCache(options)) {
56
+ return;
57
+ }
58
+ const cacheFilePath = normalizeRouteModuleCachePath(options.filePath);
59
+ const dependencyModulePaths = options.dependencyModulePaths && options.dependencyModulePaths.length > 0 ? options.dependencyModulePaths : [cacheFilePath];
60
+ const dependencyHashes = this.dependencyHasher.createDependencyHashes(dependencyModulePaths);
61
+ dependencyHashes[cacheFilePath] = options.fileHash;
62
+ const manifest = this.loadManifest();
63
+ const existingEntry = manifest.entries[cacheFilePath];
64
+ manifest.invalidationVersion = this.dependencies.getCorePackageVersion();
65
+ manifest.entries[cacheFilePath] = {
66
+ sourceHash: options.fileHash,
67
+ outputPath: options.outputPath,
68
+ builtAt: Date.now(),
69
+ buildKey: createPersistedRouteModuleBuildKey(options),
70
+ dependencyHashes,
71
+ renderedOutputs: existingEntry?.renderedOutputs
72
+ };
73
+ this.persistManifest(manifest);
74
+ }
75
+ isIncrementalStaticGenerationAvailable(context) {
76
+ if (process.env.NODE_ENV !== "production") {
77
+ return false;
78
+ }
79
+ const manifest = this.loadManifest();
80
+ if (manifest.invalidationVersion !== this.dependencies.getCorePackageVersion()) {
81
+ return false;
82
+ }
83
+ return manifest.configHash === context.configHash && manifest.buildInputsFingerprint === context.buildInputsFingerprint;
84
+ }
85
+ ensureIncrementalStaticGenerationContext(context) {
86
+ if (process.env.NODE_ENV !== "production") {
87
+ return;
88
+ }
89
+ const manifest = this.loadManifest();
90
+ manifest.invalidationVersion = this.dependencies.getCorePackageVersion();
91
+ manifest.configHash = context.configHash;
92
+ manifest.buildInputsFingerprint = context.buildInputsFingerprint;
93
+ this.persistManifest(manifest);
94
+ }
95
+ canReuseStaticRender(options) {
96
+ if (options.force) {
97
+ return false;
98
+ }
99
+ if (!fileSystem.exists(options.sourceFile)) {
100
+ return false;
101
+ }
102
+ const sourceHash = fileSystem.hash(options.sourceFile);
103
+ return this.lookupStaticRender({
104
+ filePath: options.sourceFile,
105
+ pathname: options.pathname,
106
+ sourceHash,
107
+ renderedOutputPath: options.renderedOutputPath,
108
+ context: options.context
109
+ });
110
+ }
111
+ lookupStaticRender(options) {
112
+ if (!this.isIncrementalStaticGenerationAvailable(options.context)) {
113
+ return false;
114
+ }
115
+ const manifest = this.loadManifest();
116
+ const cacheFilePath = normalizeRouteModuleCachePath(options.filePath);
117
+ const entry = manifest.entries[cacheFilePath];
118
+ const renderedOutput = entry?.renderedOutputs?.[options.pathname];
119
+ if (!entry || !renderedOutput) {
120
+ return false;
121
+ }
122
+ if (entry.sourceHash !== options.sourceHash || renderedOutput.sourceHash !== options.sourceHash) {
123
+ return false;
124
+ }
125
+ if (!this.dependencyHasher.matchesStoredHashes(entry.dependencyHashes, cacheFilePath, options.sourceHash)) {
126
+ return false;
127
+ }
128
+ const cachedOutputPath = path.isAbsolute(renderedOutput.renderedOutputPath) ? renderedOutput.renderedOutputPath : path.resolve(renderedOutput.renderedOutputPath);
129
+ if (cachedOutputPath !== path.resolve(options.renderedOutputPath)) {
130
+ return false;
131
+ }
132
+ return this.dependencies.exists(cachedOutputPath);
133
+ }
134
+ recordStaticRender(options) {
135
+ if (process.env.NODE_ENV !== "production") {
136
+ return;
137
+ }
138
+ const cacheFilePath = normalizeRouteModuleCachePath(options.filePath);
139
+ const manifest = this.loadManifest();
140
+ const existingEntry = manifest.entries[cacheFilePath];
141
+ manifest.invalidationVersion = this.dependencies.getCorePackageVersion();
142
+ manifest.configHash = options.context.configHash;
143
+ manifest.buildInputsFingerprint = options.context.buildInputsFingerprint;
144
+ manifest.entries[cacheFilePath] = {
145
+ sourceHash: options.sourceHash,
146
+ outputPath: existingEntry?.outputPath ?? "",
147
+ builtAt: existingEntry?.builtAt ?? Date.now(),
148
+ buildKey: existingEntry?.buildKey ?? "",
149
+ dependencyHashes: existingEntry?.dependencyHashes,
150
+ renderedOutputs: {
151
+ ...existingEntry?.renderedOutputs ?? {},
152
+ [options.pathname]: {
153
+ renderedOutputPath: options.renderedOutputPath,
154
+ renderedAt: Date.now(),
155
+ sourceHash: options.sourceHash
156
+ }
157
+ }
158
+ };
159
+ this.persistManifest(manifest);
160
+ }
161
+ resetMemory() {
162
+ this.manifest = void 0;
163
+ this.manifestLoaded = false;
164
+ }
165
+ pruneStaleRenderedOutputs(activePathnames) {
166
+ if (process.env.NODE_ENV !== "production") {
167
+ return [];
168
+ }
169
+ const manifest = this.loadManifest();
170
+ const removedOutputPaths = [];
171
+ for (const entry of Object.values(manifest.entries)) {
172
+ if (!entry.renderedOutputs) {
173
+ continue;
174
+ }
175
+ for (const [pathname, renderedOutput] of Object.entries(entry.renderedOutputs)) {
176
+ if (activePathnames.has(pathname)) {
177
+ continue;
178
+ }
179
+ removedOutputPaths.push(renderedOutput.renderedOutputPath);
180
+ delete entry.renderedOutputs[pathname];
181
+ }
182
+ }
183
+ if (removedOutputPaths.length > 0) {
184
+ this.persistManifest(manifest);
185
+ }
186
+ return removedOutputPaths;
187
+ }
188
+ loadManifest() {
189
+ if (this.manifestLoaded) {
190
+ return this.manifest ?? createEmptyRouteModuleBuildCacheManifest();
191
+ }
192
+ this.manifestLoaded = true;
193
+ this.manifest = this.dependencies.readManifest(this.manifestPath) ?? createEmptyRouteModuleBuildCacheManifest();
194
+ return this.manifest;
195
+ }
196
+ persistManifest(manifest) {
197
+ this.manifest = manifest;
198
+ this.dependencies.writeManifest(this.manifestPath, manifest);
199
+ }
200
+ }
201
+ export {
202
+ RouteModuleBuildCache
203
+ };
@@ -0,0 +1,61 @@
1
+ import type { EcoBuildPlugin } from '../../build/build-types.js';
2
+ import type { PageModuleBuildImportOptions } from './page-module-import.service.js';
3
+ import type { RouteModuleDependencyHashes } from './route-module-dependency-hasher.js';
4
+ /** Filename written beside transpiled server modules that stores incremental build metadata. */
5
+ export declare const ROUTE_MODULE_BUILD_CACHE_FILENAME = ".build-cache.json";
6
+ /** One persisted route-module build entry in {@link ROUTE_MODULE_BUILD_CACHE_FILENAME}. */
7
+ export interface RouteModuleBuildCacheEntry {
8
+ sourceHash: string;
9
+ outputPath: string;
10
+ builtAt: number;
11
+ buildKey: string;
12
+ dependencyHashes?: RouteModuleDependencyHashes;
13
+ renderedOutputs?: Record<string, RouteModuleStaticRenderCacheEntry>;
14
+ }
15
+ /** Metadata for one rendered static HTML artifact derived from a route module. */
16
+ export interface RouteModuleStaticRenderCacheEntry {
17
+ renderedOutputPath: string;
18
+ renderedAt: number;
19
+ sourceHash: string;
20
+ }
21
+ /** On-disk manifest describing all cached route-module builds for one server outdir. */
22
+ export interface RouteModuleBuildCacheManifest {
23
+ invalidationVersion: string;
24
+ configHash?: string;
25
+ buildInputsFingerprint?: string;
26
+ entries: Record<string, RouteModuleBuildCacheEntry>;
27
+ }
28
+ /** Successful route-module cache lookup result. */
29
+ export interface RouteModuleBuildCacheLookup {
30
+ outputPath: string;
31
+ entry: RouteModuleBuildCacheEntry;
32
+ }
33
+ /** Inputs that must remain stable for incremental static HTML generation. */
34
+ export interface RouteModuleStaticRenderCacheContext {
35
+ configHash: string;
36
+ buildInputsFingerprint: string;
37
+ }
38
+ export type RouteModuleBuildCacheManifestReader = (manifestPath: string) => RouteModuleBuildCacheManifest | undefined;
39
+ export type RouteModuleBuildCacheManifestWriter = (manifestPath: string, manifest: RouteModuleBuildCacheManifest) => void;
40
+ /** Returns the installed `@ecopages/core` package version used to invalidate persisted caches. */
41
+ export declare function getCorePackageVersion(): string;
42
+ /** Normalizes filesystem paths used as manifest keys and build-key inputs. */
43
+ export declare function normalizeRouteModuleCachePath(filePath: string): string;
44
+ /** Returns whether route-module disk caching should run for the current import. */
45
+ export declare function shouldPersistRouteModuleBuildCache(options: PageModuleBuildImportOptions): boolean;
46
+ /** Derives the deterministic on-disk filename for one transpiled route module. */
47
+ export declare function resolvePageModuleOutputFileName(options: {
48
+ filePath: string;
49
+ fileHash: string;
50
+ cacheScope?: string;
51
+ invalidationVersion?: number;
52
+ }): string;
53
+ /** Builds the cache key for persisted production route-module builds. */
54
+ export declare function createPersistedRouteModuleBuildKey(options: PageModuleBuildImportOptions): string;
55
+ export declare function createEmptyRouteModuleBuildCacheManifest(): RouteModuleBuildCacheManifest;
56
+ export declare function readRouteModuleBuildCacheManifest(manifestPath: string): RouteModuleBuildCacheManifest | undefined;
57
+ export declare function writeRouteModuleBuildCacheManifest(manifestPath: string, manifest: RouteModuleBuildCacheManifest): void;
58
+ export declare function createJsxCacheKey(jsx: PageModuleBuildImportOptions['jsx']): string;
59
+ export declare function createPluginCacheKey(plugins?: EcoBuildPlugin[]): string;
60
+ /** Exported for in-process import caches that must match persisted build keys. */
61
+ export declare function hashPluginSetup(setup: EcoBuildPlugin['setup']): string;