@ecopages/core 0.2.0-rc.7 → 0.2.0-rc.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 (45) hide show
  1. package/package.json +27 -3
  2. package/src/adapters/abstract/segment-path-matcher.js +56 -38
  3. package/src/adapters/bun/create-app.d.ts +4 -0
  4. package/src/adapters/bun/create-app.js +46 -31
  5. package/src/adapters/shared/runtime/server-adapter.d.ts +1 -0
  6. package/src/adapters/shared/runtime/server-adapter.js +9 -22
  7. package/src/build/rolldown/rolldown-adapter-helpers.js +41 -25
  8. package/src/build/rolldown/rolldown-plugin-bridge.js +25 -21
  9. package/src/eco/README.md +3 -1
  10. package/src/eco/lazy-injector-map.js +57 -41
  11. package/src/hmr/strategies/js-hmr-strategy.d.ts +2 -0
  12. package/src/hmr/strategies/js-hmr-strategy.js +24 -16
  13. package/src/plugins/component-import-discovery.js +115 -77
  14. package/src/plugins/eco-component-meta-plugin.js +47 -30
  15. package/src/route-renderer/README.md +1 -0
  16. package/src/route-renderer/orchestration/custom-element-scripts/README.md +9 -0
  17. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.d.ts +16 -0
  18. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.js +34 -0
  19. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.d.ts +68 -0
  20. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.js +183 -0
  21. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.d.ts +9 -0
  22. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.js +22 -0
  23. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.d.ts +10 -0
  24. package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.js +46 -0
  25. package/src/route-renderer/orchestration/integration-renderer.d.ts +3 -0
  26. package/src/route-renderer/orchestration/integration-renderer.js +31 -17
  27. package/src/route-renderer/orchestration/ownership-graph/component-graph.js +55 -39
  28. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.d.ts +6 -0
  29. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.js +59 -36
  30. package/src/route-renderer/page-loading/component-dependency-collection.js +95 -74
  31. package/src/route-renderer/page-loading/ecopages-virtual-imports.js +41 -28
  32. package/src/route-renderer/page-loading/file-scoped-dependency-components.d.ts +0 -3
  33. package/src/route-renderer/page-loading/file-scoped-dependency-components.js +37 -34
  34. package/src/route-renderer/page-loading/page-dependency-bundling.js +14 -8
  35. package/src/router/README.md +2 -0
  36. package/src/services/assets/asset-processing-service/page-package.js +42 -39
  37. package/src/services/module-loading/page-module-import.service.d.ts +6 -0
  38. package/src/services/module-loading/page-module-import.service.js +60 -27
  39. package/src/services/validation/schema-validation-service.d.ts +1 -0
  40. package/src/services/validation/schema-validation-service.js +23 -36
  41. package/src/static-site-generator/static-site-generator.d.ts +4 -0
  42. package/src/static-site-generator/static-site-generator.js +75 -48
  43. package/src/utils/parse-cli-args.js +29 -22
  44. package/src/watchers/project-watcher.d.ts +5 -0
  45. package/src/watchers/project-watcher.js +46 -32
