@ecopages/core 0.5.0 → 0.6.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 (128) hide show
  1. package/README.md +17 -13
  2. package/package.json +13 -4
  3. package/src/adapters/create-app.js +9 -5
  4. package/src/adapters/node/create-app.d.ts +18 -16
  5. package/src/adapters/node/create-app.js +26 -30
  6. package/src/adapters/node/server-adapter.d.ts +26 -9
  7. package/src/adapters/node/server-adapter.js +38 -8
  8. package/src/adapters/shared/hmr/hmr-manager.js +3 -2
  9. package/src/adapters/shared/http/dist-file-handler.js +25 -7
  10. package/src/adapters/shared/runtime/render-context.d.ts +2 -1
  11. package/src/adapters/shared/runtime/render-context.js +18 -21
  12. package/src/adapters/shared/runtime/runtime-server-lifecycle.d.ts +4 -3
  13. package/src/adapters/shared/runtime/runtime-server-lifecycle.js +6 -4
  14. package/src/adapters/shared/runtime/server-request-pipeline.d.ts +6 -1
  15. package/src/adapters/shared/runtime/server-request-pipeline.js +10 -10
  16. package/src/adapters/shared/runtime/server-static-builder.d.ts +11 -11
  17. package/src/adapters/shared/runtime/server-static-builder.js +37 -18
  18. package/src/assets/app-browser-client-manifest.d.ts +1 -3
  19. package/src/assets/app-browser-client-manifest.js +5 -58
  20. package/src/assets/asset-processing-service/processors/script/file-script.processor.d.ts +2 -2
  21. package/src/assets/asset-processing-service/processors/script/file-script.processor.js +2 -2
  22. package/src/assets/browser-asset-output.d.ts +1 -1
  23. package/src/assets/browser-asset-output.js +1 -1
  24. package/src/build/app-build-manifest-runtime.d.ts +11 -1
  25. package/src/build/app-build-manifest-runtime.js +17 -3
  26. package/src/build/cache/pages-unified-graph-build.d.ts +32 -35
  27. package/src/build/cache/pages-unified-graph-build.js +206 -124
  28. package/src/build/cache/server-module-entries.d.ts +35 -0
  29. package/src/build/cache/server-module-entries.js +109 -0
  30. package/src/build/contracts/build-contracts.d.ts +5 -0
  31. package/src/build/copy-server-files.d.ts +10 -0
  32. package/src/build/copy-server-files.js +37 -0
  33. package/src/build/preserve-import-meta-transform.d.ts +1 -1
  34. package/src/build/preserve-import-meta-transform.js +1 -1
  35. package/src/build/production-build-id.d.ts +9 -0
  36. package/src/build/production-build-id.js +19 -0
  37. package/src/build/production-dist-complete.d.ts +5 -0
  38. package/src/build/production-dist-complete.js +101 -0
  39. package/src/build/production-route-list.d.ts +26 -0
  40. package/src/build/production-route-list.js +78 -0
  41. package/src/build/rolldown/rolldown-adapter-helpers.d.ts +2 -2
  42. package/src/build/rolldown/rolldown-adapter-helpers.js +56 -4
  43. package/src/build/rolldown/rolldown-build-adapter.js +1 -1
  44. package/src/build/runtime/build-request-identity.js +2 -1
  45. package/src/build/runtime/build-request-policy.d.ts +3 -2
  46. package/src/build/runtime/build-request-policy.js +5 -3
  47. package/src/build/server/import-view-module.d.ts +11 -0
  48. package/src/build/server/import-view-module.js +21 -0
  49. package/src/build/server/imported-view-plugin.d.ts +16 -0
  50. package/src/build/server/imported-view-plugin.js +59 -0
  51. package/src/build/server/page-module-import.service.d.ts +19 -20
  52. package/src/build/server/page-module-import.service.js +65 -36
  53. package/src/build/server/resolve-static-view.d.ts +9 -0
  54. package/src/build/server/resolve-static-view.js +28 -0
  55. package/src/build/server/route-module-build-cache-registry.d.ts +5 -3
  56. package/src/build/server/route-module-build-cache-registry.js +5 -3
  57. package/src/build/server/server-modules-manifest.d.ts +51 -0
  58. package/src/build/server/server-modules-manifest.js +129 -0
  59. package/src/build/server/view-reference.d.ts +38 -0
  60. package/src/build/server/view-reference.js +112 -0
  61. package/src/build/server-bundle-deploy-manifest.d.ts +9 -9
  62. package/src/build/server-bundle-deploy-manifest.js +24 -28
  63. package/src/build.d.ts +3 -1
  64. package/src/build.js +3 -1
  65. package/src/config/assembly/app-runtime.d.ts +15 -2
  66. package/src/config/assembly/app-runtime.js +3 -1
  67. package/src/config/assembly/finalize-config.js +64 -13
  68. package/src/config/index.d.ts +1 -0
  69. package/src/config/index.js +1 -0
  70. package/src/config/internal-types.d.ts +6 -0
  71. package/src/config/load-eco-config.d.ts +1 -0
  72. package/src/config/load-eco-config.js +11 -2
  73. package/src/config/resolve-eco-config-path.d.ts +2 -1
  74. package/src/config/resolve-eco-config-path.js +21 -15
  75. package/src/config/user-config-types.d.ts +9 -2
  76. package/src/html-pages/html-page-module.d.ts +19 -0
  77. package/src/html-pages/html-page-module.js +66 -13
  78. package/src/html-pages/html-page-renderer.d.ts +4 -0
  79. package/src/html-pages/html-page-renderer.js +9 -3
  80. package/src/html-pages/html-pages.plugin.d.ts +10 -0
  81. package/src/html-pages/html-pages.plugin.js +7 -1
  82. package/src/index.d.ts +2 -0
  83. package/src/index.js +2 -0
  84. package/src/integration.d.ts +2 -1
  85. package/src/integration.js +1 -0
  86. package/src/page-cache/page-cache-service.d.ts +8 -0
  87. package/src/page-cache/page-cache-service.js +12 -0
  88. package/src/plugins/integration-plugin.d.ts +33 -6
  89. package/src/plugins/integration-plugin.js +14 -2
  90. package/src/plugins/processor.d.ts +17 -0
  91. package/src/plugins/processor.js +20 -1
  92. package/src/plugins/server-dom.d.ts +29 -0
  93. package/src/plugins/server-dom.js +36 -0
  94. package/src/route-renderer/error-pages/error-page-renderer.d.ts +6 -4
  95. package/src/route-renderer/error-pages/error-page-renderer.js +9 -5
  96. package/src/route-renderer/orchestration/integration-renderer.d.ts +1 -0
  97. package/src/route-renderer/orchestration/integration-renderer.js +17 -2
  98. package/src/route-renderer/orchestration/page-browser-graph/missing-production-browser-graph.d.ts +21 -0
  99. package/src/route-renderer/orchestration/page-browser-graph/missing-production-browser-graph.js +31 -0
  100. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.js +58 -24
  101. package/src/route-renderer/orchestration/page-browser-graph/route-instance-key.d.ts +12 -0
  102. package/src/route-renderer/orchestration/page-browser-graph/route-instance-key.js +55 -0
  103. package/src/router/server/route-registry.d.ts +22 -2
  104. package/src/router/server/route-registry.js +39 -15
  105. package/src/static-site-generator/production-page-browser-graph-prebuild.d.ts +8 -2
  106. package/src/static-site-generator/production-page-browser-graph-prebuild.js +14 -9
  107. package/src/static-site-generator/static-site-generator.d.ts +19 -3
  108. package/src/static-site-generator/static-site-generator.js +78 -40
  109. package/src/types/contracts.d.ts +20 -31
  110. package/src/types/public-types.d.ts +1 -0
  111. package/src/types/view-reference.d.ts +31 -0
  112. package/src/types/view-reference.js +15 -0
  113. package/src/utils/additional-watch-paths.d.ts +3 -3
  114. package/src/utils/additional-watch-paths.js +3 -1
  115. package/src/utils/resolve-entry-file.d.ts +4 -2
  116. package/src/utils/resolve-entry-file.js +4 -2
  117. package/src/utils/root-relative-path.d.ts +4 -0
  118. package/src/utils/root-relative-path.js +8 -0
  119. package/src/utils/server-bundle-context.d.ts +14 -0
  120. package/src/utils/server-bundle-context.js +45 -0
  121. package/src/watchers/project-watcher-ignore.d.ts +2 -5
  122. package/src/watchers/project-watcher-ignore.js +2 -5
  123. package/src/watchers/project-watcher.d.ts +9 -7
  124. package/src/watchers/project-watcher.js +100 -52
  125. package/src/watchers/workspace-package-watch-roots.d.ts +4 -4
  126. package/src/watchers/workspace-package-watch-roots.js +41 -25
  127. package/src/build/server/view-module-loader.d.ts +0 -10
  128. package/src/build/server/view-module-loader.js +0 -46
