@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
@@ -84,45 +84,32 @@ export class SchemaValidationService {
84
84
  * }
85
85
  * ```
86
86
  */
87
+ async validateRequestPart(schema, value, assign, allErrors, shouldValidate) {
88
+ if (!schema || !shouldValidate) {
89
+ return;
90
+ }
91
+ const result = await this.validateWithSchema(schema, value);
92
+ if (!result.success) {
93
+ allErrors.push(...(result.errors || []));
94
+ return;
95
+ }
96
+ assign(result.data);
97
+ }
87
98
  async validateRequest(source, schemas) {
88
99
  const validated = {};
89
100
  const allErrors = [];
90
- if (schemas.body && source.body !== undefined) {
91
- const result = await this.validateWithSchema(schemas.body, source.body);
92
- if (!result.success) {
93
- allErrors.push(...(result.errors || []));
94
- }
95
- else {
96
- validated.body = result.data;
97
- }
98
- }
99
- if (schemas.query && source.query) {
100
- const result = await this.validateWithSchema(schemas.query, source.query);
101
- if (!result.success) {
102
- allErrors.push(...(result.errors || []));
103
- }
104
- else {
105
- validated.query = result.data;
106
- }
107
- }
108
- if (schemas.headers && source.headers) {
109
- const result = await this.validateWithSchema(schemas.headers, source.headers);
110
- if (!result.success) {
111
- allErrors.push(...(result.errors || []));
112
- }
113
- else {
114
- validated.headers = result.data;
115
- }
116
- }
117
- if (schemas.params && source.params) {
118
- const result = await this.validateWithSchema(schemas.params, source.params);
119
- if (!result.success) {
120
- allErrors.push(...(result.errors || []));
121
- }
122
- else {
123
- validated.params = result.data;
124
- }
125
- }
101
+ await this.validateRequestPart(schemas.body, source.body, (data) => {
102
+ validated.body = data;
103
+ }, allErrors, schemas.body !== undefined && source.body !== undefined);
104
+ await this.validateRequestPart(schemas.query, source.query, (data) => {
105
+ validated.query = data;
106
+ }, allErrors, !!schemas.query && !!source.query);
107
+ await this.validateRequestPart(schemas.headers, source.headers, (data) => {
108
+ validated.headers = data;
109
+ }, allErrors, !!schemas.headers && !!source.headers);
110
+ await this.validateRequestPart(schemas.params, source.params, (data) => {
111
+ validated.params = data;
112
+ }, allErrors, !!schemas.params && !!source.params);
126
113
  if (allErrors.length > 0) {
127
114
  return { success: false, errors: allErrors };
128
115
  }
@@ -92,6 +92,10 @@ export declare class StaticSiteGenerator {
92
92
  }): Promise<void>;
93
93
  private createStaticExportContext;
94
94
  private invokeStaticExportHook;
95
+ private resetStaticGenerationState;
96
+ private ensureUnifiedGraphForStaticGeneration;
97
+ private executeStaticGenerationBody;
98
+ private finalizeStaticGenerationRun;
95
99
  /**
96
100
  * Executes the full static-generation workflow for one app run.
97
101
  */
@@ -319,13 +319,7 @@ export class StaticSiteGenerator {
319
319
  }
320
320
  }
321
321
  }
322
- /**
323
- * Executes the full static-generation workflow for one app run.
324
- */
325
- async run({ router, baseUrl, routeRendererFactory, staticRoutes, force = false, preserveExportDirectory = false, }) {
326
- const skippedDynamicPages = [];
327
- const activeStaticPathnames = new Set();
328
- const sitemapEligiblePathnames = this.appConfig.sitemap?.enabled ? new Set() : undefined;
322
+ resetStaticGenerationState(force) {
329
323
  this.forceFullStaticGeneration = force;
330
324
  this.staticRenderCacheContext = createRouteModuleStaticRenderCacheContext(this.appConfig);
331
325
  this.pageModuleLoader = undefined;
@@ -335,16 +329,72 @@ export class StaticSiteGenerator {
335
329
  if (shouldPrebuildProductionPageBrowserGraphs() && force) {
336
330
  clearProductionPageBrowserGraphSession(this.appConfig);
337
331
  }
338
- const routes = await router.listStaticGenerationRoutes({ runtimeOrigin: baseUrl });
339
- const exportRoutes = routes.map((route) => toEcopagesRouteInfo(route));
340
- if (shouldBuildPagesUnifiedGraph()) {
341
- await ensurePagesUnifiedGraphBuilt({
342
- appConfig: this.appConfig,
343
- entryPaths: routes.map((route) => route.templateRoute.filePath),
344
- outdir: getServerModuleBuildCacheOutdir(this.appConfig),
345
- force,
332
+ }
333
+ async ensureUnifiedGraphForStaticGeneration(routes, force) {
334
+ if (!shouldBuildPagesUnifiedGraph()) {
335
+ return;
336
+ }
337
+ await ensurePagesUnifiedGraphBuilt({
338
+ appConfig: this.appConfig,
339
+ entryPaths: routes.map((route) => route.templateRoute.filePath),
340
+ outdir: getServerModuleBuildCacheOutdir(this.appConfig),
341
+ force,
342
+ });
343
+ }
344
+ async executeStaticGenerationBody(input) {
345
+ if (shouldPrebuildProductionPageBrowserGraphs() && input.routeRendererFactory) {
346
+ await prebuildProductionPageBrowserGraphs(input.routes.map((route) => ({
347
+ routeFile: route.templateRoute.filePath,
348
+ params: route.params,
349
+ })), input.routeRendererFactory);
350
+ }
351
+ this.generateRobotsTxt();
352
+ await this.generateStaticPages({
353
+ router: input.router,
354
+ baseUrl: input.baseUrl,
355
+ routeRendererFactory: input.routeRendererFactory,
356
+ skipped: input.skippedDynamicPages,
357
+ activeStaticPathnames: input.activeStaticPathnames,
358
+ preloadedRoutes: input.routes,
359
+ sitemapEligiblePathnames: input.sitemapEligiblePathnames,
360
+ });
361
+ if (input.staticRoutes && input.staticRoutes.length > 0 && input.routeRendererFactory) {
362
+ await this.generateExplicitStaticPages({
363
+ staticRoutes: input.staticRoutes,
364
+ routeRendererFactory: input.routeRendererFactory,
365
+ skipped: input.skippedDynamicPages,
366
+ activeStaticPathnames: input.activeStaticPathnames,
367
+ sitemapEligiblePathnames: input.sitemapEligiblePathnames,
346
368
  });
347
369
  }
370
+ if (input.preserveExportDirectory) {
371
+ this.pruneStaleStaticOutputs(input.activeStaticPathnames);
372
+ }
373
+ }
374
+ finalizeStaticGenerationRun(input) {
375
+ if (this.appConfig.sitemap?.enabled && input.sitemapEligiblePathnames) {
376
+ const locations = resolveSitemapLocations({
377
+ eligiblePathnames: [...input.sitemapEligiblePathnames],
378
+ sitemap: this.appConfig.sitemap,
379
+ baseUrl: input.baseUrl,
380
+ });
381
+ this.generateSitemap(locations, this.appConfig.sitemap);
382
+ }
383
+ if (input.skippedDynamicPages.length > 0) {
384
+ appLogger.debug(`Skipped ${input.skippedDynamicPages.length} page(s) with cache: 'dynamic' (not supported in static generation)`, input.skippedDynamicPages);
385
+ }
386
+ }
387
+ /**
388
+ * Executes the full static-generation workflow for one app run.
389
+ */
390
+ async run({ router, baseUrl, routeRendererFactory, staticRoutes, force = false, preserveExportDirectory = false, }) {
391
+ const skippedDynamicPages = [];
392
+ const activeStaticPathnames = new Set();
393
+ const sitemapEligiblePathnames = this.appConfig.sitemap?.enabled ? new Set() : undefined;
394
+ this.resetStaticGenerationState(force);
395
+ const routes = await router.listStaticGenerationRoutes({ runtimeOrigin: baseUrl });
396
+ const exportRoutes = routes.map((route) => toEcopagesRouteInfo(route));
397
+ await this.ensureUnifiedGraphForStaticGeneration(routes, force);
348
398
  const staticExportContext = this.createStaticExportContext({
349
399
  router,
350
400
  baseUrl,
@@ -356,34 +406,17 @@ export class StaticSiteGenerator {
356
406
  });
357
407
  await this.invokeStaticExportHook('beforeStaticExport', staticExportContext);
358
408
  try {
359
- if (shouldPrebuildProductionPageBrowserGraphs() && routeRendererFactory) {
360
- await prebuildProductionPageBrowserGraphs(routes.map((route) => ({
361
- routeFile: route.templateRoute.filePath,
362
- params: route.params,
363
- })), routeRendererFactory);
364
- }
365
- this.generateRobotsTxt();
366
- await this.generateStaticPages({
409
+ await this.executeStaticGenerationBody({
367
410
  router,
368
411
  baseUrl,
369
412
  routeRendererFactory,
370
- skipped: skippedDynamicPages,
413
+ staticRoutes,
414
+ preserveExportDirectory,
415
+ routes,
416
+ skippedDynamicPages,
371
417
  activeStaticPathnames,
372
- preloadedRoutes: routes,
373
418
  sitemapEligiblePathnames,
374
419
  });
375
- if (staticRoutes && staticRoutes.length > 0 && routeRendererFactory) {
376
- await this.generateExplicitStaticPages({
377
- staticRoutes,
378
- routeRendererFactory,
379
- skipped: skippedDynamicPages,
380
- activeStaticPathnames,
381
- sitemapEligiblePathnames,
382
- });
383
- }
384
- if (preserveExportDirectory) {
385
- this.pruneStaleStaticOutputs(activeStaticPathnames);
386
- }
387
420
  }
388
421
  catch (error) {
389
422
  if (shouldPrebuildProductionPageBrowserGraphs()) {
@@ -394,17 +427,11 @@ export class StaticSiteGenerator {
394
427
  finally {
395
428
  await this.invokeStaticExportHook('afterStaticExport', staticExportContext);
396
429
  }
397
- if (this.appConfig.sitemap?.enabled && sitemapEligiblePathnames) {
398
- const locations = resolveSitemapLocations({
399
- eligiblePathnames: [...sitemapEligiblePathnames],
400
- sitemap: this.appConfig.sitemap,
401
- baseUrl,
402
- });
403
- this.generateSitemap(locations, this.appConfig.sitemap);
404
- }
405
- if (skippedDynamicPages.length > 0) {
406
- appLogger.debug(`Skipped ${skippedDynamicPages.length} page(s) with cache: 'dynamic' (not supported in static generation)`, skippedDynamicPages);
407
- }
430
+ this.finalizeStaticGenerationRun({
431
+ baseUrl,
432
+ skippedDynamicPages,
433
+ sitemapEligiblePathnames,
434
+ });
408
435
  }
409
436
  async generateExplicitStaticPages(input) {
410
437
  appLogger.debug('Generating explicit static routes', input.staticRoutes.map((r) => r.path));
@@ -16,6 +16,32 @@ function getEmbeddedRuntimeCommandOptions() {
16
16
  }
17
17
  const ECOPAGES_BIN_FILES = ['ecopages.ts', 'ecopages.js', 'cli.js'];
18
18
  const ECOPAGES_AVAILABLE_COMMANDS = ['dev', 'build', 'start', 'preview'];
19
+ function resolveEcopagesSubcommand(runtimeArgv) {
20
+ const ecopagesIndex = runtimeArgv.findIndex((arg) => ECOPAGES_BIN_FILES.some((filename) => arg.endsWith(filename)));
21
+ if (ecopagesIndex === -1) {
22
+ return '';
23
+ }
24
+ if (ecopagesIndex < runtimeArgv.length - 1 &&
25
+ ECOPAGES_AVAILABLE_COMMANDS.some((cmd) => runtimeArgv[ecopagesIndex + 1] === cmd)) {
26
+ return runtimeArgv[ecopagesIndex + 1];
27
+ }
28
+ return 'start';
29
+ }
30
+ function deriveCliCommandFlags(command, values) {
31
+ const isStartCommand = command === 'start' || (!values.dev && !values.build && !values.preview);
32
+ const isDevCommand = command === 'dev' || !!values.dev;
33
+ const isBuildCommand = command === 'build' || !!values.build;
34
+ const isPreviewCommand = command === 'preview' || !!values.preview;
35
+ return { isStartCommand, isDevCommand, isBuildCommand, isPreviewCommand };
36
+ }
37
+ function applyNodeEnvForCliCommand(isDevCommand) {
38
+ if (isDevCommand) {
39
+ process.env.NODE_ENV ??= 'development';
40
+ }
41
+ else {
42
+ process.env.NODE_ENV = 'production';
43
+ }
44
+ }
19
45
  /**
20
46
  * Parses command line arguments for the server.
21
47
  * It returns {@link ReturnParseCliArgs}
@@ -39,22 +65,8 @@ export function parseCliArgs(options = {}) {
39
65
  },
40
66
  allowPositionals: true,
41
67
  });
42
- let command = '';
43
- const ecopagesIndex = runtimeArgv.findIndex((arg) => ECOPAGES_BIN_FILES.some((filename) => arg.endsWith(filename)));
44
- const isAvailableCommand = ecopagesIndex !== -1;
45
- if (isAvailableCommand) {
46
- command =
47
- ecopagesIndex < runtimeArgv.length - 1 &&
48
- ECOPAGES_AVAILABLE_COMMANDS.some((cmd) => {
49
- return runtimeArgv[ecopagesIndex + 1] === cmd;
50
- })
51
- ? runtimeArgv[ecopagesIndex + 1]
52
- : 'start';
53
- }
54
- const isStartCommand = command === 'start' || (!values.dev && !values.build && !values.preview);
55
- const isDevCommand = command === 'dev' || !!values.dev;
56
- const isBuildCommand = command === 'build' || !!values.build;
57
- const isPreviewCommand = command === 'preview' || !!values.preview;
68
+ const command = resolveEcopagesSubcommand(runtimeArgv);
69
+ const { isStartCommand, isDevCommand, isBuildCommand, isPreviewCommand } = deriveCliCommandFlags(command, values);
58
70
  const parsedCommandOptions = {
59
71
  preview: isPreviewCommand,
60
72
  build: isBuildCommand,
@@ -66,11 +78,6 @@ export function parseCliArgs(options = {}) {
66
78
  hostname: values.hostname,
67
79
  reactFastRefresh: values['react-fast-refresh'],
68
80
  };
69
- if (isDevCommand) {
70
- process.env.NODE_ENV ??= 'development';
71
- }
72
- else {
73
- process.env.NODE_ENV = 'production';
74
- }
81
+ applyNodeEnvForCliCommand(isDevCommand);
75
82
  return parsedCommandOptions;
76
83
  }
@@ -92,6 +92,11 @@ export declare class ProjectWatcher {
92
92
  * @param event - The type of file system event
93
93
  */
94
94
  private handleFileChange;
95
+ private invalidatePageCachesForChange;
96
+ private applyRouteAndServerInvalidation;
97
+ private shouldDeferProcessorNotifications;
98
+ private handleDeferredHmrFileChange;
99
+ private handleStandardHmrFileChange;
95
100
  private processFileChange;
96
101
  /**
97
102
  * Re-imports server modules before HMR broadcast so reload/refetch does not
@@ -155,6 +155,47 @@ export class ProjectWatcher {
155
155
  this.pendingChangeEvents.set(filePath, { event, timer });
156
156
  return Promise.resolve();
157
157
  }
158
+ async invalidatePageCachesForChange(plan, resolvedFilePath) {
159
+ if (shouldClearAllPageHtmlCache(plan.category)) {
160
+ await clearAppPageCache(this.appConfig);
161
+ return;
162
+ }
163
+ await invalidateAppPageCacheBySourcePaths(this.appConfig, [resolvedFilePath]);
164
+ }
165
+ async applyRouteAndServerInvalidation(plan, filePath, resolvedFilePath, event, isRegisteredDevTransformEdit) {
166
+ if (plan.refreshRoutes && (event === 'unlink' || event === 'add')) {
167
+ getAppPageBrowserGraphSession(this.appConfig).invalidateByRouteFile(resolvedFilePath);
168
+ }
169
+ if (plan.invalidateServerModules && !isRegisteredDevTransformEdit) {
170
+ this.invalidationService.invalidateServerModules([filePath]);
171
+ }
172
+ if (plan.refreshRoutes) {
173
+ await this.refreshRouterRoutesCallback();
174
+ }
175
+ }
176
+ shouldDeferProcessorNotifications(plan, isRegisteredDevTransformEdit) {
177
+ return (plan.category === 'include-source' ||
178
+ plan.category === 'explicit-server-view' ||
179
+ isRegisteredDevTransformEdit);
180
+ }
181
+ async handleDeferredHmrFileChange(filePath, resolvedFilePath, event, plan, graphPreparation) {
182
+ await this.prewarmBeforeHmr(resolvedFilePath, plan);
183
+ await this.hmrManager.handleFileChange(filePath, {
184
+ graphIdentities: graphPreparation?.affectedGraphIdentities,
185
+ });
186
+ await this.notifyProcessors(filePath, event);
187
+ }
188
+ async handleStandardHmrFileChange(filePath, event, plan, graphPreparation) {
189
+ await this.notifyProcessors(filePath, event);
190
+ if (plan.processorHandledAsset) {
191
+ return;
192
+ }
193
+ if (plan.delegateToHmr) {
194
+ await this.hmrManager.handleFileChange(filePath, {
195
+ graphIdentities: graphPreparation?.affectedGraphIdentities,
196
+ });
197
+ }
198
+ }
158
199
  async processFileChange(filePath, event) {
159
200
  try {
160
201
  const plan = this.invalidationService.planFileChange(filePath);
@@ -164,50 +205,23 @@ export class ProjectWatcher {
164
205
  }
165
206
  this.uncacheModules();
166
207
  const resolvedFilePath = path.resolve(filePath);
167
- if (shouldClearAllPageHtmlCache(plan.category)) {
168
- await clearAppPageCache(this.appConfig);
169
- }
170
- else {
171
- await invalidateAppPageCacheBySourcePaths(this.appConfig, [resolvedFilePath]);
172
- }
208
+ await this.invalidatePageCachesForChange(plan, resolvedFilePath);
173
209
  const graphPreparation = this.hmrManager.isEnabled()
174
210
  ? prepareHmrFileChange(this.appConfig, resolvedFilePath)
175
211
  : undefined;
176
- if (plan.refreshRoutes && (event === 'unlink' || event === 'add')) {
177
- getAppPageBrowserGraphSession(this.appConfig).invalidateByRouteFile(resolvedFilePath);
178
- }
179
212
  const isRegisteredDevTransformEdit = isRegisteredDevTransformEntrypoint(this.hmrManager.getRegisteredEntrypoints(), resolvedFilePath);
180
- if (plan.invalidateServerModules && !isRegisteredDevTransformEdit) {
181
- this.invalidationService.invalidateServerModules([filePath]);
182
- }
183
- if (plan.refreshRoutes) {
184
- await this.refreshRouterRoutesCallback();
185
- }
213
+ await this.applyRouteAndServerInvalidation(plan, filePath, resolvedFilePath, event, isRegisteredDevTransformEdit);
186
214
  if (plan.reloadBrowser) {
187
215
  await this.notifyProcessors(filePath, event);
188
216
  this.requestBrowserReload();
189
217
  return;
190
218
  }
191
- const deferProcessorNotifications = plan.category === 'include-source' ||
192
- plan.category === 'explicit-server-view' ||
193
- isRegisteredDevTransformEdit;
219
+ const deferProcessorNotifications = this.shouldDeferProcessorNotifications(plan, isRegisteredDevTransformEdit);
194
220
  if (deferProcessorNotifications && plan.delegateToHmr) {
195
- await this.prewarmBeforeHmr(resolvedFilePath, plan);
196
- await this.hmrManager.handleFileChange(filePath, {
197
- graphIdentities: graphPreparation?.affectedGraphIdentities,
198
- });
199
- await this.notifyProcessors(filePath, event);
200
- return;
201
- }
202
- await this.notifyProcessors(filePath, event);
203
- if (plan.processorHandledAsset) {
221
+ await this.handleDeferredHmrFileChange(filePath, resolvedFilePath, event, plan, graphPreparation);
204
222
  return;
205
223
  }
206
- if (plan.delegateToHmr) {
207
- await this.hmrManager.handleFileChange(filePath, {
208
- graphIdentities: graphPreparation?.affectedGraphIdentities,
209
- });
210
- }
224
+ await this.handleStandardHmrFileChange(filePath, event, plan, graphPreparation);
211
225
  }
212
226
  catch (error) {
213
227
  if (error instanceof Error) {