@ecopages/core 0.2.0-beta.6 → 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 (104) 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 -7
  26. package/src/adapters/shared/server-static-builder.js +89 -49
  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 +47 -3
  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/runtime-build-output-normalizer.d.ts +7 -0
  49. package/src/build/runtime-build-output-normalizer.js +85 -1
  50. package/src/build/server-entry-build-cache.d.ts +44 -0
  51. package/src/build/server-entry-build-cache.js +169 -0
  52. package/src/hmr/hmr.test.e2e.js +15 -4
  53. package/src/plugins/integration-plugin.d.ts +19 -8
  54. package/src/plugins/integration-plugin.js +7 -2
  55. package/src/plugins/processor.d.ts +5 -0
  56. package/src/plugins/processor.js +7 -0
  57. package/src/route-renderer/GRAPH.md +4 -4
  58. package/src/route-renderer/README.md +6 -5
  59. package/src/route-renderer/orchestration/foreign-subtree-execution.service.js +1 -3
  60. package/src/route-renderer/orchestration/integration-renderer.d.ts +10 -2
  61. package/src/route-renderer/orchestration/integration-renderer.js +32 -12
  62. package/src/route-renderer/orchestration/render-output.utils.d.ts +2 -0
  63. package/src/route-renderer/orchestration/render-output.utils.js +4 -0
  64. package/src/route-renderer/orchestration/route-render-orchestrator.d.ts +2 -2
  65. package/src/route-renderer/orchestration/route-render-orchestrator.js +17 -16
  66. package/src/router/client/navigation-coordinator.js +10 -0
  67. package/src/services/assets/asset-processing-service/browser-runtime-asset.factory.d.ts +1 -0
  68. package/src/services/assets/asset-processing-service/browser-runtime-asset.factory.js +1 -0
  69. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.d.ts +4 -2
  70. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.js +27 -7
  71. package/src/services/assets/asset-processing-service/processors/script/node-module-script.processor.js +17 -3
  72. package/src/services/assets/browser-bundle.service.d.ts +1 -0
  73. package/src/services/assets/browser-bundle.service.js +9 -3
  74. package/src/services/html/html-transformer.service.d.ts +1 -1
  75. package/src/services/html/html-transformer.service.js +4 -4
  76. package/src/services/invalidation/development-invalidation.service.d.ts +10 -1
  77. package/src/services/invalidation/development-invalidation.service.js +29 -0
  78. package/src/services/module-loading/README.md +49 -0
  79. package/src/services/module-loading/app-server-module-transpiler.service.js +4 -17
  80. package/src/services/module-loading/page-module-import.service.d.ts +4 -1
  81. package/src/services/module-loading/page-module-import.service.js +52 -29
  82. package/src/services/module-loading/route-module-build-cache-registry.d.ts +24 -0
  83. package/src/services/module-loading/route-module-build-cache-registry.js +37 -0
  84. package/src/services/module-loading/route-module-build-cache.d.ts +4 -0
  85. package/src/services/module-loading/route-module-build-cache.js +38 -0
  86. package/src/services/module-loading/route-module-build-cache.store.d.ts +57 -0
  87. package/src/services/module-loading/route-module-build-cache.store.js +203 -0
  88. package/src/services/module-loading/route-module-build-manifest.d.ts +61 -0
  89. package/src/services/module-loading/route-module-build-manifest.js +124 -0
  90. package/src/services/module-loading/route-module-dependency-hasher.d.ts +81 -0
  91. package/src/services/module-loading/route-module-dependency-hasher.js +121 -0
  92. package/src/services/module-loading/server-module-transpiler.service.js +1 -1
  93. package/src/static-site-generator/README.md +33 -0
  94. package/src/static-site-generator/static-build-invalidation.d.ts +10 -0
  95. package/src/static-site-generator/static-build-invalidation.js +78 -0
  96. package/src/static-site-generator/static-export-context.d.ts +22 -0
  97. package/src/static-site-generator/static-export-context.js +0 -0
  98. package/src/static-site-generator/static-site-generator.d.ts +17 -20
  99. package/src/static-site-generator/static-site-generator.js +188 -85
  100. package/src/types/internal-types.d.ts +8 -0
  101. package/src/utils/parse-cli-args.d.ts +1 -0
  102. package/src/utils/parse-cli-args.js +3 -0
  103. package/src/watchers/project-watcher.d.ts +8 -0
  104. package/src/watchers/project-watcher.js +32 -12