@@ -4,6 +4,7 @@ import { createAliasResolverPlugin } from '../../plugins/alias-resolver-plugin.j
4
4
  import { resolveBuildEnvironmentOptions } from './build-environment-options.js';
5
5
  import { getJsxOwnershipPlugins } from '../browser/jsx-ownership-plugins.js';
6
6
  import { createEcoComponentMetaPlugin } from '../../plugins/eco-component-meta-plugin.js';
7
+ import { isProductionRuntime } from '../../env.js';
7
8
  import { createPreserveImportMetaPlugin } from '../preserve-import-meta-transform.js';
8
9
  /**
9
10
  * App-owned plugins of one environment, in the order their hooks run.
@@ -78,14 +79,15 @@ export function resolveBuildPluginOrder(appConfig, environment, options) {
78
79
  * themselves. Plugins follow {@link resolveBuildPluginOrder}, so server module loading shares browser
79
80
  * attribution. Every server request
80
81
  * also gets {@link createPreserveImportMetaPlugin} for `runtimeOutdir` (or
81
- * `outdir`), so bundled modules, Core included, keep reading files relative to
82
- * their sources from `dist/.server/` and `.eco/.server-modules/`.
82
+ * `outdir`) in development, so compiled modules in `.eco/.server-modules/`
83
+ * keep reading files next to their sources. Production emit leaves
84
+ * `import.meta` pointing at the files in `dist/server/`.
83
85
  */