@@ -0,0 +1,183 @@
1
+ import { getComponentIdentity } from '../../../eco/component-identity.js';
2
+ import path from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
4
+ import { randomUUID } from 'node:crypto';
5
+ import { getCustomElementScriptPreloadScopeState, invalidateCustomElementScriptPreload, } from './custom-element-script-preload-invalidation.js';
6
+ export { invalidateCustomElementScriptPreload };
7
+ const registryKeys = new WeakMap();
8
+ export const CUSTOM_ELEMENT_SSR_PRELOAD_CACHE_SCOPES = {
9
+ lit: 'lit',
10
+ ecopagesJsx: 'ecopages-jsx',
11
+ };
12
+ function getRegistryKey(registry) {
13
+ let key = registryKeys.get(registry);
14
+ if (!key) {
15
+ key = randomUUID();
16
+ registryKeys.set(registry, key);
17
+ }
18
+ return key;
19
+ }
20
+ /**
21
+ * Detects preload failures that are expected for browser-only modules.
22
+ */
23
+ export function isExpectedCustomElementSsrPreloadError(error) {
24
+ const errorMessage = error instanceof Error ? error.message : String(error);
25
+ return (errorMessage.includes(`reading 'Element'`) ||
26
+ errorMessage.includes('window is not defined') ||
27
+ errorMessage.includes('document is not defined') ||
28
+ errorMessage.includes('navigator is not defined'));
29
+ }
30
+ function collectSsrScriptsFromConfig(config, scriptPaths, resolveDependencyPath, componentFile, requireLazyScriptEntry = false) {
31
+ if (!componentFile) {
32
+ return;
33
+ }
34
+ const componentDir = path.dirname(componentFile);
35
+ for (const script of config.dependencies?.scripts ?? []) {
36
+ if (typeof script === 'string' || script.ssr !== true || !script.src) {
37
+ continue;
38
+ }
39
+ if (requireLazyScriptEntry && !script.lazy) {
40
+ continue;
41
+ }
42
+ scriptPaths.add(resolveDependencyPath(componentDir, script.src));
43
+ }
44
+ }
45
+ /**
46
+ * Collects `dependencies.scripts` paths marked `ssr: true` from a component tree.
47
+ */
48
+ export function collectCustomElementSsrPreloadScripts(components, resolveDependencyPath, requireLazyScriptEntry = false) {
49
+ const scriptPaths = new Set();
50
+ const visitedConfigs = new Set();
51
+ const collect = (component) => {
52
+ const config = component?.config;
53
+ if (!config || visitedConfigs.has(config)) {
54
+ return;
55
+ }
56
+ visitedConfigs.add(config);
57
+ const componentFile = getComponentIdentity(component?.config)?.file;
58
+ collectSsrScriptsFromConfig(config, scriptPaths, resolveDependencyPath, componentFile, requireLazyScriptEntry);
59
+ for (const layout of config.layouts ?? []) {
60
+ collect(layout);
61
+ }
62
+ for (const nestedComponent of config.dependencies?.components || []) {
63
+ collect(nestedComponent);
64
+ }
65
+ };
66
+ for (const component of components) {
67
+ collect(component);
68
+ }
69
+ return Array.from(scriptPaths);
70
+ }
71
+ /**
72
+ * Preloads `dependencies.scripts` entries marked `ssr: true` before SSR.
73
+ *
74
+ * @remarks
75
+ * `lazy` controls browser delivery only; `ssr: true` opts into server evaluation
76
+ * for both eager and lazy script entries. State is keyed by `cacheScope` so
77
+ * concurrent renders and dev invalidation share one deduplicated import cache.
78
+ */
79
+ export class CustomElementScriptPreloader {
80
+ cacheScope;
81
+ enabled;
82
+ requireLazyScriptEntry;
83
+ resolveDependencyPath;
84
+ preferSourceImports;
85
+ resolvePreloadEntrypoint;
86
+ importServerModule;
87
+ logLabel;
88
+ constructor({ cacheScope, enabled = true, requireLazyScriptEntry = false, resolveDependencyPath, preferSourceImports, resolvePreloadEntrypoint, importServerModule, logLabel = 'ecopages', }) {
89
+ this.cacheScope = cacheScope;
90
+ this.enabled = enabled;
91
+ this.requireLazyScriptEntry = requireLazyScriptEntry;
92
+ this.resolveDependencyPath = resolveDependencyPath;
93
+ this.preferSourceImports = preferSourceImports ?? typeof Bun !== 'undefined';
94
+ this.resolvePreloadEntrypoint = resolvePreloadEntrypoint;
95
+ this.importServerModule = importServerModule;
96
+ this.logLabel = logLabel;
97
+ }
98
+ invalidateScript(scriptPath) {
99
+ invalidateCustomElementScriptPreload(scriptPath, this.cacheScope);
100
+ }
101
+ isExpectedSsrPreloadError(error) {
102
+ return isExpectedCustomElementSsrPreloadError(error);
103
+ }
104
+ collectSsrPreloadScripts(components) {
105
+ return collectCustomElementSsrPreloadScripts(components, this.resolveDependencyPath, this.requireLazyScriptEntry);
106
+ }
107
+ /**
108
+ * Preloads SSR-eligible scripts to register custom elements before render.
109
+ */
110
+ async preloadSsrScripts(components) {
111
+ if (!this.enabled) {
112
+ return;
113
+ }
114
+ const scopeState = getCustomElementScriptPreloadScopeState(this.cacheScope);
115
+ const registry = globalThis.customElements;
116
+ if (scopeState.activeRegistry !== registry) {
117
+ scopeState.preloadedScripts.clear();
118
+ scopeState.preloadFailedScripts.clear();
119
+ scopeState.inFlightImports.clear();
120
+ scopeState.activeRegistry = registry;
121
+ }
122
+ const scripts = this.collectSsrPreloadScripts(components);
123
+ if (scripts.length === 0) {
124
+ return;
125
+ }
126
+ await Promise.all(scripts.map((scriptPath) => this.preloadOneScript(scriptPath, scopeState, registry)));
127
+ }
128
+ async preloadOneScript(scriptPath, scopeState, registry) {
129
+ if (scopeState.preloadedScripts.has(scriptPath) || scopeState.preloadFailedScripts.has(scriptPath)) {
130
+ return;
131
+ }
132
+ let inFlight = scopeState.inFlightImports.get(scriptPath);
133
+ if (inFlight) {
134
+ await inFlight;
135
+ return;
136
+ }
137
+ inFlight = (async () => {
138
+ try {
139
+ await this.importScriptForPreload(scriptPath, registry);
140
+ scopeState.preloadedScripts.add(scriptPath);
141
+ }
142
+ catch (error) {
143
+ scopeState.preloadFailedScripts.add(scriptPath);
144
+ if (this.isExpectedSsrPreloadError(error)) {
145
+ if (process.env.ECOPAGES_DEBUG === 'true') {
146
+ console.warn(`[ecopages][${this.logLabel}] Skipping SSR preload for browser-only script: ${scriptPath}`);
147
+ }
148
+ return;
149
+ }
150
+ console.warn(`[ecopages][${this.logLabel}] Failed to preload SSR script: ${scriptPath}`, error);
151
+ }
152
+ finally {
153
+ scopeState.inFlightImports.delete(scriptPath);
154
+ }
155
+ })();
156
+ scopeState.inFlightImports.set(scriptPath, inFlight);
157
+ await inFlight;
158
+ }
159
+ async importScriptForPreload(scriptPath, registry) {
160
+ const registryKey = registry ? getRegistryKey(registry) : 'default';
161
+ if (this.resolvePreloadEntrypoint) {
162
+ const preloadEntrypoint = await this.resolvePreloadEntrypoint(scriptPath);
163
+ const importUrl = pathToFileURL(preloadEntrypoint);
164
+ if (registry && typeof Bun === 'undefined') {
165
+ importUrl.searchParams.set('eco-ssr-registry', registryKey);
166
+ }
167
+ await import(/* @vite-ignore */ importUrl.href);
168
+ return;
169
+ }
170
+ if (this.importServerModule) {
171
+ await this.importServerModule(scriptPath, registryKey);
172
+ return;
173
+ }
174
+ if (!this.preferSourceImports) {
175
+ throw new Error(`Cannot preload SSR script ${scriptPath}: configure appConfig.runtime.appModuleLoader or enable source imports (Bun)`);
176
+ }
177
+ const importUrl = pathToFileURL(scriptPath);
178
+ if (registry && typeof Bun === 'undefined') {
179
+ importUrl.searchParams.set('eco-ssr-registry', registryKey);
180
+ }
181
+ await import(/* @vite-ignore */ importUrl.href);
182
+ }
183
+ }
@@ -0,0 +1,9 @@
1
+ import type { EcoPagesAppConfig } from '../../../types/internal-types.js';
2
+ /**
3
+ * Imports one registered script through the app server module loader for SSR.
4
+ *
5
+ * @remarks
6
+ * Uses an isolated outdir per custom-element registry so concurrent renders do
7
+ * not share mutable module evaluation state.
8
+ */
9
+ export declare function createCustomElementServerModuleImporter(appConfig: EcoPagesAppConfig, outdirSegment: string): (filePath: string, registryKey: string) => Promise<unknown>;
@@ -0,0 +1,22 @@
1
+ import path from 'node:path';
2
+ /**
3
+ * Imports one registered script through the app server module loader for SSR.
4
+ *
5
+ * @remarks
6
+ * Uses an isolated outdir per custom-element registry so concurrent renders do
7
+ * not share mutable module evaluation state.
8
+ */
9
+ export function createCustomElementServerModuleImporter(appConfig, outdirSegment) {
10
+ return async (filePath, registryKey) => {
11
+ const loader = appConfig.runtime?.appModuleLoader;
12
+ if (!loader) {
13
+ throw new Error('Custom element SSR script preload requires the app server module loader');
14
+ }
15
+ return loader.importModule({
16
+ filePath,
17
+ rootDir: appConfig.rootDir,
18
+ outdir: path.join(appConfig.absolutePaths.workDir, outdirSegment, registryKey),
19
+ forceBuild: true,
20
+ });
21
+ };
22
+ }
@@ -0,0 +1,10 @@
1
+ import { type AssetDefinition, type ProcessedAsset } from '../../../services/assets/asset-processing-service/index.js';
2
+ export type CustomElementSsrPreloadEntrypointResolverOptions = {
3
+ preferSourceImports: boolean;
4
+ processDependencies?: (dependencies: AssetDefinition[], integrationName: string) => Promise<ProcessedAsset[]>;
5
+ };
6
+ /**
7
+ * Resolves SSR script paths through the asset pipeline so preload imports share
8
+ * the renderer dependency graph instead of an isolated app-module bundle.
9
+ */
10
+ export declare function createCustomElementSsrPreloadEntrypointResolver({ preferSourceImports, processDependencies, }: CustomElementSsrPreloadEntrypointResolverOptions): (scriptPath: string) => Promise<string>;
@@ -0,0 +1,46 @@
1
+ import { AssetFactory, } from '../../../services/assets/asset-processing-service/index.js';
2
+ /**
3
+ * Resolves SSR script paths through the asset pipeline so preload imports share
4
+ * the renderer dependency graph instead of an isolated app-module bundle.
5
+ */
6
+ export function createCustomElementSsrPreloadEntrypointResolver({ preferSourceImports, processDependencies, }) {
7
+ const entrypointCache = new Map();
8
+ return async (scriptPath) => {
9
+ const cachedEntrypoint = entrypointCache.get(scriptPath);
10
+ if (cachedEntrypoint) {
11
+ return cachedEntrypoint;
12
+ }
13
+ if (preferSourceImports) {
14
+ entrypointCache.set(scriptPath, scriptPath);
15
+ return scriptPath;
16
+ }
17
+ if (!processDependencies) {
18
+ entrypointCache.set(scriptPath, scriptPath);
19
+ return scriptPath;
20
+ }
21
+ try {
22
+ const processed = await processDependencies([
23
+ AssetFactory.createInlineFileScript({
24
+ filepath: scriptPath,
25
+ excludeFromHtml: true,
26
+ position: 'head',
27
+ bundle: true,
28
+ attributes: {
29
+ type: 'module',
30
+ defer: '',
31
+ },
32
+ }),
33
+ ], `custom-element-ssr-preload:${scriptPath}`);
34
+ const entrypoint = processed[0]?.filepath ?? scriptPath;
35
+ entrypointCache.set(scriptPath, entrypoint);
36
+ return entrypoint;
37
+ }
38
+ catch (error) {
39
+ if (process.env.ECOPAGES_DEBUG === 'true') {
40
+ console.warn(`[ecopages] Failed to resolve custom-element SSR preload entrypoint for: ${scriptPath}`, error);
41
+ }
42
+ entrypointCache.set(scriptPath, scriptPath);
43
+ return scriptPath;
44
+ }
45
+ };
46
+ }
@@ -219,6 +219,9 @@ export declare abstract class IntegrationRenderer<C = EcoPagesElement> {
219
219
  transformDocumentHtml?: (html: string) => string;
220
220
  foreignChildRoots?: ReadonlyArray<EcoComponent | Partial<EcoComponent>>;
221
221
  }): Promise<string>;