@@ -0,0 +1,78 @@
1
+ import path from "node:path";
2
+ import { fileSystem } from "@ecopages/file-system";
3
+ import {
4
+ createBuildInputsFingerprint,
5
+ hashAppConfigFile,
6
+ haveBuildInputsChanged
7
+ } from "../build/build-input-fingerprint.js";
8
+ import {
9
+ ROUTE_MODULE_BUILD_CACHE_FILENAME
10
+ } from "../services/module-loading/route-module-build-manifest.js";
11
+ import {
12
+ getSharedRouteModuleBuildCache,
13
+ getServerModuleBuildCacheOutdir
14
+ } from "../services/module-loading/route-module-build-cache-registry.js";
15
+ import { resolveInternalExecutionDir } from "../utils/resolve-work-dir.js";
16
+ import {
17
+ collectBuildInputContributors,
18
+ createBuildInputsFingerprint as createBuildInputsFingerprint2,
19
+ didBuildInputContributorChange,
20
+ hashAppConfigFile as hashAppConfigFile2,
21
+ haveBuildInputsChanged as haveBuildInputsChanged2
22
+ } from "../build/build-input-fingerprint.js";
23
+ function createRouteModuleStaticRenderCacheContext(appConfig) {
24
+ return {
25
+ configHash: hashAppConfigFile(appConfig),
26
+ buildInputsFingerprint: createBuildInputsFingerprint(appConfig)
27
+ };
28
+ }
29
+ function shouldResetStaticExportDirectory(appConfig, force = false) {
30
+ if (force) {
31
+ return true;
32
+ }
33
+ if (haveBuildInputsChanged(appConfig)) {
34
+ return true;
35
+ }
36
+ const routeModuleCache = getSharedRouteModuleBuildCache(getServerModuleBuildCacheOutdir(appConfig), appConfig);
37
+ return !routeModuleCache.isIncrementalStaticGenerationAvailable(
38
+ createRouteModuleStaticRenderCacheContext(appConfig)
39
+ );
40
+ }
41
+ const PRODUCTION_BUILD_CACHE_OUTDIRS = [".server-modules", ".server-route-modules"];
42
+ function clearProductionBuildCaches(appConfig) {
43
+ if (process.env.NODE_ENV !== "production") {
44
+ return;
45
+ }
46
+ const executionDir = resolveInternalExecutionDir(appConfig);
47
+ for (const subdir of PRODUCTION_BUILD_CACHE_OUTDIRS) {
48
+ const manifestPath = path.join(executionDir, subdir, ROUTE_MODULE_BUILD_CACHE_FILENAME);
49
+ if (fileSystem.exists(manifestPath)) {
50
+ fileSystem.remove(manifestPath);
51
+ }
52
+ }
53
+ const serverEntryCachePath = path.join(executionDir, ".server-entry", ROUTE_MODULE_BUILD_CACHE_FILENAME);
54
+ if (fileSystem.exists(serverEntryCachePath)) {
55
+ fileSystem.remove(serverEntryCachePath);
56
+ }
57
+ const pagesGraphCachePath = path.join(executionDir, ".server-pages-graph", ROUTE_MODULE_BUILD_CACHE_FILENAME);
58
+ if (fileSystem.exists(pagesGraphCachePath)) {
59
+ fileSystem.remove(pagesGraphCachePath);
60
+ }
61
+ for (const cache of appConfig.runtime?.routeModuleBuildCaches?.values() ?? []) {
62
+ cache.resetMemory();
63
+ }
64
+ appConfig.runtime?.routeModuleBuildCaches?.clear();
65
+ if (appConfig.runtime) {
66
+ appConfig.runtime.serverEntryBuildExecutor = void 0;
67
+ }
68
+ }
69
+ export {
70
+ clearProductionBuildCaches,
71
+ collectBuildInputContributors,
72
+ createBuildInputsFingerprint2 as createBuildInputsFingerprint,
73
+ createRouteModuleStaticRenderCacheContext,
74
+ didBuildInputContributorChange,
75
+ hashAppConfigFile2 as hashAppConfigFile,
76
+ haveBuildInputsChanged2 as haveBuildInputsChanged,
77
+ shouldResetStaticExportDirectory
78
+ };
@@ -0,0 +1,22 @@
1
+ import type { EcoPagesAppConfig } from '../types/internal-types.js';
2
+ import type { StaticRoute } from '../types/public-types.js';
3
+ import type { StaticGenerationRendererResolver } from '../route-renderer/route-renderer.js';
4
+ import type { StaticGenerationRoute } from '../router/server/route-registry.js';
5
+ type StaticGenerationRouteSource = {
6
+ listStaticGenerationRoutes(input: {
7
+ runtimeOrigin: string;
8
+ }): Promise<readonly StaticGenerationRoute[]>;
9
+ };
10
+ /**
11
+ * Context passed to integration plugins during static export lifecycle hooks.
12
+ */
13
+ export interface StaticExportContext {
14
+ appConfig: EcoPagesAppConfig;
15
+ router: StaticGenerationRouteSource;
16
+ baseUrl: string;
17
+ routeRendererFactory?: StaticGenerationRendererResolver;
18
+ staticRoutes?: StaticRoute[];
19
+ force: boolean;
20
+ preserveExportDirectory: boolean;
21
+ }
22
+ export {};
@@ -2,6 +2,7 @@ import type { EcoPagesAppConfig } from '../types/internal-types.js';
2
2
  import type { StaticRoute } from '../types/public-types.js';