84
86
  export function createServerBuildRequest(appConfig, input) {
85
87
  const importMetaDir = input.runtimeOutdir ?? input.outdir;
86
88
  const plugins = resolveBuildPluginOrder(appConfig, 'server', {
87
89
  callerPlugins: input.plugins,
88
- coreTransforms: importMetaDir ? [createPreserveImportMetaPlugin(importMetaDir)] : [],
90
+ coreTransforms: importMetaDir && !isProductionRuntime() ? [createPreserveImportMetaPlugin(importMetaDir)] : [],
89
91
  });
90
92
  const { plugins: _callerPlugins, ...overrides } = input;
91
93
  return {
@@ -0,0 +1,11 @@
1
+ import type { EcoPagesAppConfig } from '../../config/internal-types.js';
2
+ import type { ViewLoader } from '../../types/contracts.js';
3
+ /**
4
+ * Loads a view through the app module loader.
5
+ *
6
+ * @remarks
7
+ * Development compiles from source with watcher invalidation. Production imports
8
+ * the module `ecopages build` wrote to the server-modules map.
9
+ */
10
+ export declare function importViewModule<T = unknown>(appConfig: EcoPagesAppConfig, filePath: string): Promise<T>;
11
+ export declare function createViewLoaderFromFilePath<P = Record<string, unknown>>(appConfig: EcoPagesAppConfig, filePath: string): ViewLoader<P>;
@@ -0,0 +1,21 @@
1
+ import { getServerModuleBuildCacheOutdir } from './route-module-build-cache-registry.js';
2
+ import { requireAppRuntime } from '../../utils/app-runtime.js';
3
+ /**
4
+ * Loads a view through the app module loader.
5
+ *
6
+ * @remarks
7
+ * Development compiles from source with watcher invalidation. Production imports
8
+ * the module `ecopages build` wrote to the server-modules map.
9
+ */
10
+ export async function importViewModule(appConfig, filePath) {
11
+ return await requireAppRuntime(appConfig).moduleLoader.importModule({
12
+ filePath,
13
+ outdir: getServerModuleBuildCacheOutdir(appConfig),
14
+ externalPackages: true,
15
+ });
16
+ }
17
+ export function createViewLoaderFromFilePath(appConfig, filePath) {
18
+ const loader = async () => await importViewModule(appConfig, filePath);
19
+ loader.filePath = filePath;
20
+ return loader;
21
+ }
@@ -0,0 +1,16 @@
1
+ import type { EcoBuildPlugin } from '../contracts/build-types.js';
2
+ import type { EcoPagesAppConfig } from '../../config/internal-types.js';
3
+ export declare const IMPORTED_VIEW_PLUGIN_NAME = "ecopages-imported-views";
4
+ /** Paths the server-entry plugin turned into view references for this app. */
5
+ export declare function getImportedViewPaths(appConfig: EcoPagesAppConfig): readonly string[];
6
+ /**
7
+ * Turns Integration-owned imports in the server-entry graph into view references.
8
+ *
9
+ * @remarks
10
+ * Register only on the server-entry bundle. Page and route-module builds must
11
+ * still compile those files. Recorded paths join the production server-modules
12
+ * map so `ctx.render` views are not compiled at request time.
13
+ */
14
+ export declare function createImportedViewPlugin(appConfig: EcoPagesAppConfig, options?: {
15
+ entryPath?: string;
16
+ }): EcoBuildPlugin;
@@ -0,0 +1,59 @@
1
+ import path from 'node:path';
2
+ import { collectViewIntegrations, findViewOwner, isExemptFromViewReference, viewExtensionFilter, viewReferenceModuleSource, } from './view-reference.js';
3
+ export const IMPORTED_VIEW_PLUGIN_NAME = 'ecopages-imported-views';
4
+ const importedViewPaths = new WeakMap();
5
+ /** Paths the server-entry plugin turned into view references for this app. */
6
+ export function getImportedViewPaths(appConfig) {
7
+ return [...(importedViewPaths.get(appConfig) ?? [])];
8
+ }
9
+ function recordImportedViewPath(appConfig, filePath) {
10
+ const resolvedPath = path.resolve(filePath);
11
+ const paths = importedViewPaths.get(appConfig);
12
+ if (paths) {
13
+ paths.add(resolvedPath);
14
+ return;
15
+ }
16
+ importedViewPaths.set(appConfig, new Set([resolvedPath]));
17
+ }
18
+ /**
19
+ * Turns Integration-owned imports in the server-entry graph into view references.
20
+ *
21
+ * @remarks
22
+ * Register only on the server-entry bundle. Page and route-module builds must
23
+ * still compile those files. Recorded paths join the production server-modules
24
+ * map so `ctx.render` views are not compiled at request time.
25
+ */
26
+ export function createImportedViewPlugin(appConfig, options = {}) {
27
+ const integrations = collectViewIntegrations(appConfig.integrations);
28
+ const extensions = [...new Set(integrations.flatMap((integration) => [...integration.extensions]))];
29
+ const entryPath = options.entryPath ? path.resolve(options.entryPath) : undefined;
30
+ if (extensions.length === 0) {
31
+ return {
32
+ name: IMPORTED_VIEW_PLUGIN_NAME,
33
+ environments: ['server'],
34
+ setup() { },
35
+ };
36
+ }
37
+ const filter = viewExtensionFilter(extensions);
38
+ return {
39
+ name: IMPORTED_VIEW_PLUGIN_NAME,
40
+ environments: ['server'],
41
+ cacheKey: JSON.stringify(extensions),
42
+ setup(build) {
43
+ build.onLoad({ filter }, (args) => {
44
+ if (isExemptFromViewReference(args.path, { entryPath, appRoot: appConfig.rootDir })) {
45
+ return undefined;
46
+ }
47
+ const owner = findViewOwner(integrations, args.path);
48
+ if (!owner) {
49
+ return undefined;
50
+ }
51
+ recordImportedViewPath(appConfig, args.path);
52
+ return {
53
+ contents: viewReferenceModuleSource(args.path, owner.name),
54
+ loader: 'js',
55
+ };
56
+ });
57
+ },
58
+ };
59
+ }
@@ -42,19 +42,15 @@ export interface PageModuleImportDependencies {
42
42
  buildModule(options: BuildOptions): Promise<BuildResult>;
43
43
  }
44
44
  /**
45
- * Loads page-like modules through the Ecopages build pipeline.
45
+ * Loads page-like modules for route scanning, page data loading, and request-time inspection.
46
46
  *
47
- * This service centralizes the shared build-first import strategy used by route
48
- * scanning, page data loading, and request-time page inspection.
49
- *
50
- * Keeping this logic in one place prevents subtle drift in cache-busting,
51
- * transpilation settings, and error semantics across the different callers.
47
+ * Production imports the module `ecopages build` emitted. Development compiles
48
+ * from source through the Ecopages build pipeline.
52
49
  */
53
50
  export declare class PageModuleImportService {
54
51
  private readonly appConfig;
55
52
  private readonly dependencies;
56
53
  private readonly importCache;
57
- private readonly productionFileHashes;
58
54
  private recordPipelineLoad;
59
55
  constructor(appConfig: EcoPagesAppConfig, dependencies?: Partial<PageModuleImportDependencies>);
60
56
  /**
@@ -72,12 +68,14 @@ export declare class PageModuleImportService {
72
68
  */
73
69
  invalidateDevelopmentGraph(): void;
74
70
  /**
75
- * Imports a page-like module from source.
71
+ * Imports a page-like module.
76
72
  *
77
- * The caller controls the output directory and error wording so different
78
- * subsystems can reuse the same loading mechanism while preserving their
79
- * current diagnostics. Module identities stay stable for unchanged files and
80
- * roll forward automatically when the source hash changes during watch mode.
73
+ * Production loads the module `ecopages build` emitted. Development compiles
74
+ * from source. The caller controls the output directory and error wording so
75
+ * different subsystems can reuse the same loading mechanism while preserving
76
+ * their current diagnostics. Development module identities stay stable for
77
+ * unchanged files and roll forward automatically when the source hash changes
78
+ * during watch mode.
81
79
  *
82
80
  * @typeParam T Expected module shape.
83
81
  * @param options Runtime-specific import settings.
@@ -85,18 +83,19 @@ export declare class PageModuleImportService {
85
83
  */
86
84
  importModule<T = unknown>(options: PageModuleBuildImportOptions): Promise<T>;
87
85
  /**
88
- * Imports a module whose sources cannot change while the process runs.
86
+ * Imports a compiled server module emitted by `ecopages build`.
89
87
  *
90
88
  * @remarks
91
- * In production nothing edits the sources and no watcher invalidates them: every build
92
- * compiles from source in a fresh process. The entry source hash is read once per service and
93
- * memoized for the build identity. An in-memory hit therefore avoids file hashing; after that
94
- * cache is cleared, the route-module cache may still hash tracked dependencies to validate its
95
- * compiled output.
89
+ * Production serving never compiles: a missing map entry or missing file is an
90
+ * error that names the source and says to run `ecopages build`. During
91
+ * `ecopages build`, before the map exists on disk, listing routes still compiles
92
+ * from source. `bypassCache` only skips the in-memory import promise, not the
93
+ * manifest. A path outside the deploy root still matches when it ends with a
94
+ * recorded source key, so `identity.file` from the build machine can load.
96
95
  */
97
- private importUnchangedModule;
96
+ private importFromProductionManifest;
97
+ private loadProductionManifestModule;
98
98
  private importWithCache;
99
- private getProductionFileHash;
100
99
  private resolveRouteModuleOutputNaming;
101
100
  private createRouteModuleBuildOptions;
102
101
  private createRouteModuleCacheBuildOptions;
@@ -6,7 +6,7 @@ import { requireAppRuntime } from '../../utils/app-runtime.js';
6
6
  import {} from '../../build/build-adapter.js';
7
7
  import { requireBuildQueue } from '../../build/runtime/build-queue.js';
8
8
  import { createServerBuildRequest } from '../../build/runtime/build-request-policy.js';
9
- import { importPagesUnifiedGraphModule, isPagesUnifiedGraphPage, shouldBuildPagesUnifiedGraph, } from '../../build/cache/pages-unified-graph-build.js';
9
+ import { importEmittedServerModule, importPagesUnifiedGraphModule, isPagesUnifiedGraphPage, } from '../../build/cache/pages-unified-graph-build.js';
10
10
  import { findIntegrationForFile } from '../../plugins/find-integration-for-file.js';
11
11
  import { recordPageModuleBuildInvocation } from '../../build/rolldown/rolldown-build-invocation-metrics.js';
12
12
  import { recordPageModuleLoad } from '../../diagnostics/request-pipeline-metrics.js';
@@ -15,20 +15,18 @@ import { getSharedRouteModuleBuildCache } from './route-module-build-cache-regis
15
15
  import { resolveRouteModuleDependencyPaths } from './route-module-dependency-hasher.js';
16
16
  import { assertBuildSucceeded } from '../../build/build-log.js';
17
17
  import { isDevelopmentRuntime, isProductionRuntime } from '../../utils/runtime.js';
18
+ import { toRootRelativePath } from '../../utils/root-relative-path.js';
19
+ import { loadProductionServerModules, matchProductionModuleKey, missingProductionServerModuleError, } from './server-modules-manifest.js';
18
20
  /**
19
- * Loads page-like modules through the Ecopages build pipeline.
21
+ * Loads page-like modules for route scanning, page data loading, and request-time inspection.
20
22
  *
21
- * This service centralizes the shared build-first import strategy used by route
22
- * scanning, page data loading, and request-time page inspection.
23
- *
24
- * Keeping this logic in one place prevents subtle drift in cache-busting,
25
- * transpilation settings, and error semantics across the different callers.
23
+ * Production imports the module `ecopages build` emitted. Development compiles
24
+ * from source through the Ecopages build pipeline.
26
25
  */
27
26
  export class PageModuleImportService {
28
27
  appConfig;
29
28
  dependencies;
30
29
  importCache = new Map();
31
- productionFileHashes = new Map();
32
30
  recordPipelineLoad(outcome, artifactCount = 0) {
33
31
  recordPageModuleLoad(requireAppRuntime(this.appConfig).requestPipelineMetrics, outcome, artifactCount);
34
32
  }
@@ -61,38 +59,78 @@ export class PageModuleImportService {
61
59
  this.clearImportCache();
62
60
  }
63
61
  /**
64
- * Imports a page-like module from source.
62
+ * Imports a page-like module.
65
63
  *
66
- * The caller controls the output directory and error wording so different
67
- * subsystems can reuse the same loading mechanism while preserving their
68
- * current diagnostics. Module identities stay stable for unchanged files and
69
- * roll forward automatically when the source hash changes during watch mode.
64
+ * Production loads the module `ecopages build` emitted. Development compiles
65
+ * from source. The caller controls the output directory and error wording so
66
+ * different subsystems can reuse the same loading mechanism while preserving
67
+ * their current diagnostics. Development module identities stay stable for
68
+ * unchanged files and roll forward automatically when the source hash changes
69
+ * during watch mode.
70
70
  *
71
71
  * @typeParam T Expected module shape.
72
72
  * @param options Runtime-specific import settings.
73
73
  * @returns The loaded module.
74
74
  */
75
75
  async importModule(options) {
76
- const { filePath } = options;
77
- if (!options.bypassCache && isProductionRuntime()) {
78
- return this.importUnchangedModule(options);
76
+ if (isProductionRuntime() && loadProductionServerModules(this.appConfig)) {
77
+ return this.importFromProductionManifest(options);
79
78
  }
80
- const fileHash = this.dependencies.hashFile(filePath);
79
+ const fileHash = this.dependencies.hashFile(options.filePath);
81
80
  return this.importWithCache(options, fileHash, !options.bypassCache);
82
81
  }
83
82
  /**
84
- * Imports a module whose sources cannot change while the process runs.
83
+ * Imports a compiled server module emitted by `ecopages build`.
85
84
  *
86
85
  * @remarks
87
- * In production nothing edits the sources and no watcher invalidates them: every build
88
- * compiles from source in a fresh process. The entry source hash is read once per service and
89
- * memoized for the build identity. An in-memory hit therefore avoids file hashing; after that
90
- * cache is cleared, the route-module cache may still hash tracked dependencies to validate its
91
- * compiled output.
86
+ * Production serving never compiles: a missing map entry or missing file is an
87
+ * error that names the source and says to run `ecopages build`. During
88
+ * `ecopages build`, before the map exists on disk, listing routes still compiles
89
+ * from source. `bypassCache` only skips the in-memory import promise, not the
90
+ * manifest. A path outside the deploy root still matches when it ends with a
91
+ * recorded source key, so `identity.file` from the build machine can load.
92
92
  */
93
- async importUnchangedModule(options) {
94
- const fileHash = this.getProductionFileHash(options.filePath);
95
- return this.importWithCache(options, fileHash, true);
93
+ async importFromProductionManifest(options) {
94
+ const manifest = loadProductionServerModules(this.appConfig);
95
+ const sourceKey = (manifest && matchProductionModuleKey(this.appConfig.rootDir, options.filePath, manifest.modules)) ??
96
+ toRootRelativePath(this.appConfig.rootDir, options.filePath);
97
+ const relativePath = manifest?.modules[sourceKey];
98
+ const cacheKey = `production::${relativePath ? path.resolve(manifest.outdir, relativePath) : sourceKey}`;
99
+ if (!options.bypassCache) {
100
+ const cachedModule = this.importCache.get(cacheKey);
101
+ if (cachedModule) {
102
+ const loadedModule = (await cachedModule.promise);
103
+ this.recordPipelineLoad('import-cache-hit');
104
+ return loadedModule;
105
+ }
106
+ }
107
+ const importPromise = this.loadProductionManifestModule(sourceKey);
108
+ if (!options.bypassCache) {
109
+ this.importCache.set(cacheKey, { promise: importPromise });
110
+ }
111
+ try {
112
+ return await importPromise;
113
+ }
114
+ catch (error) {
115
+ this.importCache.delete(cacheKey);
116
+ throw error;
117
+ }
118
+ }
119
+ async loadProductionManifestModule(sourceKey) {
120
+ const manifest = loadProductionServerModules(this.appConfig);
121
+ if (!manifest) {
122
+ throw missingProductionServerModuleError(sourceKey);
123
+ }
124
+ const relativePath = manifest.modules[sourceKey];
125
+ if (!relativePath) {
126
+ throw missingProductionServerModuleError(sourceKey);
127
+ }
128
+ const outputPath = path.join(manifest.outdir, relativePath);
129
+ if (!fileSystem.exists(outputPath)) {
130
+ throw missingProductionServerModuleError(sourceKey);
131
+ }
132
+ this.recordPipelineLoad('unified-graph');
133
+ return importEmittedServerModule(this.appConfig, path.resolve(this.appConfig.rootDir, sourceKey), outputPath);
96
134
  }
97
135
  async importWithCache(options, fileHash, cacheEnabled) {
98
136
  if (!cacheEnabled) {
@@ -128,15 +166,6 @@ export class PageModuleImportService {
128
166
  throw error;
129
167
  }
130
168
  }
131
- getProductionFileHash(filePath) {
132
- const cached = this.productionFileHashes.get(filePath);
133
- if (cached !== undefined) {
134
- return cached;
135
- }
136
- const fileHash = this.dependencies.hashFile(filePath);
137
- this.productionFileHashes.set(filePath, fileHash);
138
- return fileHash;
139
- }
140
169
  resolveRouteModuleOutputNaming(filePath, fileHash) {
141
170
  const outputFileName = createRuntimeBuildOutputFileName(resolvePageModuleOutputFileName({ filePath, fileHash }), this.getDevelopmentGeneration());
142
171
  return outputFileName.replace(/\.mjs$/u, '.[ext]');
@@ -179,7 +208,7 @@ export class PageModuleImportService {
179
208
  return loadedModule;
180
209
  }
181
210
  async tryLoadPagesUnifiedGraphModule(filePath) {
182
- if (!shouldBuildPagesUnifiedGraph() || !isPagesUnifiedGraphPage(filePath, this.appConfig)) {
211
+ if (!isProductionRuntime() || !isPagesUnifiedGraphPage(filePath, this.appConfig)) {
183
212
  return undefined;
184
213
  }
185
214
  const graphModule = await importPagesUnifiedGraphModule(this.appConfig, filePath);
@@ -0,0 +1,9 @@
1
+ import type { EcoPagesAppConfig } from '../../config/internal-types.js';
2
+ import type { EcoPageComponent, ViewLoader } from '../../types/contracts.js';
3
+ import { type ViewReference } from '../../types/view-reference.js';
4
+ export type StaticViewInput<P = Record<string, unknown>> = ViewLoader<P> | ViewReference<P> | EcoPageComponent<P>;
5
+ /**
6
+ * Turns an imported view, an inline `eco.page` component, or a loader into the
7
+ * lazy loader `app.static` and error pages store.
8
+ */
9
+ export declare function resolveStaticView<P>(appConfig: EcoPagesAppConfig, view: StaticViewInput<P>): ViewLoader<P>;
@@ -0,0 +1,28 @@
1
+ import { getComponentIdentity } from '../../eco/component-identity.js';
2
+ import { isViewReference } from '../../types/view-reference.js';
3
+ import { createViewLoaderFromFilePath } from './import-view-module.js';
4
+ const PATH_VIEW_REMOVED = 'String and URL view paths are removed. Import the view and pass it to app.static(), ctx.render(), or the error-page helpers.';
5
+ /**
6
+ * Turns an imported view, an inline `eco.page` component, or a loader into the
7
+ * lazy loader `app.static` and error pages store.
8
+ */
9
+ export function resolveStaticView(appConfig, view) {
10
+ if (typeof view === 'string' || view instanceof URL) {
11
+ throw new Error(PATH_VIEW_REMOVED);
12
+ }
13
+ if (isViewReference(view)) {
14
+ return createViewLoaderFromFilePath(appConfig, view.filePath);
15
+ }
16
+ if (typeof view === 'function' && 'config' in view && view.config) {
17
+ const loader = async () => ({ default: view });
18
+ const filePath = getComponentIdentity(view)?.file;
19
+ if (filePath) {
20
+ loader.filePath = filePath;
21
+ }
22
+ return loader;
23
+ }
24
+ if (typeof view === 'function') {
25
+ return view;
26
+ }
27
+ throw new Error('app.static and error pages expect an imported view, an eco.page component, or a view loader.');
28
+ }
@@ -1,11 +1,13 @@
1
1
  import type { EcoPagesAppConfig } from '../../config/internal-types.js';
2
2
  import { RouteModuleBuildCache } from './route-module-build-cache.store.js';
3
3
  /**
4
- * Returns the outdir whose transpile cache backs in-process static generation.
4
+ * Returns the outdir of route-module server builds and their transpile cache.
5
5
  *
6
6
  * @remarks
7
- * Static pages render through `.eco/.server-modules`; `.server-route-modules` only
8
- * serves route-registry scanning.
7
+ * Development builds every server module here. Production imports the modules emitted
8
+ * into `dist/server/` and builds here only for a module the server-modules map does not
9
+ * list, or for every module when `ECOPAGES_UNIFIED_PAGES_GRAPH` disables the map.
10
+ * `.server-route-modules` only serves route-registry scanning.
9
11
  */
10
12
  export declare function getServerModuleBuildCacheOutdir(appConfig: EcoPagesAppConfig): string;
11
13
  /**
@@ -4,11 +4,13 @@ import { requireAppRuntime } from '../../utils/app-runtime.js';
4
4
  import { resolveInternalExecutionDir } from '../../utils/resolve-work-dir.js';
5
5
  import { RouteModuleBuildCache } from './route-module-build-cache.store.js';
6
6
  /**
7
- * Returns the outdir whose transpile cache backs in-process static generation.
7
+ * Returns the outdir of route-module server builds and their transpile cache.
8
8
  *
9
9
  * @remarks
10
- * Static pages render through `.eco/.server-modules`; `.server-route-modules` only
11
- * serves route-registry scanning.
10
+ * Development builds every server module here. Production imports the modules emitted
11
+ * into `dist/server/` and builds here only for a module the server-modules map does not
12
+ * list, or for every module when `ECOPAGES_UNIFIED_PAGES_GRAPH` disables the map.
13
+ * `.server-route-modules` only serves route-registry scanning.
12
14
  */
13
15
  export function getServerModuleBuildCacheOutdir(appConfig) {
14
16
  return path.join(resolveInternalExecutionDir(appConfig), '.server-modules');
@@ -0,0 +1,51 @@
1
+ import type { EcoPagesAppConfig } from '../../config/internal-types.js';
2
+ export declare const SERVER_MODULES_MANIFEST_FILENAME = "server-modules.json";
3
+ /**
4
+ * Maps each server entry's root-relative source path to its emitted module,
5
+ * relative to {@link ProductionServerModulesManifest.outdir}.
6
+ */
7
+ export type ProductionServerModulesManifest = {
8
+ outdir: string;
9
+ buildId?: string;
10
+ modules: Record<string, string>;
11
+ };
12
+ export declare function getServerModulesManifestPath(outdir: string): string;
13
+ /**
14
+ * Writes the production server-modules map beside the emitted modules.
15
+ */
16
+ export declare function writeProductionServerModulesManifest(manifest: ProductionServerModulesManifest): void;
17
+ /**
18
+ * Source keys from `dist/server/server-modules.json`, before the app runtime exists.
19
+ */
20
+ export declare function listProductionServerModuleKeysFromDist(rootDir: string, distDir: string): string[] | undefined;
21
+ /**
22
+ * Returns the production server-modules map for this app, loading it from
23
+ * `dist/server/server-modules.json` when this process has not emitted one.
24
+ */
25
+ export declare function loadProductionServerModules(appConfig: EcoPagesAppConfig): ProductionServerModulesManifest | undefined;
26
+ /**
27
+ * Loads the production server-modules map or throws if `ecopages build` has not written it.
28
+ */
29
+ export declare function requireProductionServerModules(appConfig: EcoPagesAppConfig): ProductionServerModulesManifest;
30
+ /**
31
+ * Error when production cannot resolve a compiled server module.
32
+ *
33
+ * @remarks
34
+ * Names the source file and tells the operator to run `ecopages build`. It is
35
+ * never a compile fallback.
36
+ */
37
+ export declare function missingProductionServerModuleError(sourceKey: string): Error;
38
+ /**
39
+ * Finds the production map key for `filePath`.
40
+ *
41
+ * @remarks
42
+ * Prefers the path relative to `rootDir`. When `identity.file` still names the
43
+ * build machine (an absolute path outside the deploy root), the longest map key
44
+ * that is a suffix of that path wins, so a copied `dist/` can load scripts
45
+ * resolved from that identity.
46
+ */
47
+ export declare function matchProductionModuleKey(rootDir: string, filePath: string, modules: Record<string, string>): string | undefined;
48
+ /**
49
+ * True when `ecopages build` recorded a compiled module for `filePath`.
50
+ */
51
+ export declare function hasProductionServerModule(appConfig: EcoPagesAppConfig, filePath: string): boolean;
@@ -0,0 +1,129 @@
1
+ import path from 'node:path';
2
+ import { fileSystem } from '@ecopages/file-system';
3
+ import { requireAppRuntime } from '../../utils/app-runtime.js';
4
+ import { toRootRelativePath } from '../../utils/root-relative-path.js';
5
+ import { getServerBundleOutputPaths } from '../server-bundle-deploy-manifest.js';
6
+ import { SERVER_BUNDLE_DIR } from '../../utils/resolve-entry-file.js';
7
+ export const SERVER_MODULES_MANIFEST_FILENAME = 'server-modules.json';
8
+ export function getServerModulesManifestPath(outdir) {
9
+ return path.join(outdir, SERVER_MODULES_MANIFEST_FILENAME);
10
+ }
11
+ /**
12
+ * Writes the production server-modules map beside the emitted modules.
13
+ */
14
+ export function writeProductionServerModulesManifest(manifest) {
15
+ fileSystem.ensureDir(manifest.outdir);
16
+ const payload = {
17
+ ...(manifest.buildId ? { buildId: manifest.buildId } : {}),
18
+ modules: manifest.modules,
19
+ };
20
+ fileSystem.write(getServerModulesManifestPath(manifest.outdir), `${JSON.stringify(payload, null, '\t')}\n`);
21
+ }
22
+ /**
23
+ * Source keys from `dist/server/server-modules.json`, before the app runtime exists.
24
+ */
25
+ export function listProductionServerModuleKeysFromDist(rootDir, distDir) {
26
+ const payload = readProductionServerModulesFile(path.join(path.resolve(rootDir, distDir), SERVER_BUNDLE_DIR));
27
+ return payload ? Object.keys(payload.modules) : undefined;
28
+ }
29
+ function readProductionServerModulesFile(outdir) {
30
+ const manifestPath = getServerModulesManifestPath(outdir);
31
+ if (!fileSystem.exists(manifestPath)) {
32
+ return undefined;
33
+ }
34
+ try {
35
+ const payload = JSON.parse(fileSystem.readFileSync(manifestPath).toString());
36
+ if (!payload?.modules || typeof payload.modules !== 'object') {
37
+ return undefined;
38
+ }
39
+ return payload;
40
+ }
41
+ catch {
42
+ return undefined;
43
+ }
44
+ }
45
+ /**
46
+ * Returns the production server-modules map for this app, loading it from
47
+ * `dist/server/server-modules.json` when this process has not emitted one.
48
+ */
49
+ export function loadProductionServerModules(appConfig) {
50
+ const runtime = requireAppRuntime(appConfig);
51
+ if (runtime.productionServerModules) {
52
+ return runtime.productionServerModules;
53
+ }
54
+ const { serverOutdir } = getServerBundleOutputPaths(appConfig);
55
+ const payload = readProductionServerModulesFile(serverOutdir);
56
+ if (!payload) {
57
+ return undefined;
58
+ }
59
+ const manifest = {
60
+ outdir: serverOutdir,
61
+ buildId: payload.buildId,
62
+ modules: payload.modules,
63
+ };
64
+ runtime.productionServerModules = manifest;
65
+ return manifest;
66
+ }
67
+ /**
68
+ * Loads the production server-modules map or throws if `ecopages build` has not written it.
69
+ */
70
+ export function requireProductionServerModules(appConfig) {
71
+ const manifest = loadProductionServerModules(appConfig);
72
+ if (!manifest) {
73
+ throw new Error('No production server-modules map. Run `ecopages build`.');
74
+ }
75
+ return manifest;
76
+ }
77
+ /**
78
+ * Error when production cannot resolve a compiled server module.
79
+ *
80
+ * @remarks
81
+ * Names the source file and tells the operator to run `ecopages build`. It is
82
+ * never a compile fallback.
83
+ */
84
+ export function missingProductionServerModuleError(sourceKey) {
85
+ return new Error(`No compiled server module for ${sourceKey}. Run \`ecopages build\`.`);
86
+ }
87
+ function modulePathWithoutQuery(filePath) {
88
+ const query = filePath.indexOf('?');
89
+ return query === -1 ? filePath : filePath.slice(0, query);
90
+ }
91
+ /**
92
+ * Finds the production map key for `filePath`.
93
+ *
94
+ * @remarks
95
+ * Prefers the path relative to `rootDir`. When `identity.file` still names the
96
+ * build machine (an absolute path outside the deploy root), the longest map key
97
+ * that is a suffix of that path wins, so a copied `dist/` can load scripts
98
+ * resolved from that identity.
99
+ */
100
+ export function matchProductionModuleKey(rootDir, filePath, modules) {
101
+ const file = modulePathWithoutQuery(filePath);
102
+ const sourceKey = toRootRelativePath(rootDir, file);
103
+ if (sourceKey in modules) {
104
+ return sourceKey;
105
+ }
106
+ const posix = file.split(path.sep).join('/');
107
+ let match;
108
+ for (const key of Object.keys(modules)) {
109
+ if (posix === key || posix.endsWith(`/${key}`)) {
110
+ if (!match || key.length > match.length) {
111
+ match = key;
112
+ }
113
+ }
114
+ }
115
+ return match;
116
+ }
117
+ /**
118
+ * True when `ecopages build` recorded a compiled module for `filePath`.
119
+ */
120
+ export function hasProductionServerModule(appConfig, filePath) {
121
+ if (!appConfig.appRuntime) {
122
+ return false;
123
+ }
124
+ const manifest = loadProductionServerModules(appConfig);
125
+ if (!manifest) {
126
+ return false;
127
+ }
128
+ return matchProductionModuleKey(appConfig.rootDir, filePath, manifest.modules) !== undefined;
129
+ }