@ecopages/core 0.2.0-rc.0 → 0.2.0-rc.10

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 (184) hide show
  1. package/README.md +52 -33
  2. package/package.json +58 -4
  3. package/src/adapters/README.md +3 -1
  4. package/src/adapters/abstract/application-adapter.d.ts +28 -6
  5. package/src/adapters/abstract/application-adapter.js +58 -5
  6. package/src/adapters/abstract/segment-path-matcher.js +56 -38
  7. package/src/adapters/abstract/server-adapter.d.ts +14 -0
  8. package/src/adapters/abstract/server-adapter.js +10 -0
  9. package/src/adapters/bun/create-app.d.ts +10 -6
  10. package/src/adapters/bun/create-app.js +64 -37
  11. package/src/adapters/bun/server-adapter.d.ts +6 -2
  12. package/src/adapters/bun/server-adapter.js +14 -1
  13. package/src/adapters/create-app.d.ts +62 -7
  14. package/src/adapters/create-app.js +61 -5
  15. package/src/adapters/node/create-app.d.ts +5 -5
  16. package/src/adapters/node/create-app.js +18 -5
  17. package/src/adapters/node/runtime-host.js +9 -4
  18. package/src/adapters/node/server-adapter.d.ts +5 -1
  19. package/src/adapters/node/server-adapter.js +16 -1
  20. package/src/adapters/shared/http/define-api-handler.d.ts +7 -49
  21. package/src/adapters/shared/http/explicit-static-route-matcher.js +5 -2
  22. package/src/adapters/shared/http/fs-server-response-factory.d.ts +2 -8
  23. package/src/adapters/shared/http/fs-server-response-factory.js +5 -30
  24. package/src/adapters/shared/http/fs-server-response-matcher.d.ts +24 -24
  25. package/src/adapters/shared/http/fs-server-response-matcher.js +59 -83
  26. package/src/adapters/shared/runtime/application-adapter.d.ts +1 -0
  27. package/src/adapters/shared/runtime/application-adapter.js +12 -0
  28. package/src/adapters/shared/runtime/bind-runtime-server.d.ts +17 -0
  29. package/src/adapters/shared/runtime/bind-runtime-server.js +50 -0
  30. package/src/adapters/shared/runtime/port-manager-default-prompt.d.ts +6 -0
  31. package/src/adapters/shared/runtime/port-manager-default-prompt.js +41 -0
  32. package/src/adapters/shared/runtime/port-manager.d.ts +3 -3
  33. package/src/adapters/shared/runtime/port-manager.js +14 -45
  34. package/src/adapters/shared/runtime/server-adapter.d.ts +14 -5
  35. package/src/adapters/shared/runtime/server-adapter.js +22 -30
  36. package/src/adapters/shared/runtime/server-route-handler.js +11 -3
  37. package/src/adapters/shared/runtime/server-static-builder.d.ts +9 -3
  38. package/src/adapters/shared/runtime/server-static-builder.js +59 -37
  39. package/src/build/README.md +5 -1
  40. package/src/build/cache/server-entry-build-cache.d.ts +24 -1
  41. package/src/build/cache/server-entry-build-cache.js +125 -8
  42. package/src/build/contracts/content-virtual-modules.d.ts +3 -1
  43. package/src/build/contracts/content-virtual-modules.js +8 -0
  44. package/src/build/rolldown/rolldown-adapter-helpers.js +56 -31
  45. package/src/build/rolldown/rolldown-plugin-bridge.js +25 -21
  46. package/src/build/rolldown/runtime-build-output-normalizer.js +3 -29
  47. package/src/build/runtime/build-request-policy.js +1 -1
  48. package/src/build/server-bundle-publication.d.ts +14 -0
  49. package/src/build/server-bundle-publication.js +49 -0
  50. package/src/config/README.md +17 -2
  51. package/src/config/apply-user-config.d.ts +6 -0
  52. package/src/config/apply-user-config.js +67 -0
  53. package/src/config/config-builder.d.ts +10 -1
  54. package/src/config/config-builder.js +51 -10
  55. package/src/config/constants.d.ts +4 -0
  56. package/src/config/constants.js +4 -0
  57. package/src/config/define-config.d.ts +9 -0
  58. package/src/config/define-config.js +10 -0
  59. package/src/config/index.d.ts +5 -0
  60. package/src/config/index.js +4 -0
  61. package/src/config/is-finalized-app-config.d.ts +6 -0
  62. package/src/config/is-finalized-app-config.js +11 -0
  63. package/src/config/load-eco-config.d.ts +21 -0
  64. package/src/config/load-eco-config.js +100 -0
  65. package/src/config/resolve-eco-config-path.d.ts +23 -0
  66. package/src/config/resolve-eco-config-path.js +47 -0
  67. package/src/config/server-config-bundle.d.ts +25 -0
  68. package/src/config/server-config-bundle.js +148 -0
  69. package/src/config/user-config-types.d.ts +164 -0
  70. package/src/config/user-config-types.js +1 -0
  71. package/src/css-imports.d.ts +4 -0
  72. package/src/declarations.d.ts +11 -5
  73. package/src/dev/README.md +6 -0
  74. package/src/dev/development-restart-watch-paths.d.ts +13 -0
  75. package/src/dev/development-restart-watch-paths.js +37 -0
  76. package/src/dev/development-restart.d.ts +13 -0
  77. package/src/dev/development-restart.js +21 -0
  78. package/src/dev/transform-server/dev-transform-server.d.ts +6 -0
  79. package/src/dev/transform-server/dev-transform-server.js +16 -5
  80. package/src/eco/README.md +73 -5
  81. package/src/eco/component-identity.d.ts +3 -2
  82. package/src/eco/component-identity.js +5 -2
  83. package/src/eco/discovered-dependencies.d.ts +45 -0
  84. package/src/eco/discovered-dependencies.js +87 -0
  85. package/src/eco/eco.js +6 -0
  86. package/src/eco/eco.types.d.ts +18 -2
  87. package/src/eco/lazy-injector-map.js +57 -41
  88. package/src/eco/page-dependency-contributions.d.ts +19 -0
  89. package/src/eco/page-dependency-contributions.js +52 -0
  90. package/src/errors/http-error-page-contract.d.ts +77 -0
  91. package/src/errors/http-error-page-contract.js +106 -0
  92. package/src/hmr/strategies/js-hmr-strategy.d.ts +2 -0
  93. package/src/hmr/strategies/js-hmr-strategy.js +24 -16
  94. package/src/index.browser.d.ts +3 -0
  95. package/src/index.browser.js +3 -0
  96. package/src/index.d.ts +4 -1
  97. package/src/index.js +4 -1
  98. package/src/islands/README.md +5 -1
  99. package/src/plugins/README.md +10 -2
  100. package/src/plugins/component-import-discovery.d.ts +19 -0
  101. package/src/plugins/component-import-discovery.js +241 -0
  102. package/src/plugins/eco-component-meta-plugin.d.ts +3 -1
  103. package/src/plugins/eco-component-meta-plugin.js +118 -16
  104. package/src/plugins/integration-plugin.d.ts +4 -9
  105. package/src/plugins/processor.d.ts +23 -2
  106. package/src/plugins/processor.js +25 -0
  107. package/src/plugins/tsconfig-import-resolver.d.ts +3 -1
  108. package/src/plugins/tsconfig-import-resolver.js +4 -2
  109. package/src/route-renderer/GRAPH.md +1 -1
  110. package/src/route-renderer/README.md +16 -3
  111. package/src/{adapters/shared/http/explicit-static-render-preparation.d.ts → route-renderer/explicit-view-render-preparation.d.ts} +3 -3
  112. package/src/route-renderer/orchestration/custom-element-scripts/README.md +9 -0
  113. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.d.ts +16 -0
  114. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.js +34 -0
  115. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.d.ts +80 -0
  116. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.js +190 -0
  117. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.d.ts +17 -0
  118. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.js +41 -0
  119. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.d.ts +10 -0
  120. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.js +46 -0
  121. package/src/route-renderer/orchestration/document-shell/document-shell-render.service.d.ts +7 -2
  122. package/src/route-renderer/orchestration/document-shell/document-shell-render.service.js +2 -0
  123. package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.d.ts +13 -1
  124. package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.js +15 -0
  125. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +11 -2
  126. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +10 -2
  127. package/src/route-renderer/orchestration/integration-renderer.d.ts +11 -5
  128. package/src/route-renderer/orchestration/integration-renderer.js +42 -29
  129. package/src/route-renderer/orchestration/ownership-graph/component-graph-collectors.d.ts +2 -5
  130. package/src/route-renderer/orchestration/ownership-graph/component-graph-collectors.js +2 -68
  131. package/src/route-renderer/orchestration/ownership-graph/component-graph.js +55 -39
  132. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.d.ts +6 -0
  133. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.js +59 -36
  134. package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.js +8 -10
  135. package/src/route-renderer/orchestration/string-markup-renderer.d.ts +1 -1
  136. package/src/route-renderer/orchestration/string-markup-renderer.js +10 -8
  137. package/src/route-renderer/page-loading/component-dependency-collection.d.ts +6 -2
  138. package/src/route-renderer/page-loading/component-dependency-collection.js +113 -68
  139. package/src/route-renderer/page-loading/ecopages-virtual-imports.js +41 -28
  140. package/src/route-renderer/page-loading/file-scoped-dependency-components.d.ts +10 -3
  141. package/src/route-renderer/page-loading/file-scoped-dependency-components.js +62 -25
  142. package/src/route-renderer/page-loading/page-dependency-bundling.js +14 -8
  143. package/src/route-renderer/page-loading/resolved-page-dependencies.d.ts +1 -2
  144. package/src/route-renderer/page-loading/resolved-page-dependencies.js +8 -7
  145. package/src/route-renderer/route-renderer.d.ts +9 -0
  146. package/src/route-renderer/route-renderer.js +18 -0
  147. package/src/router/README.md +7 -1
  148. package/src/router/server/route-registry.d.ts +13 -0
  149. package/src/router/server/route-registry.js +46 -32
  150. package/src/services/README.md +5 -0
  151. package/src/services/assets/asset-processing-service/browser-runtime-entry-resolution.d.ts +36 -9
  152. package/src/services/assets/asset-processing-service/browser-runtime-entry-resolution.js +122 -37
  153. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.d.ts +2 -0
  154. package/src/services/assets/asset-processing-service/browser-runtime-entry.factory.js +13 -2
  155. package/src/services/assets/asset-processing-service/page-package.js +42 -39
  156. package/src/services/error-pages/README.md +9 -0
  157. package/src/services/error-pages/default-error-pages.d.ts +44 -0
  158. package/src/services/error-pages/default-error-pages.js +207 -0
  159. package/src/services/error-pages/error-page-renderer.d.ts +58 -0
  160. package/src/services/error-pages/error-page-renderer.js +146 -0
  161. package/src/services/error-pages/semantic-error-page-exporter.d.ts +23 -0
  162. package/src/services/error-pages/semantic-error-page-exporter.js +35 -0
  163. package/src/services/invalidation/development-invalidation.service.d.ts +7 -1
  164. package/src/services/invalidation/development-invalidation.service.js +35 -3
  165. package/src/services/module-loading/README.md +16 -13
  166. package/src/services/module-loading/app-server-module-transpiler.service.js +2 -0
  167. package/src/services/module-loading/collection-server-module-build.service.d.ts +1 -0
  168. package/src/services/module-loading/collection-server-module-build.service.js +5 -2
  169. package/src/services/module-loading/page-module-import.service.d.ts +8 -0
  170. package/src/services/module-loading/page-module-import.service.js +62 -28
  171. package/src/services/module-loading/route-module-dependency-hasher.js +13 -1
  172. package/src/services/module-loading/view-module-loader.d.ts +10 -0
  173. package/src/services/module-loading/view-module-loader.js +46 -0
  174. package/src/services/validation/schema-validation-service.d.ts +1 -0
  175. package/src/services/validation/schema-validation-service.js +23 -36
  176. package/src/static-site-generator/README.md +6 -1
  177. package/src/static-site-generator/static-site-generator.d.ts +7 -2
  178. package/src/static-site-generator/static-site-generator.js +90 -49
  179. package/src/types/internal-types.d.ts +6 -1
  180. package/src/types/public-types.d.ts +34 -24
  181. package/src/utils/parse-cli-args.js +29 -22
  182. package/src/watchers/project-watcher.d.ts +14 -1
  183. package/src/watchers/project-watcher.js +85 -34
  184. /package/src/{adapters/shared/http/explicit-static-render-preparation.js → route-renderer/explicit-view-render-preparation.js} +0 -0