3
3
  import type { PageRendererResolver, StaticGenerationRendererResolver } from '../route-renderer/route-renderer.js';
4
4
  import type { StaticGenerationRoute } from '../router/server/route-registry.js';
5
+ import type { RouteModuleBuildCache } from '../services/module-loading/route-module-build-cache.store.js';
5
6
  type StaticGenerationRouteSource = {
6
7
  listStaticGenerationRoutes(input: {
7
8
  runtimeOrigin: string;
@@ -27,26 +28,19 @@ export declare const STATIC_SITE_GENERATOR_ERRORS: {
27
28
  */
28
29
  export declare class StaticSiteGenerator {
29
30
  appConfig: EcoPagesAppConfig;
31
+ private readonly routeModuleBuildCacheOverride?;
32
+ private staticRenderCacheContext;
33
+ private forceFullStaticGeneration;
30
34
  /**
31
35
  * Creates the static-site generator for one app config.
32
36
  */
33
- constructor({ appConfig }: {
37
+ constructor({ appConfig, routeModuleBuildCache, }: {
34
38
  appConfig: EcoPagesAppConfig;
39
+ routeModuleBuildCache?: RouteModuleBuildCache;
35
40
  });
41
+ private getRouteModuleBuildCache;
36
42
  private getExportDir;
37
- /**
38
- * Logs the standardized warning emitted when a dynamic-cache page is skipped.
39
- */
40
- private warnDynamicPageSkipped;
41
- /**
42
- * Determines whether one filesystem-discovered page should be excluded from
43
- * static generation.
44
- */
45
43
  private shouldSkipStaticPageFile;
46
- /**
47
- * Determines whether one explicit static route view should be excluded from
48
- * static generation.
49
- */
50
44
  private shouldSkipStaticView;
51
45
  /**
52
46
  * Writes the robots.txt file declared by the app config.
@@ -61,32 +55,35 @@ export declare class StaticSiteGenerator {
61
55
  */
62
56
  getDirectories(routes: string[]): string[];
63
57
  private writeStaticOutput;
64
- private getStaticBuildStrategy;
58
+ private writeStaticPageArtifact;
59
+ private pruneStaleStaticOutputs;
65
60
  private createFilesystemStaticContents;
66
61
  /**
67
62
  * Generates static output for all filesystem-discovered routes.
68
63
  *
69
64
  * @remarks
70
- * Routes whose integrations opt into fetch-based static builds are rendered by
71
- * issuing a request against the running server origin. Render-strategy routes
72
- * go through the normal route renderer directly.
65
+ * Routes are rendered through the normal route renderer directly.
73
66
  */
74
- generateStaticPages(router: StaticGenerationRouteSource, baseUrl: string, routeRendererFactory?: StaticPageRouteRendererFactory): Promise<void>;
75
- private resolveStaticFetchUrl;
67
+ generateStaticPages(router: StaticGenerationRouteSource, baseUrl: string, routeRendererFactory?: StaticPageRouteRendererFactory, skipped?: string[], activeStaticPathnames?: Set<string>, preloadedRoutes?: readonly StaticGenerationRoute[]): Promise<void>;
68
+ private createStaticExportContext;
69
+ private invokeStaticExportHook;
76
70
  /**
77
71
  * Executes the full static-generation workflow for one app run.
78
72
  */
79
- run({ router, baseUrl, routeRendererFactory, staticRoutes, }: {
73
+ run({ router, baseUrl, routeRendererFactory, staticRoutes, force, preserveExportDirectory, }: {
80
74
  router: StaticGenerationRouteSource;
81
75
  baseUrl: string;
82
76
  routeRendererFactory?: StaticGenerationRendererFactory;
83
77
  staticRoutes?: StaticRoute[];
78
+ force?: boolean;
79
+ preserveExportDirectory?: boolean;
84
80
  }): Promise<void>;
85
81
  /**
86
82
  * Generates static pages from explicit static routes registered via app.static().
87
83
  * These routes use eco.page views via loader functions for HMR support.
88
84
  */
89
85
  private generateExplicitStaticPages;
86
+ private resolveExplicitViewSourceFile;
90
87
  private generateExplicitStaticRoute;
91
88
  private planExplicitStaticRoute;
92
89
  private createExplicitStaticContents;
@@ -1,8 +1,14 @@
1
1
  import path from "node:path";
2
+ import { availableParallelism } from "node:os";
2
3
  import { appLogger } from "../global/app-logger.js";
3
4
  import { fileSystem } from "@ecopages/file-system";
4
- import { PathUtils } from "../utils/path-utils.module.js";
5
5
  import { prepareExplicitStaticRender } from "../adapters/shared/explicit-static-render-preparation.js";
6
+ import { createRouteModuleStaticRenderCacheContext } from "./static-build-invalidation.js";
7
+ import {
8
+ getServerModuleBuildCacheOutdir,
9
+ getSharedRouteModuleBuildCache
10
+ } from "../services/module-loading/route-module-build-cache-registry.js";
11
+ import { ensurePagesUnifiedGraphBuilt, shouldBuildPagesUnifiedGraph } from "../build/pages-unified-graph-build.js";
6
12
  const STATIC_SITE_GENERATOR_ERRORS = {
7
13
  ROUTE_RENDERER_FACTORY_REQUIRED: "RouteRendererFactory is required for render strategy",
8
14
  unsupportedBodyType: (bodyType) => `Unsupported body type for static generation: ${bodyType}`,
@@ -10,50 +16,51 @@ const STATIC_SITE_GENERATOR_ERRORS = {
10
16
  noRendererForIntegration: (integrationName) => `No renderer found for integration: ${integrationName}`,
11
17
  dynamicRouteRequiresStaticPaths: (routePath) => `Dynamic route ${routePath} requires staticPaths to be defined on the view.`
12
18
  };
19
+ function resolveStaticPageConcurrency() {
20
+ return Math.max(1, availableParallelism() - 1);
21
+ }
22
+ async function runWithConcurrency(items, concurrency, worker) {
23
+ if (items.length === 0) {
24
+ return;
25
+ }
26
+ const limit = Math.max(1, concurrency);
27
+ let nextIndex = 0;
28
+ const runners = Array.from({ length: Math.min(limit, items.length) }, async () => {
29
+ while (nextIndex < items.length) {
30
+ const currentIndex = nextIndex++;
31
+ await worker(items[currentIndex]);
32
+ }
33
+ });
34
+ await Promise.all(runners);
35
+ }
13
36
  class StaticSiteGenerator {
14
37
  appConfig;
38
+ routeModuleBuildCacheOverride;
39
+ staticRenderCacheContext;
40
+ forceFullStaticGeneration = false;
15
41
  /**
16
42
  * Creates the static-site generator for one app config.
17
43
  */
18
- constructor({ appConfig }) {
44
+ constructor({
45
+ appConfig,
46
+ routeModuleBuildCache
47
+ }) {
19
48
  this.appConfig = appConfig;
49
+ this.routeModuleBuildCacheOverride = routeModuleBuildCache;
50
+ this.staticRenderCacheContext = createRouteModuleStaticRenderCacheContext(appConfig);
51
+ }
52
+ getRouteModuleBuildCache() {
53
+ return this.routeModuleBuildCacheOverride ?? getSharedRouteModuleBuildCache(getServerModuleBuildCacheOutdir(this.appConfig), this.appConfig);
20
54
  }
21
55
  getExportDir() {
22
56
  return this.appConfig.absolutePaths?.distDir ?? path.join(this.appConfig.rootDir, this.appConfig.distDir);
23
57
  }
24
- /**
25
- * Logs the standardized warning emitted when a dynamic-cache page is skipped.
26
- */
27
- warnDynamicPageSkipped(filePath) {
28
- appLogger.warn(
29
- "Pages with cache: 'dynamic' are not supported in static generation or preview, so they will be skipped\n",
30
- `\u27A4 ${filePath}`
31
- );
32
- }
33
- /**
34
- * Determines whether one filesystem-discovered page should be excluded from
35
- * static generation.
36
- */
37
58
  async shouldSkipStaticPageFile(filePath, routeRendererFactory) {
38
- const module = await routeRendererFactory.getPageRenderer(filePath).loadPageModule(filePath, {
39
- cacheScope: "static-page-probe"
40
- });
41
- if (module.default?.cache !== "dynamic") {
42
- return false;
43
- }
44
- this.warnDynamicPageSkipped(filePath);
45
- return true;
59
+ const module = await routeRendererFactory.getPageRenderer(filePath).loadPageModule(filePath);
60
+ return module.default?.cache === "dynamic";
46
61
  }
47
- /**
48
- * Determines whether one explicit static route view should be excluded from
49
- * static generation.
50
- */
51
- shouldSkipStaticView(routePath, view) {
52
- if (view.cache !== "dynamic") {
53
- return false;
54
- }
55
- this.warnDynamicPageSkipped(routePath);
56
- return true;
62
+ shouldSkipStaticView(_routePath, view) {
63
+ return view.cache === "dynamic";
57
64
  }
58
65
  /**
59
66
  * Writes the robots.txt file declared by the app config.
@@ -100,29 +107,56 @@ class StaticSiteGenerator {
100
107
  fileSystem.write(outputPath, contents);
101
108
  return outputPath;
102
109
  }
103
- getStaticBuildStrategy(filePath) {
104
- const ext = PathUtils.getEcoTemplateExtension(filePath);
105
- const integration = this.appConfig.integrations.find((plugin) => plugin.extensions.includes(ext));
106
- return integration?.staticBuildStep || "render";
110
+ async writeStaticPageArtifact(options) {
111
+ options.activeStaticPathnames?.add(options.pathname);
112
+ const directories = options.directories ?? this.getDirectories([options.pathname]);
113
+ const renderedOutputPath = this.getOutputPath(options.pathname, directories);
114
+ const routeModuleBuildCache = this.getRouteModuleBuildCache();
115
+ if (options.reuseRenderedOutput !== false && options.sourceFile && routeModuleBuildCache.canReuseStaticRender({
116
+ sourceFile: options.sourceFile,
117
+ pathname: options.pathname,
118
+ renderedOutputPath,
119
+ context: this.staticRenderCacheContext,
120
+ force: this.forceFullStaticGeneration
121
+ })) {
122
+ appLogger.debug(`Skipped unchanged static page: ${options.debugLabel ?? options.pathname}`);
123
+ return;
124
+ }
125
+ const contents = await options.createContents();
126
+ if (contents === null) {
127
+ return;
128
+ }
129
+ const outputPath = this.writeStaticOutput(options.pathname, contents, directories);
130
+ if (options.sourceFile) {
131
+ routeModuleBuildCache.recordStaticRender({
132
+ filePath: options.sourceFile,
133
+ pathname: options.pathname,
134
+ sourceHash: fileSystem.hash(options.sourceFile),
135
+ renderedOutputPath: outputPath,
136
+ context: this.staticRenderCacheContext
137
+ });
138
+ }
107
139
  }
108
- async createFilesystemStaticContents(route, baseUrl, routeRendererFactory) {
140
+ pruneStaleStaticOutputs(activeStaticPathnames) {
141
+ const removedOutputPaths = this.getRouteModuleBuildCache().pruneStaleRenderedOutputs(activeStaticPathnames);
142
+ for (const outputPath of removedOutputPaths) {
143
+ const resolvedOutputPath = path.isAbsolute(outputPath) ? outputPath : path.resolve(outputPath);
144
+ if (fileSystem.exists(resolvedOutputPath)) {
145
+ fileSystem.remove(resolvedOutputPath);
146
+ appLogger.debug(`Removed stale static output: ${resolvedOutputPath}`);
147
+ }
148
+ }
149
+ }
150
+ async createFilesystemStaticContents(route, _baseUrl, routeRendererFactory, skipped) {
109
151
  const {
110
152
  templateRoute: { filePath },
111
153
  params
112
154
  } = route;
113
- if (this.getStaticBuildStrategy(filePath) === "fetch") {
114
- const fetchUrl = this.resolveStaticFetchUrl(route.requestUrl, baseUrl);
115
- const response = await fetch(fetchUrl);
116
- if (!response.ok) {
117
- appLogger.error(`Failed to fetch ${fetchUrl}. Status: ${response.status}`);
118
- return null;
119
- }
120
- return response.text();
121
- }
122
155
  if (!routeRendererFactory) {
123
156
  throw new Error(STATIC_SITE_GENERATOR_ERRORS.ROUTE_RENDERER_FACTORY_REQUIRED);
124
157
  }
125
158
  if (await this.shouldSkipStaticPageFile(filePath, routeRendererFactory)) {
159
+ skipped?.push(filePath);
126
160
  return null;
127
161
  }
128
162
  const renderer = routeRendererFactory.getPageRenderer(filePath);
@@ -143,42 +177,51 @@ class StaticSiteGenerator {
143
177
  * Generates static output for all filesystem-discovered routes.
144
178
  *
145
179
  * @remarks
146
- * Routes whose integrations opt into fetch-based static builds are rendered by
147
- * issuing a request against the running server origin. Render-strategy routes
148
- * go through the normal route renderer directly.
180
+ * Routes are rendered through the normal route renderer directly.
149
181
  */
150
- async generateStaticPages(router, baseUrl, routeRendererFactory) {
151
- const routes = await router.listStaticGenerationRoutes({ runtimeOrigin: baseUrl });
182
+ async generateStaticPages(router, baseUrl, routeRendererFactory, skipped, activeStaticPathnames, preloadedRoutes) {
183
+ const routes = preloadedRoutes ?? await router.listStaticGenerationRoutes({ runtimeOrigin: baseUrl });
152
184
  appLogger.debug(
153
185
  "Static Pages",
154
186
  routes.map((route) => route.requestUrl)
155
187
  );
156
188
  const directories = this.getDirectories(routes.map((route) => route.requestUrl));
157
- for (const route of routes) {
189
+ await runWithConcurrency(routes, resolveStaticPageConcurrency(), async (route) => {
158
190
  try {
159
- const contents = await this.createFilesystemStaticContents(route, baseUrl, routeRendererFactory);
160
- if (contents === null) {
161
- continue;
162
- }
163
- this.writeStaticOutput(route.pathname, contents, directories);
191
+ await this.writeStaticPageArtifact({
192
+ pathname: route.pathname,
193
+ sourceFile: route.templateRoute.filePath,
194
+ directories,
195
+ debugLabel: route.requestUrl,
196
+ activeStaticPathnames,
197
+ createContents: () => this.createFilesystemStaticContents(route, baseUrl, routeRendererFactory, skipped)
198
+ });
164
199
  } catch (error) {
165
200
  appLogger.error(
166
201
  `Error generating static page for ${route.requestUrl}:`,
167
202
  error instanceof Error ? error : String(error)
168
203
  );
169
204
  }
170
- }
205
+ });
171
206
  }
172
- resolveStaticFetchUrl(route, baseUrl) {
173
- if (!route.startsWith("http://") && !route.startsWith("https://")) {
174
- return `${baseUrl}${route}`;
207
+ createStaticExportContext(input) {
208
+ return {
209
+ appConfig: this.appConfig,
210
+ router: input.router,
211
+ baseUrl: input.baseUrl,
212
+ routeRendererFactory: input.routeRendererFactory,
213
+ staticRoutes: input.staticRoutes,
214
+ force: input.force,
215
+ preserveExportDirectory: input.preserveExportDirectory
216
+ };
217
+ }
218
+ async invokeStaticExportHook(hook, context) {
219
+ for (const integration of this.appConfig.integrations) {
220
+ const handler = integration[hook];
221
+ if (typeof handler === "function") {
222
+ await handler.call(integration, context);
223
+ }
175
224
  }
176
- const targetUrl = new URL(route);
177
- const buildUrl = new URL(baseUrl);
178
- buildUrl.pathname = targetUrl.pathname;
179
- buildUrl.search = targetUrl.search;
180
- buildUrl.hash = targetUrl.hash;
181
- return buildUrl.href;
182
225
  }
183
226
  /**
184
227
  * Executes the full static-generation workflow for one app run.
@@ -187,19 +230,71 @@ class StaticSiteGenerator {
187
230
  router,
188
231
  baseUrl,
189
232
  routeRendererFactory,
190
- staticRoutes
233
+ staticRoutes,
234
+ force = false,
235
+ preserveExportDirectory = false
191
236
  }) {
192
- this.generateRobotsTxt();
193
- await this.generateStaticPages(router, baseUrl, routeRendererFactory);
194
- if (staticRoutes && staticRoutes.length > 0 && routeRendererFactory) {
195
- await this.generateExplicitStaticPages(staticRoutes, routeRendererFactory);
237
+ const skippedDynamicPages = [];
238
+ const activeStaticPathnames = /* @__PURE__ */ new Set();
239
+ this.forceFullStaticGeneration = force;
240
+ this.staticRenderCacheContext = createRouteModuleStaticRenderCacheContext(this.appConfig);
241
+ if (!force) {
242
+ this.getRouteModuleBuildCache().ensureIncrementalStaticGenerationContext(this.staticRenderCacheContext);
243
+ }
244
+ const routes = await router.listStaticGenerationRoutes({ runtimeOrigin: baseUrl });
245
+ if (shouldBuildPagesUnifiedGraph()) {
246
+ await ensurePagesUnifiedGraphBuilt({
247
+ appConfig: this.appConfig,
248
+ entryPaths: routes.map((route) => route.templateRoute.filePath),
249
+ outdir: getServerModuleBuildCacheOutdir(this.appConfig),
250
+ force
251
+ });
252
+ }
253
+ const staticExportContext = this.createStaticExportContext({
254
+ router,
255
+ baseUrl,
256
+ routeRendererFactory,
257
+ staticRoutes,
258
+ force,
259
+ preserveExportDirectory
260
+ });
261
+ await this.invokeStaticExportHook("beforeStaticExport", staticExportContext);
262
+ try {
263
+ this.generateRobotsTxt();
264
+ await this.generateStaticPages(
265
+ router,
266
+ baseUrl,
267
+ routeRendererFactory,
268
+ skippedDynamicPages,
269
+ activeStaticPathnames,
270
+ routes
271
+ );
272
+ if (staticRoutes && staticRoutes.length > 0 && routeRendererFactory) {
273
+ await this.generateExplicitStaticPages(
274
+ staticRoutes,
275
+ routeRendererFactory,
276
+ skippedDynamicPages,
277
+ activeStaticPathnames
278
+ );
279
+ }
280
+ if (preserveExportDirectory) {
281
+ this.pruneStaleStaticOutputs(activeStaticPathnames);
282
+ }
283
+ } finally {
284
+ await this.invokeStaticExportHook("afterStaticExport", staticExportContext);
285
+ }
286
+ if (skippedDynamicPages.length > 0) {
287
+ appLogger.debug(
288
+ `Skipped ${skippedDynamicPages.length} page(s) with cache: 'dynamic' (not supported in static generation)`,
289
+ skippedDynamicPages
290
+ );
196
291
  }
197
292
  }
198
293
  /**
199
294
  * Generates static pages from explicit static routes registered via app.static().
200
295
  * These routes use eco.page views via loader functions for HMR support.
201
296
  */
202
- async generateExplicitStaticPages(staticRoutes, routeRendererFactory) {
297
+ async generateExplicitStaticPages(staticRoutes, routeRendererFactory, skipped, activeStaticPathnames) {
203
298
  appLogger.debug(
204
299
  "Generating explicit static routes",
205
300
  staticRoutes.map((r) => r.path)
@@ -209,9 +304,10 @@ class StaticSiteGenerator {
209
304
  const mod = await route.loader();
210
305
  const view = mod.default;
211
306
  if (this.shouldSkipStaticView(route.path, view)) {
307
+ skipped?.push(route.path);
212
308
  continue;
213
309
  }
214
- await this.generateExplicitStaticRoute(route.path, view, routeRendererFactory);
310
+ await this.generateExplicitStaticRoute(route.path, view, routeRendererFactory, activeStaticPathnames);
215
311
  } catch (error) {
216
312
  appLogger.error(
217
313
  `Error generating explicit static page for ${route.path}:`,
@@ -220,18 +316,25 @@ class StaticSiteGenerator {
220
316
  }
221
317
  }
222
318
  }
223
- async generateExplicitStaticRoute(routePath, view, routeRendererFactory) {
319
+ resolveExplicitViewSourceFile(view) {
320
+ const sourceFile = view.config?.__eco?.file;
321
+ if (!sourceFile) {
322
+ return void 0;
323
+ }
324
+ return path.isAbsolute(sourceFile) ? sourceFile : path.join(this.appConfig.rootDir, sourceFile);
325
+ }
326
+ async generateExplicitStaticRoute(routePath, view, routeRendererFactory, activeStaticPathnames) {
224
327
  const { renderer, routeEntries } = await this.planExplicitStaticRoute(routePath, view, routeRendererFactory);
328
+ const sourceFile = this.resolveExplicitViewSourceFile(view);
225
329
  for (const { pathname, params } of routeEntries) {
226
- const contents = await this.createExplicitStaticContents(
227
- routePath,
228
- view,
229
- params,
230
- routeRendererFactory,
231
- renderer
232
- );
233
- const outputPath = this.writeStaticOutput(pathname, contents);
234
- appLogger.debug(`Generated static page: ${pathname} -> ${outputPath}`);
330
+ await this.writeStaticPageArtifact({
331
+ pathname,
332
+ sourceFile,
333
+ debugLabel: pathname,
334
+ activeStaticPathnames,
335
+ createContents: () => this.createExplicitStaticContents(routePath, view, params, routeRendererFactory, renderer)
336
+ });
337
+ appLogger.debug(`Generated static page: ${pathname}`);
235
338
  }
236
339
  }
237
340
  async planExplicitStaticRoute(routePath, view, routeRendererFactory) {
@@ -13,6 +13,7 @@ import type { SourceModuleLoader } from '../services/module-loading/module-loadi
13
13
  import type { EntrypointDependencyGraph } from '../services/runtime-state/entrypoint-dependency-graph.service.js';
14
14
  import type { ServerInvalidationState } from '../services/runtime-state/server-invalidation-state.service.js';
15
15
  import type { ServerModuleTranspiler } from '../services/module-loading/server-module-transpiler.service.js';
16
+ import type { RouteModuleBuildCache } from '../services/module-loading/route-module-build-cache.store.js';
16
17
  export interface RobotsPreference {
17
18
  /**
18
19
  * The user agent
@@ -150,12 +151,19 @@ export type EcoPagesAppConfig = {
150
151
  buildAdapter?: BuildAdapter;
151
152
  buildManifest?: AppBuildManifest;
152
153
  buildExecutor?: BuildExecutor;
154
+ hmrBuildExecutor?: BuildExecutor;
155
+ routeModuleBuildExecutor?: BuildExecutor;
153
156
  devGraphService?: DevGraphService;
154
157
  entrypointDependencyGraph?: EntrypointDependencyGraph;
155
158
  hostModuleLoader?: SourceModuleLoader;
156
159
  rendererModuleContext?: unknown;
157
160
  serverInvalidationState?: ServerInvalidationState;
158
161
  serverModuleTranspiler?: ServerModuleTranspiler;
162
+ routeModuleBuildCaches?: Map<string, RouteModuleBuildCache>;
163
+ /** Serialized server-entry bundler installed by {@link getInstalledServerEntryBuildExecutor}. */
164
+ serverEntryBuildExecutor?: BuildExecutor;
165
+ /** Set after {@link setupAppRuntimePlugins} runs processor/integration setup once per process. */
166
+ runtimeAssetsPrepared?: boolean;
159
167
  };
160
168
  /**
161
169
  * Experimental features.
@@ -13,6 +13,7 @@ export type ReturnParseCliArgs = {
13
13
  build: boolean;
14
14
  start: boolean;
15
15
  dev: boolean;
16
+ force: boolean;
16
17
  port?: number;
17
18
  hostname?: string;
18
19
  reactFastRefresh?: boolean;
@@ -7,6 +7,7 @@ function getEmbeddedRuntimeCommandOptions() {
7
7
  build: false,
8
8
  start: !isDevelopment,
9
9
  dev: isDevelopment,
10
+ force: false,
10
11
  port: void 0,
11
12
  hostname: void 0,
12
13
  reactFastRefresh: void 0
@@ -25,6 +26,7 @@ function parseCliArgs(options = {}) {
25
26
  dev: { type: "boolean" },
26
27
  preview: { type: "boolean" },
27
28
  build: { type: "boolean" },
29
+ force: { type: "boolean" },
28
30
  port: { type: "string" },
29
31
  hostname: { type: "string" },
30
32
  "react-fast-refresh": { type: "boolean" }
@@ -48,6 +50,7 @@ function parseCliArgs(options = {}) {
48
50
  build: isBuildCommand,
49
51
  start: isStartCommand,
50
52
  dev: isDevCommand,
53
+ force: !!values.force,
51
54
  port: values.port ? Number(values.port) : void 0,
52
55
  hostname: values.hostname,
53
56
  reactFastRefresh: values["react-fast-refresh"]
@@ -133,4 +133,12 @@ export declare class ProjectWatcher {
133
133
  * rapid file changes efficiently.
134
134
  */
135
135
  createWatcherSubscription(): Promise<FSWatcher>;
136
+ /**
137
+ * Closes the active filesystem watcher subscription.
138
+ *
139
+ * @remarks
140
+ * Safe to call multiple times. Used when tearing down dev servers in tests
141
+ * and other short-lived Ecopages runtimes.
142
+ */
143
+ close(): Promise<void>;
136
144
  }