222
+ private serializeStringRenderChildren;
223
+ private assertStringRenderChildren;
224
+ private assertStringRenderContent;
222
225
  protected renderStringComponentWithSerializedChildren(input: ComponentRenderInput, component: (props: Record<string, unknown>) => Promise<EcoPagesElement> | EcoPagesElement): Promise<ComponentRenderResult>;
223
226
  /**
224
227
  * Renders a string-first component, then resolves any queued foreign
@@ -366,26 +366,40 @@ export class IntegrationRenderer {
366
366
  appendProcessedDependencies: (...assetGroups) => this.appendProcessedDependencies(...assetGroups),
367
367
  }, input, this.DOC_TYPE);
368
368
  }
369
- async renderStringComponentWithSerializedChildren(input, component) {
370
- const serializedChildren = input.children === undefined
371
- ? undefined
372
- : typeof input.children === 'string'
373
- ? input.children
374
- : isMarkupNodeLike(input.children) && typeof input.children.outerHTML === 'string'
375
- ? input.children.outerHTML
376
- : undefined;
377
- if (input.children !== undefined && serializedChildren === undefined) {
378
- const componentFile = input.component.config?.identity?.file ?? 'unknown component';
379
- const childTag = Object.prototype.toString.call(input.children);
380
- throw new TypeError(`[ecopages] ${this.name} renderer expected serialized children for ${componentFile}, received ${childTag}.`);
369
+ serializeStringRenderChildren(children) {
370
+ if (children === undefined) {
371
+ return undefined;
372
+ }
373
+ if (typeof children === 'string') {
374
+ return children;
375
+ }
376
+ if (isMarkupNodeLike(children) && typeof children.outerHTML === 'string') {
377
+ return children.outerHTML;
378
+ }
379
+ return undefined;
380
+ }
381
+ assertStringRenderChildren(input, serializedChildren) {
382
+ if (input.children === undefined || serializedChildren !== undefined) {
383
+ return;
384
+ }
385
+ const componentFile = input.component.config?.identity?.file ?? 'unknown component';
386
+ const childTag = Object.prototype.toString.call(input.children);
387
+ throw new TypeError(`[ecopages] ${this.name} renderer expected serialized children for ${componentFile}, received ${childTag}.`);
388
+ }
389
+ assertStringRenderContent(input, content) {
390
+ if (typeof content === 'string') {
391
+ return;
381
392
  }
393
+ const componentFile = input.component.config?.identity?.file ?? 'unknown component';
394
+ const contentTag = Object.prototype.toString.call(content);
395
+ throw new TypeError(`[ecopages] ${this.name} renderer expected a string render result for ${componentFile}, received ${contentTag}.`);
396
+ }
397
+ async renderStringComponentWithSerializedChildren(input, component) {
398
+ const serializedChildren = this.serializeStringRenderChildren(input.children);
399
+ this.assertStringRenderChildren(input, serializedChildren);
382
400
  const props = serializedChildren === undefined ? input.props : { ...input.props, children: serializedChildren };
383
401
  const content = await component(props);
384
- if (typeof content !== 'string') {
385
- const componentFile = input.component.config?.identity?.file ?? 'unknown component';
386
- const contentTag = Object.prototype.toString.call(content);
387
- throw new TypeError(`[ecopages] ${this.name} renderer expected a string render result for ${componentFile}, received ${contentTag}.`);
388
- }
402
+ this.assertStringRenderContent(input, content);
389
403
  const html = content;
390
404
  const assets = input.component.config?.dependencies &&
391
405
  typeof this.assetProcessingService?.processDependencies === 'function'
@@ -1,24 +1,57 @@
1
1
  import { getComponentIdentity } from '../../../eco/component-identity.js';
2
- export function walkComponentGraph(input) {
3
- const seenComponents = new Set();
4
- const seenConfigs = new Set();
2
+ function visitComponentGraphConfig(config, parentIntegrationName, state, visitComponent) {
3
+ if (!config || state.seenConfigs.has(config)) {
4
+ return;
5
+ }
6
+ state.seenConfigs.add(config);
7
+ if (state.onConfig) {
8
+ state.onConfig(config);
9
+ }
10
+ if (state.visitLayout) {
11
+ for (const layout of config.layouts ?? []) {
12
+ if (layout?.config) {
13
+ visitComponentGraphConfig(layout.config, parentIntegrationName, state, visitComponent);
14
+ }
15
+ }
16
+ }
17
+ for (const child of config.dependencies?.components ?? []) {
18
+ visitComponent(child, parentIntegrationName, 'dependency');
19
+ }
20
+ }
21
+ function notifyComponentGraphConfig(config, state) {
22
+ if (!state.onConfig || !config || state.seenConfigs.has(config)) {
23
+ return;
24
+ }
25
+ state.seenConfigs.add(config);
26
+ state.onConfig(config);
27
+ }
28
+ function visitComponentGraphChildren(ecoComponent, integrationName, state, visitComponent) {
29
+ if (state.visitLayout) {
30
+ for (const layout of ecoComponent.config?.layouts ?? []) {
31
+ if (layout?.config) {
32
+ visitComponentGraphConfig(layout.config, integrationName, state, visitComponent);
33
+ }
34
+ }
35
+ }
36
+ for (const child of ecoComponent.config?.dependencies?.components ?? []) {
37
+ visitComponent(child, integrationName, 'dependency');
38
+ }
39
+ }
40
+ function createComponentGraphVisitor(state) {
5
41
  const visitComponent = (component, parentIntegrationName, source) => {
6
42
  if (!component) {
7
43
  return;
8
44
  }
9
45
  const ecoComponent = component;
10
- if (seenComponents.has(ecoComponent)) {
46
+ if (state.seenComponents.has(ecoComponent)) {
11
47
  return;
12
48
  }
13
- seenComponents.add(ecoComponent);
49
+ state.seenComponents.add(ecoComponent);
14
50
  const identity = getComponentIdentity(ecoComponent);
15
51
  const integrationName = ecoComponent.config?.integration ?? identity?.integration ?? parentIntegrationName;
16
- if (input.onConfig && ecoComponent.config && !seenConfigs.has(ecoComponent.config)) {
17
- seenConfigs.add(ecoComponent.config);
18
- input.onConfig(ecoComponent.config);
19
- }
20
- if (input.onComponent) {
21
- const shouldContinue = input.onComponent({
52
+ notifyComponentGraphConfig(ecoComponent.config, state);
53
+ if (state.onComponent) {
54
+ const shouldContinue = state.onComponent({
22
55
  component: ecoComponent,
23
56
  parentIntegrationName,
24
57
  source,
@@ -27,36 +60,19 @@ export function walkComponentGraph(input) {
27
60
  return;
28
61
  }
29
62
  }
30
- if (input.visitLayout) {
31
- for (const layout of ecoComponent.config?.layouts ?? []) {
32
- if (layout?.config) {
33
- visitConfig(layout.config, integrationName);
34
- }
35
- }
36
- }
37
- for (const child of ecoComponent.config?.dependencies?.components ?? []) {
38
- visitComponent(child, integrationName, 'dependency');
39
- }
63
+ visitComponentGraphChildren(ecoComponent, integrationName, state, visitComponent);
40
64
  };
41
- const visitConfig = (config, parentIntegrationName) => {
42
- if (!config || seenConfigs.has(config)) {
43
- return;
44
- }
45
- seenConfigs.add(config);
46
- if (input.onConfig) {
47
- input.onConfig(config);
48
- }
49
- if (input.visitLayout) {
50
- for (const layout of config.layouts ?? []) {
51
- if (layout?.config) {
52
- visitConfig(layout.config, parentIntegrationName);
53
- }
54
- }
55
- }
56
- for (const child of config.dependencies?.components ?? []) {
57
- visitComponent(child, parentIntegrationName, 'dependency');
58
- }
65
+ return visitComponent;
66
+ }
67
+ export function walkComponentGraph(input) {
68
+ const state = {
69
+ seenComponents: new Set(),
70
+ seenConfigs: new Set(),
71
+ visitLayout: input.visitLayout,
72
+ onComponent: input.onComponent,
73
+ onConfig: input.onConfig,
59
74
  };
75
+ const visitComponent = createComponentGraphVisitor(state);
60
76
  for (const root of input.roots) {
61
77
  visitComponent(root.component, input.currentIntegrationName, root.source);
62
78
  }
@@ -42,6 +42,12 @@ export declare class PageBrowserGraphService {
42
42
  private buildPageBrowserGraphRecord;
43
43
  private materializePageBrowserGraph;
44
44
  private resolveGroupedPageBrowserAssets;
45
+ private collectGroupedBuildPlanContributions;
46
+ private registerRouteGroupedDependencies;
47
+ private mergePeerGroupedContributions;
48
+ private addGroupedDependencyFilePaths;
49
+ private matchGroupedAssetsByRoute;
50
+ private addProcessedGroupedAssetDependencyPaths;
45
51
  private buildGroupedPageBrowserAssets;
46
52
  private partitionPageBrowserGraphAssets;
47
53
  }
@@ -147,29 +147,36 @@ export class PageBrowserGraphService {
147
147
  cacheable: groupedCacheable,
148
148
  };
149
149
  }
150
- async buildGroupedPageBrowserAssets(input, currentContribution) {
150
+ collectGroupedBuildPlanContributions(input) {
151
+ if (!input.groupedBuildPlan) {
152
+ return { contributions: [], hasCollectionFailures: false };
153
+ }
154
+ return {
155
+ contributions: input.groupedBuildPlan.instances.map((instance) => ({
156
+ routeFile: instance.routeFile,
157
+ dependencyInstanceKey: instance.dependencyInstanceKey,
158
+ contribution: instance.contribution,
159
+ })),
160
+ hasCollectionFailures: false,
161
+ };
162
+ }
163
+ registerRouteGroupedDependencies(routeLookupKey, routeFile, routeGroupedDependencies, groupedDependencies, groupedAssetKeysByRoute, routeDisplayPaths, dependencyPaths) {
164
+ if (routeGroupedDependencies.length === 0) {
165
+ return;
166
+ }
167
+ groupedDependencies.push(...routeGroupedDependencies);
168
+ groupedAssetKeysByRoute.set(routeLookupKey, new Set(routeGroupedDependencies.map((dep) => getGroupedBundleAssetKey(dep.groupedBundle))));
169
+ routeDisplayPaths.set(routeLookupKey, routeFile);
170
+ dependencyPaths.add(path.resolve(routeFile));
171
+ }
172
+ mergePeerGroupedContributions(input, currentContribution, groupedContributions, hasCollectionFailures) {
151
173
  const dependencyInstanceKey = input.dependencyInstanceKey ?? '';
152
174
  const currentRouteLookupKey = createRouteGraphLookupKey(input.routeFile, dependencyInstanceKey);
153
- const groupedCollection = input.groupedBuildPlan
154
- ? {
155
- contributions: input.groupedBuildPlan.instances.map((instance) => ({
156
- routeFile: instance.routeFile,
157
- dependencyInstanceKey: instance.dependencyInstanceKey,
158
- contribution: instance.contribution,
159
- })),
160
- hasCollectionFailures: false,
161
- }
162
- : {
163
- contributions: [],
164
- hasCollectionFailures: false,
165
- };
166
- const groupedContributions = groupedCollection.contributions;
167
175
  const currentRouteGroupedDependencies = (currentContribution.dependencies ?? []).filter((dep) => isGroupedContentScriptAsset(dep));
168
176
  const groupedDependencies = [...currentRouteGroupedDependencies];
169
177
  const groupedAssetKeysByRoute = new Map();
170
178
  const routeDisplayPaths = new Map();
171
179
  const dependencyPaths = new Set([path.resolve(input.routeFile)]);
172
- let hasCollectionFailures = groupedCollection.hasCollectionFailures;
173
180
  if (currentRouteGroupedDependencies.length > 0) {
174
181
  groupedAssetKeysByRoute.set(currentRouteLookupKey, new Set(currentRouteGroupedDependencies.map((dep) => getGroupedBundleAssetKey(dep.groupedBundle))));
175
182
  routeDisplayPaths.set(currentRouteLookupKey, input.routeFile);
@@ -184,40 +191,38 @@ export class PageBrowserGraphService {
184
191
  continue;
185
192
  }
186
193
  const routeGroupedDependencies = contribution.dependencies.filter((dep) => isGroupedContentScriptAsset(dep));
187
- if (routeGroupedDependencies.length === 0) {
188
- continue;
189
- }
190
- groupedDependencies.push(...routeGroupedDependencies);
191
- groupedAssetKeysByRoute.set(createRouteGraphLookupKey(routeFile, groupedContribution.dependencyInstanceKey), new Set(routeGroupedDependencies.map((dep) => getGroupedBundleAssetKey(dep.groupedBundle))));
192
- routeDisplayPaths.set(createRouteGraphLookupKey(routeFile, groupedContribution.dependencyInstanceKey), routeFile);
193
- dependencyPaths.add(path.resolve(routeFile));
194
- }
195
- if (groupedDependencies.length === 0) {
196
- return {
197
- assetsByRoute: new Map(),
198
- dependencyPaths,
199
- hasCollectionFailures,
200
- cacheable: Boolean(input.groupedBuildPlan) && !hasCollectionFailures,
201
- };
194
+ this.registerRouteGroupedDependencies(createRouteGraphLookupKey(routeFile, groupedContribution.dependencyInstanceKey), routeFile, routeGroupedDependencies, groupedDependencies, groupedAssetKeysByRoute, routeDisplayPaths, dependencyPaths);
202
195
  }
196
+ return {
197
+ groupedDependencies,
198
+ groupedAssetKeysByRoute,
199
+ routeDisplayPaths,
200
+ dependencyPaths,
201
+ hasCollectionFailures,
202
+ };
203
+ }
204
+ addGroupedDependencyFilePaths(groupedDependencies, dependencyPaths) {
203
205
  for (const dependency of groupedDependencies) {
204
206
  if (dependency.source === 'file') {
205
207
  dependencyPaths.add(path.resolve(dependency.filepath));
206
208
  }
207
209
  }
208
- const processedGroupedDependencies = await this.assetProcessingService.processDependencies(groupedDependencies, `${input.integrationName}:grouped-page-browser-graph`);
210
+ }
211
+ matchGroupedAssetsByRoute(groupedAssetKeysByRoute, routeDisplayPaths, groupedAssetsByBundleKey) {
209
212
  const groupedAssetsByRoute = new Map();
210
- const groupedAssetsByBundleKey = indexGroupedAssetsByBundleKey(processedGroupedDependencies);
211
- for (const [routeFile, groupedAssetKeys] of groupedAssetKeysByRoute) {
213
+ for (const [routeLookupKey, groupedAssetKeys] of groupedAssetKeysByRoute) {
212
214
  const matchedAssets = [...groupedAssetKeys]
213
215
  .flatMap((assetKey) => groupedAssetsByBundleKey.get(assetKey) ?? [])
214
216
  .sort((left, right) => left.index - right.index)
215
217
  .map(({ asset }) => asset);
216
218
  if (groupedAssetKeys.size > 0 && matchedAssets.length === 0) {
217
- appLogger.warn(`Grouped page-browser assets for ${routeDisplayPaths.get(routeFile) ?? routeFile} are missing groupedBundle metadata after processing. Hydration scripts may be omitted from HTML.`);
219
+ appLogger.warn(`Grouped page-browser assets for ${routeDisplayPaths.get(routeLookupKey) ?? routeLookupKey} are missing groupedBundle metadata after processing. Hydration scripts may be omitted from HTML.`);
218
220
  }
219
- groupedAssetsByRoute.set(routeFile, matchedAssets);
221
+ groupedAssetsByRoute.set(routeLookupKey, matchedAssets);
220
222
  }
223
+ return groupedAssetsByRoute;
224
+ }
225
+ addProcessedGroupedAssetDependencyPaths(processedGroupedDependencies, dependencyPaths) {
221
226
  for (const asset of processedGroupedDependencies) {
222
227
  if (asset.sourceFilepath) {
223
228
  dependencyPaths.add(path.resolve(asset.sourceFilepath));
@@ -226,6 +231,24 @@ export class PageBrowserGraphService {
226
231
  dependencyPaths.add(path.resolve(bundledSourceFilepath));
227
232
  }
228
233
  }
234
+ }
235
+ async buildGroupedPageBrowserAssets(input, currentContribution) {
236
+ const groupedCollection = this.collectGroupedBuildPlanContributions(input);
237
+ const merged = this.mergePeerGroupedContributions(input, currentContribution, groupedCollection.contributions, groupedCollection.hasCollectionFailures);
238
+ const { groupedDependencies, groupedAssetKeysByRoute, routeDisplayPaths, dependencyPaths, hasCollectionFailures, } = merged;
239
+ if (groupedDependencies.length === 0) {
240
+ return {
241
+ assetsByRoute: new Map(),
242
+ dependencyPaths,
243
+ hasCollectionFailures,
244
+ cacheable: Boolean(input.groupedBuildPlan) && !hasCollectionFailures,
245
+ };
246
+ }
247
+ this.addGroupedDependencyFilePaths(groupedDependencies, dependencyPaths);
248
+ const processedGroupedDependencies = await this.assetProcessingService.processDependencies(groupedDependencies, `${input.integrationName}:grouped-page-browser-graph`);
249
+ const groupedAssetsByBundleKey = indexGroupedAssetsByBundleKey(processedGroupedDependencies);
250
+ const groupedAssetsByRoute = this.matchGroupedAssetsByRoute(groupedAssetKeysByRoute, routeDisplayPaths, groupedAssetsByBundleKey);
251
+ this.addProcessedGroupedAssetDependencyPaths(processedGroupedDependencies, dependencyPaths);
229
252
  return {
230
253
  assetsByRoute: groupedAssetsByRoute,
231
254
  dependencyPaths,