@@ -21,6 +21,24 @@ export class RouteRendererFactory {
21
21
  this.rendererModules = rendererModules;
22
22
  this.runtimeOrigin = runtimeOrigin;
23
23
  }
24
+ /**
25
+ * Rebinds renderer creation to the actual listening origin.
26
+ *
27
+ * @remarks
28
+ * A development server may select a fallback port after this factory is
29
+ * constructed. Cached renderers retain their original page-module loader, so
30
+ * they must be discarded when the origin changes.
31
+ */
32
+ setRuntimeOrigin(runtimeOrigin) {
33
+ if (this.runtimeOrigin === runtimeOrigin) {
34
+ return;
35
+ }
36
+ this.runtimeOrigin = runtimeOrigin;
37
+ this.rendererCache.clear();
38
+ for (const integration of this.appConfig.integrations) {
39
+ integration.setRuntimeOrigin?.(runtimeOrigin);
40
+ }
41
+ }
24
42
  /**
25
43
  * Returns a route renderer for the supplied route file.
26
44
  */
@@ -56,7 +56,11 @@ Match priority:
56
56
 
57
57
  1. `exact` — the pathname must equal the route pathname exactly.
58
58
  2. `dynamic` — the clean (bracket-stripped) prefix must appear in the pathname, and the segment counts must match.
59
- 3. `catch-all` — the clean prefix must appear in the pathname.
59
+ 3. `catch-all` — the prefix segments must match the pathname. When multiple
60
+ catch-all routes match, shared prefix segments are compared from left to right
61
+ so an earlier static segment wins over a dynamic one; when those patterns tie,
62
+ the route with the longer prefix wins, then pathname order provides a
63
+ deterministic tie-breaker.
60
64
 
61
65
  ## `client/`
62
66
 
@@ -110,6 +114,8 @@ import {
110
114
 
111
115
  This is separate from coordinator `subscribe()` events, which track runtime ownership rather than page lifecycle.
112
116
 
117
+ Scripts listening to client navigation events (`eco:after-swap`, `eco:page-load`) should be registered as client scripts (e.g., using `scripts: ['./component.script.ts']`) or guarded by `isServer` from `@ecopages/radiant/is-server` when used in Radiant projects where `document` is defined during SSR.
118
+
113
119
  ### Link Intent (`link-intent.ts`)
114
120
 
115
121
  Shared helpers for locating anchors and recovering stale navigation intent.
@@ -62,6 +62,19 @@ export declare class RouteRegistry {
62
62
  reload(): Promise<void>;
63
63
  onReload(listener: () => void): () => void;
64
64
  matchRequest(requestUrl: string): RouteMatch | null;
65
+ /**
66
+ * Orders overlapping catch-all routes by the specificity of their prefix.
67
+ *
68
+ * @remarks
69
+ * Prefix segments are compared from left to right because an earlier static
70
+ * segment constrains a request more strongly than a later one. A longer
71
+ * prefix wins only after the shared segment pattern ties. The pathname tie
72
+ * breaker keeps equal patterns deterministic without depending on discovery
73
+ * order.
74
+ */
75
+ private compareCatchAllSpecificity;
76
+ private getCatchAllPrefix;
77
+ private isDynamicRouteSegment;
65
78
  listStaticPathExpansions(input: {
66
79
  runtimeOrigin: string;
67
80
  }): Promise<readonly StaticPathExpansion[]>;
@@ -49,49 +49,60 @@ export class RouteRegistry {
49
49
  const requestedPathname = normalizePathname(url.pathname);
50
50
  const query = this.getSearchParams(url);
51
51
  for (const route of this.templateRouteList) {
52
- if (route.kind !== 'exact') {
52
+ if (route.kind === 'exact') {
53
+ if (requestedPathname === route.pathname || requestedPathname === `${route.pathname}/`) {
54
+ return {
55
+ requestedPathname,
56
+ templateRoute: route,
57
+ params: {},
58
+ query,
59
+ };
60
+ }
53
61
  continue;
54
62
  }
55
- if (requestedPathname === route.pathname || requestedPathname === `${route.pathname}/`) {
63
+ const params = this.tryExtractParams(route, requestedPathname);
64
+ if (params) {
56
65
  return {
57
66
  requestedPathname,
58
67
  templateRoute: route,
59
- params: {},
68
+ params,
60
69
  query,
61
70
  };
62
71
  }
63
72
  }
64
- for (const route of this.templateRouteList) {
65
- if (route.kind !== 'dynamic') {
66
- continue;
67
- }
68
- const params = this.tryExtractParams(route, requestedPathname);
69
- if (!params) {
70
- continue;
71
- }
72
- return {
73
- requestedPathname,
74
- templateRoute: route,
75
- params,
76
- query,
77
- };
78
- }
79
- for (const route of this.templateRouteList) {
80
- if (route.kind !== 'catch-all') {
81
- continue;
82
- }
83
- const params = this.tryExtractParams(route, requestedPathname);
84
- if (!params) {
85
- continue;
73
+ return null;
74
+ }
75
+ /**
76
+ * Orders overlapping catch-all routes by the specificity of their prefix.
77
+ *
78
+ * @remarks
79
+ * Prefix segments are compared from left to right because an earlier static
80
+ * segment constrains a request more strongly than a later one. A longer
81
+ * prefix wins only after the shared segment pattern ties. The pathname tie
82
+ * breaker keeps equal patterns deterministic without depending on discovery
83
+ * order.
84
+ */
85
+ compareCatchAllSpecificity(left, right) {
86
+ const leftPrefix = this.getCatchAllPrefix(left.pathname);
87
+ const rightPrefix = this.getCatchAllPrefix(right.pathname);
88
+ const sharedLength = Math.min(leftPrefix.length, rightPrefix.length);
89
+ for (let index = 0; index < sharedLength; index += 1) {
90
+ const leftIsDynamic = this.isDynamicRouteSegment(leftPrefix[index]);
91
+ const rightIsDynamic = this.isDynamicRouteSegment(rightPrefix[index]);
92
+ if (leftIsDynamic !== rightIsDynamic) {
93
+ return leftIsDynamic ? 1 : -1;
86
94
  }
87
- return {
88
- requestedPathname,
89
- templateRoute: route,
90
- params,
91
- query,
92
- };
93
95
  }
94
- return null;
96
+ const prefixLengthDifference = rightPrefix.length - leftPrefix.length;
97
+ return prefixLengthDifference !== 0 ? prefixLengthDifference : left.pathname.localeCompare(right.pathname);
98
+ }
99
+ getCatchAllPrefix(pathname) {
100
+ const segments = pathname.split('/').filter(Boolean);
101
+ const catchAllIndex = segments.findIndex((segment) => segment.startsWith('[...') && segment.endsWith(']'));
102
+ return catchAllIndex === -1 ? segments : segments.slice(0, catchAllIndex);
103
+ }
104
+ isDynamicRouteSegment(segment) {
105
+ return segment.startsWith('[') && segment.endsWith(']');
95
106
  }
96
107
  async listStaticPathExpansions(input) {
97
108
  const expansions = [];
@@ -168,6 +179,9 @@ export class RouteRegistry {
168
179
  if (priorityDifference !== 0) {
169
180
  return priorityDifference;
170
181
  }
182
+ if (left.kind === 'catch-all' && right.kind === 'catch-all') {
183
+ return this.compareCatchAllSpecificity(left, right);
184
+ }
171
185
  if (left.pathname === '/') {
172
186
  return -1;
173
187
  }
@@ -23,11 +23,16 @@ Typical responsibilities include:
23
23
  - `runtime-state/`: app-owned invalidation state and dependency graphs
24
24
  - `runtime-manifest/`: node runtime manifest derivation and persistence
25
25
  - `html/`: final HTML dependency injection and rewriter selection
26
+ - `error-pages/`: shared semantic 404/500 source precedence, rendering, built-in documents, and static export artifacts
26
27
 
27
28
  The asset-processing service caches emitted file assets by source identity in development as well as production.
28
29
  When a source file changes, its source hash or explicit invalidation removes the cached asset before the next render,
29
30
  so shared layout styles do not need to be rebuilt for every navigated page while HMR remains fresh.
30
31
 
32
+ Browser runtime module assets resolve bare package roots through their ESM import target. For legacy packages without an `exports` map, they prefer `package.json#module` over CJS `main`; CJS resolution is only a compatibility fallback. Generated entries use `export *` for ESM files and explicit named re-exports for CJS files (so bindings such as React `jsx` exist on the vendor). A default binding is added only when the selected entry exposes one. Runtime vendors are package-root contracts: subpath imports need their own vendor declaration or remain in the consuming bundle.
33
+
34
+ Package entry lookup is rooted at the application directory, so a framework-local dependency with the same name cannot shadow the application's installed package. ESM export conditions are evaluated in the same order as the generated vendor import (`node`, `import`, then `default`).
35
+
31
36
  ## Design Rule
32
37
 
33
38
  If a concern affects more than one integration or more than one runtime adapter, it usually belongs here instead of in a package-specific implementation.
@@ -2,9 +2,27 @@ import type { createRequire } from 'node:module';
2
2
  export type BrowserRuntimeDefaultExportPolicy = 'emit-default' | 'skip-default';
3
3
  type RequireFromRoot = ReturnType<typeof createRequire>;
4
4
  /**
5
- * Resolves a package specifier to its ESM entry file path from the app root.
5
+ * Resolves a package specifier through Node's ESM import conditions from the app root.
6
+ *
7
+ * @remarks
8
+ * The resolver is rooted at the application directory so package lookup cannot
9
+ * accidentally use a dependency with the same name from the framework's own
10
+ * installation. The condition order matches the generated vendor import: Node
11
+ * compatibility first, then ESM import, then the package default.
6
12
  */
7
13
  export declare function resolvePackageEsmEntryPath(specifier: string, rootDir: string): string | undefined;
14
+ /**
15
+ * Resolves a browser runtime specifier to the absolute file that the generated
16
+ * vendor entry will import, when that import is a path.
17
+ *
18
+ * @remarks
19
+ * `node:` specifiers have no file path. `file:` URLs are converted to paths.
20
+ */
21
+ export declare function resolveBrowserRuntimeEntryPath(options: {
22
+ specifier: string;
23
+ requireFromRoot: RequireFromRoot;
24
+ rootDir: string;
25
+ }): string | undefined;
8
26
  /**
9
27
  * Builds a relative import path from a generated runtime entry file to a resolved module path.
10
28
  */
@@ -13,10 +31,9 @@ export declare function toRelativeEntryImport(entryDir: string, resolvedPath: st
13
31
  * Resolves a browser runtime entry import to an ESM file path when possible.
14
32
  *
15
33
  * @remarks
16
- * `createRequire().resolve()` follows the `require` export condition and can
17
- * land on `.cjs` entrypoints. Browser vendor bundles then emit runtime
18
- * `require()` calls for React externals. Prefer Node's ESM resolver first, then
19
- * a `.cjs` → `.js` sibling fallback.
34
+ * Browser vendor entries resolve through ESM import conditions first. A package
35
+ * root without `exports` may instead select its legacy `module` field; CJS
36
+ * resolution is the final compatibility fallback.
20
37
  */
21
38
  export declare function resolveBrowserRuntimeEntryImport(options: {
22
39
  specifier: string;
@@ -42,11 +59,21 @@ export declare function inferBrowserRuntimeDefaultExportPolicy(options: {
42
59
  rootDir: string;
43
60
  }): BrowserRuntimeDefaultExportPolicy;
44
61
  /**
45
- * Reads the named runtime exports that should be re-exported from a generated runtime entry module.
62
+ * Returns true when the resolved vendor file is an ESM module that Rolldown can
63
+ * re-export with `export *`.
64
+ *
65
+ * @remarks
66
+ * React's published "import" files are still CJS wrappers. `export *` from those
67
+ * files does not emit named ESM bindings such as `jsx`.
68
+ */
69
+ export declare function browserRuntimeEntryHasEsmExports(entryPath: string | undefined): boolean;
70
+ /**
71
+ * Reads enumerable CJS exports from the same file the generated entry imports.
46
72
  *
47
73
  * @remarks
48
- * Default exports are handled separately because generated runtime entry files need to emit a
49
- * synthetic default binding only when the caller explicitly asks for it.
74
+ * Used only when {@link browserRuntimeEntryHasEsmExports} is false. ESM entries
75
+ * must not harvest names from `require()`, which copies class statics that are
76
+ * not ESM named exports.
50
77
  */
51
- export declare function listBrowserRuntimeModuleExportNames(specifier: string, requireFromRoot: RequireFromRoot): string[];
78
+ export declare function listBrowserRuntimeCjsExportNames(moduleId: string, requireFromRoot: RequireFromRoot): string[];
52
79
  export {};
@@ -1,13 +1,98 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { pathToFileURL, fileURLToPath } from 'node:url';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { ResolverFactory } from 'oxc-resolver';
4
5
  import { isBarePackageImportSpecifier } from '../../../plugins/tsconfig-import-resolver.js';
6
+ import { toPackageRootSpecifier } from '../../../plugins/package-specifier.js';
7
+ const esmEntryResolvers = new Map();
8
+ function getEsmEntryResolver(rootDir) {
9
+ const normalizedRoot = path.resolve(rootDir);
10
+ const cached = esmEntryResolvers.get(normalizedRoot);
11
+ if (cached) {
12
+ return cached;
13
+ }
14
+ const resolver = new ResolverFactory({ conditionNames: ['node', 'import', 'default'] });
15
+ esmEntryResolvers.set(normalizedRoot, resolver);
16
+ return resolver;
17
+ }
5
18
  /**
6
- * Resolves a package specifier to its ESM entry file path from the app root.
19
+ * Resolves a package specifier through Node's ESM import conditions from the app root.
20
+ *
21
+ * @remarks
22
+ * The resolver is rooted at the application directory so package lookup cannot
23
+ * accidentally use a dependency with the same name from the framework's own
24
+ * installation. The condition order matches the generated vendor import: Node
25
+ * compatibility first, then ESM import, then the package default.
7
26
  */
8
27
  export function resolvePackageEsmEntryPath(specifier, rootDir) {
9
28
  try {
10
- return fileURLToPath(import.meta.resolve(specifier, pathToFileURL(path.join(rootDir, 'package.json')).href));
29
+ const result = getEsmEntryResolver(rootDir).sync(path.resolve(rootDir), specifier);
30
+ return result.path;
31
+ }
32
+ catch {
33
+ return undefined;
34
+ }
35
+ }
36
+ function resolveLegacyPackageModuleEntry(options) {
37
+ if (!isBarePackageImportSpecifier(options.specifier, options.rootDir)) {
38
+ return undefined;
39
+ }
40
+ const packageRoot = toPackageRootSpecifier(options.specifier);
41
+ if (options.specifier !== packageRoot) {
42
+ return undefined;
43
+ }
44
+ try {
45
+ const packageJsonPath = options.requireFromRoot.resolve(`${packageRoot}/package.json`);
46
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
47
+ if (packageJson.exports !== undefined || typeof packageJson.module !== 'string') {
48
+ return undefined;
49
+ }
50
+ const modulePath = path.resolve(path.dirname(packageJsonPath), packageJson.module);
51
+ return fs.existsSync(modulePath) ? modulePath : undefined;
52
+ }
53
+ catch {
54
+ return undefined;
55
+ }
56
+ }
57
+ const ESM_DEFAULT_EXPORT = /\bexport\s+default\b/;
58
+ const ESM_NAMED_EXPORT = /\bexport\s+(?:[\w*{]|const|let|var|function|class)/;
59
+ const EXPORTABLE_BINDING_NAME = /^[$A-Z_a-z][$\w]*$/;
60
+ /**
61
+ * Resolves a browser runtime specifier to the absolute file that the generated
62
+ * vendor entry will import, when that import is a path.
63
+ *
64
+ * @remarks
65
+ * `node:` specifiers have no file path. `file:` URLs are converted to paths.
66
+ */
67
+ export function resolveBrowserRuntimeEntryPath(options) {
68
+ const { specifier, requireFromRoot, rootDir } = options;
69
+ if (specifier.startsWith('node:')) {
70
+ return undefined;
71
+ }
72
+ if (specifier.startsWith('file:')) {
73
+ return fileURLToPath(specifier);
74
+ }
75
+ if (specifier.startsWith('.')) {
76
+ return requireFromRoot.resolve(specifier);
77
+ }
78
+ const legacyModulePath = resolveLegacyPackageModuleEntry({ specifier, requireFromRoot, rootDir });
79
+ if (legacyModulePath) {
80
+ return legacyModulePath;
81
+ }
82
+ const esmResolvedPath = isBarePackageImportSpecifier(specifier, rootDir)
83
+ ? resolvePackageEsmEntryPath(specifier, rootDir)
84
+ : undefined;
85
+ if (esmResolvedPath) {
86
+ return esmResolvedPath;
87
+ }
88
+ try {
89
+ const resolvedPath = requireFromRoot.resolve(specifier);
90
+ const esmSibling = resolvedPath.endsWith('.cjs')
91
+ ? `${resolvedPath.slice(0, -4)}.js`
92
+ : resolvedPath.endsWith('.cts')
93
+ ? `${resolvedPath.slice(0, -4)}.ts`
94
+ : undefined;
95
+ return esmSibling && fs.existsSync(esmSibling) ? esmSibling : resolvedPath;
11
96
  }
12
97
  catch {
13
98
  return undefined;
@@ -27,36 +112,20 @@ export function toRelativeEntryImport(entryDir, resolvedPath) {
27
112
  * Resolves a browser runtime entry import to an ESM file path when possible.
28
113
  *
29
114
  * @remarks
30
- * `createRequire().resolve()` follows the `require` export condition and can
31
- * land on `.cjs` entrypoints. Browser vendor bundles then emit runtime
32
- * `require()` calls for React externals. Prefer Node's ESM resolver first, then
33
- * a `.cjs` → `.js` sibling fallback.
115
+ * Browser vendor entries resolve through ESM import conditions first. A package
116
+ * root without `exports` may instead select its legacy `module` field; CJS
117
+ * resolution is the final compatibility fallback.
34
118
  */
35
119
  export function resolveBrowserRuntimeEntryImport(options) {
36
120
  const { specifier, requireFromRoot, entryDir, rootDir } = options;
37
121
  if (specifier.startsWith('node:') || specifier.startsWith('file:')) {
38
122
  return specifier;
39
123
  }
40
- if (specifier.startsWith('.')) {
41
- const resolvedPath = requireFromRoot.resolve(specifier);
42
- return toRelativeEntryImport(entryDir, resolvedPath);
43
- }
44
- if (isBarePackageImportSpecifier(specifier, rootDir)) {
45
- const esmResolvedPath = resolvePackageEsmEntryPath(specifier, rootDir);
46
- if (esmResolvedPath) {
47
- return toRelativeEntryImport(entryDir, esmResolvedPath);
48
- }
49
- }
50
- const resolvedPath = requireFromRoot.resolve(specifier);
51
- const esmSibling = resolvedPath.endsWith('.cjs')
52
- ? `${resolvedPath.slice(0, -4)}.js`
53
- : resolvedPath.endsWith('.cts')
54
- ? `${resolvedPath.slice(0, -4)}.ts`
55
- : undefined;
56
- if (esmSibling && fs.existsSync(esmSibling)) {
57
- return toRelativeEntryImport(entryDir, esmSibling);
124
+ const entryPath = resolveBrowserRuntimeEntryPath({ specifier, requireFromRoot, rootDir });
125
+ if (!entryPath) {
126
+ throw new Error(`Unable to resolve browser runtime entry for "${specifier}" from "${rootDir}"`);
58
127
  }
59
- return toRelativeEntryImport(entryDir, resolvedPath);
128
+ return toRelativeEntryImport(entryDir, entryPath);
60
129
  }
61
130
  /**
62
131
  * Decides whether a generated runtime entry should re-export a default binding.
@@ -72,13 +141,13 @@ export function resolveBrowserRuntimeEntryImport(options) {
72
141
  */
73
142
  export function inferBrowserRuntimeDefaultExportPolicy(options) {
74
143
  const { specifier, requireFromRoot, rootDir } = options;
75
- const esmPath = resolvePackageEsmEntryPath(specifier, rootDir);
76
- if (esmPath && fs.existsSync(esmPath)) {
77
- const source = fs.readFileSync(esmPath, 'utf8');
78
- if (/\bexport\s+default\b/.test(source)) {
144
+ const entryPath = resolveBrowserRuntimeEntryPath({ specifier, requireFromRoot, rootDir });
145
+ if (entryPath && fs.existsSync(entryPath)) {
146
+ const source = fs.readFileSync(entryPath, 'utf8');
147
+ if (ESM_DEFAULT_EXPORT.test(source)) {
79
148
  return 'emit-default';
80
149
  }
81
- if (/\bexport\s+(?:[\w*{]|const|let|var|function|class)/.test(source)) {
150
+ if (ESM_NAMED_EXPORT.test(source)) {
82
151
  return 'skip-default';
83
152
  }
84
153
  }
@@ -94,22 +163,38 @@ export function inferBrowserRuntimeDefaultExportPolicy(options) {
94
163
  }
95
164
  }
96
165
  /**
97
- * Reads the named runtime exports that should be re-exported from a generated runtime entry module.
166
+ * Returns true when the resolved vendor file is an ESM module that Rolldown can
167
+ * re-export with `export *`.
168
+ *
169
+ * @remarks
170
+ * React's published "import" files are still CJS wrappers. `export *` from those
171
+ * files does not emit named ESM bindings such as `jsx`.
172
+ */
173
+ export function browserRuntimeEntryHasEsmExports(entryPath) {
174
+ if (!entryPath || !fs.existsSync(entryPath)) {
175
+ return true;
176
+ }
177
+ const source = fs.readFileSync(entryPath, 'utf8');
178
+ return ESM_DEFAULT_EXPORT.test(source) || ESM_NAMED_EXPORT.test(source);
179
+ }
180
+ /**
181
+ * Reads enumerable CJS exports from the same file the generated entry imports.
98
182
  *
99
183
  * @remarks
100
- * Default exports are handled separately because generated runtime entry files need to emit a
101
- * synthetic default binding only when the caller explicitly asks for it.
184
+ * Used only when {@link browserRuntimeEntryHasEsmExports} is false. ESM entries
185
+ * must not harvest names from `require()`, which copies class statics that are
186
+ * not ESM named exports.
102
187
  */
103
- export function listBrowserRuntimeModuleExportNames(specifier, requireFromRoot) {
188
+ export function listBrowserRuntimeCjsExportNames(moduleId, requireFromRoot) {
104
189
  let moduleExports;
105
190
  try {
106
- moduleExports = requireFromRoot(specifier);
191
+ moduleExports = requireFromRoot(moduleId);
107
192
  }
108
193
  catch {
109
194
  return [];
110
195
  }
111
196
  return Object.keys(moduleExports)
112
197
  .filter((name) => name !== '__esModule' && name !== 'default')
113
- .filter((name) => /^[$A-Z_a-z][$\w]*$/.test(name))
198
+ .filter((name) => EXPORTABLE_BINDING_NAME.test(name))
114
199
  .sort();
115
200
  }
@@ -12,6 +12,8 @@ export type BrowserRuntimeEntryModuleConfig = {
12
12
  * logic themselves. The generated file lives under the app work directory so
13
13
  * repeated runs can reuse the same location without placing sources inside
14
14
  * `node_modules`, which can cause bundlers to externalize bare imports.
15
+ * ESM entries use `export *`. CJS entries emit explicit named re-exports from
16
+ * that same file so Rolldown can synthesize ESM bindings such as `jsx`.
15
17
  */
16
18
  export declare function createBrowserRuntimeEntryModule(options: {
17
19
  modules: BrowserRuntimeEntryModuleConfig[];
@@ -2,7 +2,7 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
  import { DEFAULT_ECOPAGES_WORK_DIR } from '../../../config/constants.js';
5
- import { inferBrowserRuntimeDefaultExportPolicy, listBrowserRuntimeModuleExportNames, resolveBrowserRuntimeEntryImport, } from './browser-runtime-entry-resolution.js';
5
+ import { browserRuntimeEntryHasEsmExports, inferBrowserRuntimeDefaultExportPolicy, listBrowserRuntimeCjsExportNames, resolveBrowserRuntimeEntryImport, resolveBrowserRuntimeEntryPath, } from './browser-runtime-entry-resolution.js';
6
6
  /**
7
7
  * Creates a generated ESM entry module that re-exports runtime modules through
8
8
  * one stable file.
@@ -13,6 +13,8 @@ import { inferBrowserRuntimeDefaultExportPolicy, listBrowserRuntimeModuleExportN
13
13
  * logic themselves. The generated file lives under the app work directory so
14
14
  * repeated runs can reuse the same location without placing sources inside
15
15
  * `node_modules`, which can cause bundlers to externalize bare imports.
16
+ * ESM entries use `export *`. CJS entries emit explicit named re-exports from
17
+ * that same file so Rolldown can synthesize ESM bindings such as `jsx`.
16
18
  */
17
19
  export function createBrowserRuntimeEntryModule(options) {
18
20
  if (options.modules.some((module) => !module.specifier.startsWith('node:')) && !options.rootDir) {
@@ -34,13 +36,22 @@ export function createBrowserRuntimeEntryModule(options) {
34
36
  entryDir,
35
37
  rootDir,
36
38
  });
39
+ const entryPath = resolveBrowserRuntimeEntryPath({
40
+ specifier: module.specifier,
41
+ requireFromRoot,
42
+ rootDir,
43
+ });
37
44
  if (module.defaultExport &&
38
45
  inferBrowserRuntimeDefaultExportPolicy({ specifier: module.specifier, requireFromRoot, rootDir }) ===
39
46
  'emit-default') {
40
47
  statements.push(`import __ecopages_default_export__ from '${importSpecifier}';`);
41
48
  statements.push('export default __ecopages_default_export__;');
42
49
  }
43
- const exportNames = listBrowserRuntimeModuleExportNames(module.specifier, requireFromRoot).filter((name) => !seenExports.has(name));
50
+ if (browserRuntimeEntryHasEsmExports(entryPath)) {
51
+ statements.push(`export * from '${importSpecifier}';`);
52
+ continue;
53
+ }
54
+ const exportNames = listBrowserRuntimeCjsExportNames(entryPath ?? module.specifier, requireFromRoot).filter((name) => !seenExports.has(name));
44
55
  if (exportNames.length > 0) {
45
56
  statements.push(`export { ${exportNames.join(', ')} } from '${importSpecifier}';`);
46
57
  for (const exportName of exportNames) {
@@ -10,58 +10,61 @@ function getSuppressedSourceFilepaths(assets) {
10
10
  }
11
11
  return suppressed;
12
12
  }
13
+ function classifyProcessedAsset(asset, buckets) {
14
+ if (asset.inline) {
15
+ buckets.inlineAssets.push(asset);
16
+ return;
17
+ }
18
+ if (asset.packageRole === 'dynamic-chunk') {
19
+ buckets.dynamicChunks.push(asset);
20
+ return;
21
+ }
22
+ if (!buckets.pageScript && asset.packageRole === 'page-script') {
23
+ buckets.pageScript = asset;
24
+ return;
25
+ }
26
+ if (!buckets.pageStylesheet && asset.packageRole === 'page-style') {
27
+ buckets.pageStylesheet = asset;
28
+ return;
29
+ }
30
+ if (asset.packageRole === 'keep-separate' || asset.packageRole === 'runtime') {
31
+ buckets.separateAssets.push(asset);
32
+ return;
33
+ }
34
+ if (!buckets.pageScript && asset.kind === 'script' && !asset.excludeFromHtml) {
35
+ buckets.pageScript = asset;
36
+ return;
37
+ }
38
+ if (!buckets.pageStylesheet && asset.kind === 'stylesheet') {
39
+ buckets.pageStylesheet = asset;
40
+ return;
41
+ }
42
+ buckets.separateAssets.push(asset);
43
+ }
13
44
  export function createPagePackage(assets, options = {}) {
14
45
  const allAssets = [
15
46
  ...assets,
16
47
  ...(options.pageBrowserGraph?.entryAssets ?? []),
17
48
  ...(options.pageBrowserGraph?.chunkAssets ?? []),
18
49
  ];
19
- const inlineAssets = [];
20
- const separateAssets = [];
21
- const dynamicChunks = [];
22
- let pageScript;
23
- let pageStylesheet;
50
+ const buckets = {
51
+ inlineAssets: [],
52
+ separateAssets: [],
53
+ dynamicChunks: [],
54
+ };
24
55
  const suppressedSourceFilepaths = getSuppressedSourceFilepaths(allAssets);
25
56
  for (const asset of allAssets) {
26
- if (asset.inline) {
27
- inlineAssets.push(asset);
28
- continue;
29
- }
30
- if (asset.packageRole === 'dynamic-chunk') {
31
- dynamicChunks.push(asset);
32
- continue;
33
- }
34
- if (!pageScript && asset.packageRole === 'page-script') {
35
- pageScript = asset;
36
- continue;
37
- }
38
- if (!pageStylesheet && asset.packageRole === 'page-style') {
39
- pageStylesheet = asset;
40
- continue;
41
- }
42
- if (asset.packageRole === 'keep-separate' || asset.packageRole === 'runtime') {
43
- separateAssets.push(asset);
44
- continue;
45
- }
46
- if (!pageScript && asset.kind === 'script' && !asset.excludeFromHtml) {
47
- pageScript = asset;
48
- continue;
49
- }
50
- if (!pageStylesheet && asset.kind === 'stylesheet') {
51
- pageStylesheet = asset;
52
- continue;
53
- }
54
- separateAssets.push(asset);
57
+ classifyProcessedAsset(asset, buckets);
55
58
  }
56
59
  return {
57
60
  assets: allAssets,
58
61
  pageBrowserGraph: options.pageBrowserGraph,
59
62
  htmlAssets: allAssets.filter((asset) => shouldIncludeInHtml(asset, suppressedSourceFilepaths)),
60
- pageScript,
61
- pageStylesheet,
62
- inlineAssets,
63
- separateAssets,
64
- dynamicChunks,
63
+ pageScript: buckets.pageScript,
64
+ pageStylesheet: buckets.pageStylesheet,
65
+ inlineAssets: buckets.inlineAssets,
66
+ separateAssets: buckets.separateAssets,
67
+ dynamicChunks: buckets.dynamicChunks,
65
68
  };
66
69
  }
67
70
  function shouldIncludeInHtml(asset, suppressedSourceFilepaths) {
@@ -0,0 +1,9 @@
1
+ # Error pages
2
+
3
+ This directory owns semantic 404/500 page behavior shared by request-time rendering and static export.
4
+
5
+ - `error-page-renderer.ts` applies filesystem → registered-loader → built-in precedence. Registered views reuse `prepareExplicitStaticRender`. Callers that need a guaranteed built-in document after a custom-page failure use `renderBuiltIn()`. Static export asks `resolveSourceFile()` before rendering so unchanged sources can skip work.
6
+ - `default-error-pages.ts` renders self-contained fallback documents with development-only diagnostics.
7
+ - `semantic-error-page-exporter.ts` writes `/404.html` and `/500.html` exactly once during static export.
8
+
9
+ Runtime adapters only wrap the rendered body in an HTTP response; they do not decide error-page source precedence.