@ecopages/core 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +2 -2
  2. package/package.json +14 -4
  3. package/src/adapters/node/create-app.d.ts +5 -5
  4. package/src/adapters/node/create-app.js +1 -1
  5. package/src/adapters/node/server-adapter.d.ts +1 -0
  6. package/src/adapters/node/server-adapter.js +4 -1
  7. package/src/adapters/shared/hmr/hmr-manager.d.ts +2 -0
  8. package/src/adapters/shared/hmr/hmr-manager.js +8 -4
  9. package/src/adapters/shared/http/api-request-pipeline.d.ts +16 -3
  10. package/src/adapters/shared/http/api-request-pipeline.js +23 -16
  11. package/src/adapters/shared/http/define-api-handler.d.ts +8 -8
  12. package/src/adapters/shared/http/explicit-static-route-matcher.js +8 -4
  13. package/src/adapters/shared/http/file-route-middleware-pipeline.d.ts +10 -11
  14. package/src/adapters/shared/http/file-route-middleware-pipeline.js +5 -6
  15. package/src/adapters/shared/http/fs-server-response-matcher.js +12 -4
  16. package/src/adapters/shared/http/run-middleware-chain.d.ts +29 -2
  17. package/src/adapters/shared/http/run-middleware-chain.js +108 -10
  18. package/src/adapters/shared/runtime/render-context.d.ts +2 -0
  19. package/src/adapters/shared/runtime/render-context.js +6 -21
  20. package/src/adapters/shared/runtime/server-request-pipeline.d.ts +2 -2
  21. package/src/adapters/shared/runtime/server-request-pipeline.js +4 -3
  22. package/src/adapters/shared/runtime/server-static-builder.js +4 -1
  23. package/src/adapters/shared/runtime/unattached-scripts.d.ts +17 -0
  24. package/src/adapters/shared/runtime/unattached-scripts.js +57 -0
  25. package/src/adapters/shared/ws/node-http-websocket-upgrades.js +3 -2
  26. package/src/adapters/shared/ws/ws-pattern-matcher.d.ts +14 -1
  27. package/src/adapters/shared/ws/ws-pattern-matcher.js +14 -13
  28. package/src/assets/app-browser-client-build.js +3 -0
  29. package/src/assets/asset-processing-service/asset-processing.service.d.ts +9 -0
  30. package/src/assets/asset-processing-service/asset-processing.service.js +19 -0
  31. package/src/assets/asset-processing-service/processed-asset-ledger.d.ts +16 -2
  32. package/src/assets/asset-processing-service/processed-asset-ledger.js +31 -3
  33. package/src/build/build-graph.d.ts +19 -2
  34. package/src/build/build-graph.js +19 -18
  35. package/src/build/cache/pages-unified-graph-build.d.ts +4 -6
  36. package/src/build/cache/pages-unified-graph-build.js +14 -51
  37. package/src/build/cache/server-module-entries.d.ts +5 -9
  38. package/src/build/cache/server-module-entries.js +5 -58
  39. package/src/build/contracts/build-contracts.d.ts +15 -0
  40. package/src/build/contracts.d.ts +1 -1
  41. package/src/build/contracts.js +1 -1
  42. package/src/build/production-asset-ledger.d.ts +1 -1
  43. package/src/build/production-asset-ledger.js +4 -2
  44. package/src/build/rolldown/file-role-grammar-plugin.d.ts +11 -0
  45. package/src/build/rolldown/file-role-grammar-plugin.js +32 -0
  46. package/src/build/rolldown/rolldown-adapter-helpers.d.ts +11 -0
  47. package/src/build/rolldown/rolldown-adapter-helpers.js +79 -10
  48. package/src/build/rolldown/ssr-script-entries-plugin.d.ts +31 -0
  49. package/src/build/rolldown/ssr-script-entries-plugin.js +104 -0
  50. package/src/build/runtime/build-request-policy.js +4 -1
  51. package/src/build/server/server-modules-manifest.d.ts +6 -1
  52. package/src/build/server/server-modules-manifest.js +10 -1
  53. package/src/build/server/view-reference.d.ts +9 -0
  54. package/src/build/server/view-reference.js +11 -0
  55. package/src/build.d.ts +3 -1
  56. package/src/build.js +3 -1
  57. package/src/cli.d.ts +14 -0
  58. package/src/cli.js +14 -0
  59. package/src/config/assembly/app-runtime.d.ts +13 -0
  60. package/src/config/assembly/app-runtime.js +2 -0
  61. package/src/config/assembly/finalize-config.js +13 -8
  62. package/src/config/internal-types.d.ts +0 -12
  63. package/src/config/resolve-eco-config-path.d.ts +2 -0
  64. package/src/config/resolve-eco-config-path.js +2 -1
  65. package/src/config/server-config-bundle.d.ts +0 -1
  66. package/src/config/server-config-bundle.js +1 -1
  67. package/src/config/user-config-types.d.ts +0 -12
  68. package/src/declarations.d.ts +2 -0
  69. package/src/dev/invalidation/development-invalidation.service.d.ts +0 -2
  70. package/src/dev/invalidation/development-invalidation.service.js +1 -11
  71. package/src/dev/transform-server/dev-transform-vendor-registry.js +33 -19
  72. package/src/eco/discovered-dependencies.d.ts +5 -0
  73. package/src/eco/discovered-dependencies.js +9 -0
  74. package/src/eco/eco.browser.js +6 -26
  75. package/src/eco/eco.js +24 -16
  76. package/src/eco/migration-docs.d.ts +7 -0
  77. package/src/eco/migration-docs.js +7 -0
  78. package/src/eco/page-data-hooks.d.ts +27 -0
  79. package/src/eco/page-data-hooks.js +64 -0
  80. package/src/eco/page-layout-normalization.d.ts +2 -2
  81. package/src/hmr/client/hmr-runtime.js +1 -1
  82. package/src/hmr/strategies/js-hmr-strategy.d.ts +0 -2
  83. package/src/hmr/strategies/server-rendered-template-hmr-strategy.d.ts +2 -1
  84. package/src/hmr/strategies/server-rendered-template-hmr-strategy.js +4 -3
  85. package/src/html/html-rewriter.d.ts +9 -7
  86. package/src/html/html-rewriter.js +26 -41
  87. package/src/html/html-transformer.service.d.ts +2 -2
  88. package/src/html/html-transformer.service.js +21 -15
  89. package/src/html-pages/html-page-module-scripts.js +2 -1
  90. package/src/html-pages/html-pages.plugin.d.ts +1 -1
  91. package/src/html-pages/html-pages.plugin.js +1 -1
  92. package/src/index.d.ts +1 -0
  93. package/src/index.js +1 -0
  94. package/src/integration.d.ts +2 -2
  95. package/src/integration.js +2 -2
  96. package/src/islands/island-host.d.ts +16 -5
  97. package/src/islands/island-host.js +8 -1
  98. package/src/middleware/create-middleware.d.ts +41 -0
  99. package/src/middleware/create-middleware.js +72 -0
  100. package/src/plugins/component-import-discovery.d.ts +14 -1
  101. package/src/plugins/component-import-discovery.js +36 -16
  102. package/src/plugins/eco-component-meta-plugin.d.ts +38 -0
  103. package/src/plugins/eco-component-meta-plugin.js +208 -44
  104. package/src/plugins/ecopages-virtual-imports.d.ts +15 -0
  105. package/src/plugins/ecopages-virtual-imports.js +25 -0
  106. package/src/plugins/integration-plugin.d.ts +1 -1
  107. package/src/plugins/integration-plugin.js +1 -1
  108. package/src/plugins/tsconfig-import-resolver.d.ts +2 -0
  109. package/src/plugins/tsconfig-import-resolver.js +7 -5
  110. package/src/route-renderer/error-pages/error-page-renderer.js +32 -3
  111. package/src/route-renderer/explicit-view-render-preparation.d.ts +28 -3
  112. package/src/route-renderer/explicit-view-render-preparation.js +63 -11
  113. package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.d.ts +15 -5
  114. package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.js +27 -15
  115. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +24 -9
  116. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +73 -50
  117. package/src/route-renderer/orchestration/integration-renderer.d.ts +42 -11
  118. package/src/route-renderer/orchestration/integration-renderer.js +83 -43
  119. package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.builder.js +0 -3
  120. package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.d.ts +36 -11
  121. package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.js +74 -62
  122. package/src/route-renderer/page-declaration.d.ts +25 -0
  123. package/src/route-renderer/page-declaration.js +39 -0
  124. package/src/route-renderer/page-loading/component-declarations.d.ts +65 -0
  125. package/src/route-renderer/page-loading/component-declarations.js +77 -0
  126. package/src/route-renderer/page-loading/component-dependency-collection.d.ts +4 -16
  127. package/src/route-renderer/page-loading/component-dependency-collection.js +36 -105
  128. package/src/route-renderer/page-loading/declared-asset-collection.d.ts +19 -14
  129. package/src/route-renderer/page-loading/declared-asset-collection.js +66 -67
  130. package/src/route-renderer/page-loading/dependency-resolver.d.ts +18 -10
  131. package/src/route-renderer/page-loading/dependency-resolver.js +58 -99
  132. package/src/route-renderer/page-loading/lazy-entry-collection.d.ts +19 -26
  133. package/src/route-renderer/page-loading/lazy-entry-collection.js +53 -64
  134. package/src/route-renderer/page-loading/module-declaration-aggregation.d.ts +17 -3
  135. package/src/route-renderer/page-loading/module-declaration-aggregation.js +14 -13
  136. package/src/route-renderer/page-loading/page-module-loader.d.ts +31 -12
  137. package/src/route-renderer/page-loading/page-module-loader.js +66 -35
  138. package/src/route-renderer/page-loading/resolved-page-dependencies.js +14 -9
  139. package/src/router/server/pages-dir.d.ts +7 -0
  140. package/src/router/server/pages-dir.js +7 -0
  141. package/src/router/server/route-registry.d.ts +22 -10
  142. package/src/router/server/route-registry.js +54 -23
  143. package/src/router/server/segment-path-matcher.d.ts +29 -5
  144. package/src/router/server/segment-path-matcher.js +22 -19
  145. package/src/static-site-generator/production-page-browser-graph-prebuild.d.ts +2 -2
  146. package/src/static-site-generator/production-page-browser-graph-prebuild.js +5 -5
  147. package/src/static-site-generator/static-site-generator.d.ts +17 -2
  148. package/src/static-site-generator/static-site-generator.js +52 -18
  149. package/src/types/contracts.d.ts +216 -160
  150. package/src/types/eco-api-types.d.ts +86 -51
  151. package/src/types/locals-brand.d.ts +9 -0
  152. package/src/types/locals-brand.js +9 -0
  153. package/src/types/middleware-types.d.ts +143 -0
  154. package/src/types/middleware-types.js +1 -0
  155. package/src/types/public-types.d.ts +3 -2
  156. package/src/utils/file-roles.d.ts +48 -0
  157. package/src/utils/file-roles.js +85 -0
  158. package/src/utils/node-builtin.d.ts +10 -0
  159. package/src/utils/node-builtin.js +13 -0
  160. package/src/watchers/project-watcher.js +2 -2
  161. package/src/build/contracts/server-only-specifier.d.ts +0 -8
  162. package/src/build/contracts/server-only-specifier.js +0 -11
  163. package/src/route-renderer/orchestration/route-pipeline/marker-artifact.utils.d.ts +0 -5
  164. package/src/route-renderer/orchestration/route-pipeline/marker-artifact.utils.js +0 -11
  165. package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.d.ts +0 -4
  166. package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.js +0 -30
  167. package/src/route-renderer/page-loading/ecopages-virtual-imports.d.ts +0 -12
  168. package/src/route-renderer/page-loading/ecopages-virtual-imports.js +0 -100
  169. package/src/utils/endpoint-file-extensions.d.ts +0 -11
  170. package/src/utils/endpoint-file-extensions.js +0 -13
  171. package/src/utils/locals-utils.d.ts +0 -15
  172. package/src/utils/locals-utils.js +0 -34
@@ -0,0 +1,39 @@
1
+ import { path } from '@ecopages/file-system/path';
2
+ import { getComponentIdentity } from '../eco/component-identity.js';
3
+ import { isEcoDeclaredComponent } from '../eco/eco-declared-component.js';
4
+ /**
5
+ * Whether `page` is a declared component whose build-time identity names `file`, absolute or relative to `rootDir`.
6
+ *
7
+ * @remarks
8
+ * The identity transform stamps the file that holds the literal `eco.page(...)` call, so a Page made by a
9
+ * wrapper in another file, or re-exported from one, names that other file.
10
+ */
11
+ export function isPageDeclaredIn(file, page, rootDir) {
12
+ const identityFile = isEcoDeclaredComponent(page) ? getComponentIdentity(page)?.file : undefined;
13
+ return identityFile !== undefined && path.resolve(rootDir, identityFile) === path.resolve(rootDir, file);
14
+ }
15
+ /**
16
+ * Whether `page` is a function carrying a `config`, which is what a wrapped or re-exported `eco.page()` result
17
+ * looks like when {@link isPageDeclaredIn} fails.
18
+ */
19
+ export function isUnattributedPage(page) {
20
+ return typeof page === 'function' && typeof page.config === 'object';
21
+ }
22
+ /** The error for a view or route file whose Page the build did not attribute to it. */
23
+ export function createLiteralPageDeclarationError(file) {
24
+ return new Error(`[ecopages] ${file} default-exports an eco.page() result that the build did not attribute to this file. A route file declares its Page with a literal \`eco.page(...)\` call in that file. The build finds eco.page, eco.layout, eco.component and eco.html by their literal call, so a wrapper function, an alias such as \`const page = eco.page\`, a renamed import, or a Page declared in another file and re-exported gives the Page no identity or another file's, and server options such as props and metadata stay in the browser bundle. Call eco.page(...) directly in ${file}.`);
25
+ }
26
+ /**
27
+ * Fails when a view loaded from `file` is not a Page that file declares with a literal `eco.page(...)` call.
28
+ *
29
+ * @remarks
30
+ * Applies the route-file check to views passed to `app.static()`, `app.errorPage()` and `ctx.render()`, so a view
31
+ * behaves the same however it is routed. A value that is not a declared component at all is left to the
32
+ * renderer lookup, which reports a missing integration.
33
+ * @throws When `view` carries a config that the build did not attribute to `file`.
34
+ */
35
+ export function assertViewDeclaredIn(file, view, rootDir) {
36
+ if (isUnattributedPage(view) && !isPageDeclaredIn(file, view, rootDir)) {
37
+ throw createLiteralPageDeclarationError(file);
38
+ }
39
+ }
@@ -0,0 +1,65 @@
1
+ import type { EcoComponentConfig } from '../../types/contracts.js';
2
+ import { type DeclaredAsset } from './declared-asset-collection.js';
3
+ import { type DeclaredLazyScript } from './lazy-entry-collection.js';
4
+ import { type ModuleDeclaration } from './module-declaration-aggregation.js';
5
+ /** An inferred stylesheet and whether only a plain module imports it. */
6
+ export type InferredStyle = {
7
+ filepath: string;
8
+ plainModule: boolean;
9
+ };
10
+ /**
11
+ * The assets one component config declares, resolved against the app root.
12
+ *
13
+ * @remarks
14
+ * It covers the config alone, not its nested Components, so collection walks the graph and merges the
15
+ * declarations of each config it reaches.
16
+ */
17
+ export type ComponentDeclarations = {
18
+ /** The identity file, absolute. */
19
+ file: string;
20
+ /** Stylesheets, then eager scripts, in declaration order. */
21
+ assets: ReadonlyArray<DeclaredAsset>;
22
+ lazyScripts: ReadonlyArray<DeclaredLazyScript>;
23
+ /** Declared `modules` and the `ecopages:` virtual modules the declaring module imports, in merge order. */
24
+ modules: ReadonlyArray<ModuleDeclaration>;
25
+ explicitStylePaths: ReadonlyArray<string>;
26
+ inferredStyles: ReadonlyArray<InferredStyle>;
27
+ packageStylePaths: ReadonlyMap<string, string>;
28
+ /** Whether the config itself ships a browser script: an eager or lazy script, or a module. */
29
+ declaresScript: boolean;
30
+ };
31
+ export type ComponentDeclarationErrors = {
32
+ invalidStylesheetEntry: string;
33
+ invalidScriptEntry: string;
34
+ lazyScriptMissingSrc: string;
35
+ };
36
+ export type ComponentDeclarationStoreOptions = {
37
+ /** The app root that root-relative identity files and discovered stylesheets resolve against. */
38
+ rootDir: string;
39
+ /** Maps lazy script sources to the public URLs used when no build output URL exists. */
40
+ resolveLazyScripts: (componentDir: string, scripts: string[]) => string;
41
+ errors: ComponentDeclarationErrors;
42
+ };
43
+ /**
44
+ * Names the app browser client entry of a lazy script for the Integration that owns its component.
45
+ *
46
+ * @remarks `entryHash` is {@link DeclaredLazyScript.entryHash}; only the owner varies between renders.
47
+ */
48
+ export declare function createLazyEntryName(integrationName: string, entryHash: string): string;
49
+ /**
50
+ * Resolves the declarations of each component config once per app.
51
+ *
52
+ * @remarks
53
+ * A config is a value of the module that declares it, so its declarations are fixed for the life of that module
54
+ * instance: the file paths, the virtual imports discovery recorded and the lazy entry keys never change while the config
55
+ * exists. A development edit re-imports the module, and the new config is resolved afresh. Configs are held
56
+ * weakly, so a dropped module instance takes its declarations with it.
57
+ */
58
+ export declare class ComponentDeclarationStore {
59
+ private readonly declarationsByConfig;
60
+ private readonly options;
61
+ constructor(options: ComponentDeclarationStoreOptions);
62
+ /** Returns the declarations of `config`, or `undefined` when it has no identity file and so takes no part. */
63
+ get(config: EcoComponentConfig): ComponentDeclarations | undefined;
64
+ private resolve;
65
+ }
@@ -0,0 +1,77 @@
1
+ import { path } from '@ecopages/file-system/path';
2
+ import { getComponentIdentity } from '../../eco/component-identity.js';
3
+ import { resolveDiscoveredStylesheets } from '../../eco/discovered-dependency-paths.js';
4
+ import { declareAssetEntries, resolveDeclaredPath } from './declared-asset-collection.js';
5
+ import { declareLazyScriptEntries } from './lazy-entry-collection.js';
6
+ import { declareModules } from './module-declaration-aggregation.js';
7
+ import { getDiscoveredVirtualImports } from '../../eco/discovered-dependencies.js';
8
+ /**
9
+ * Names the app browser client entry of a lazy script for the Integration that owns its component.
10
+ *
11
+ * @remarks `entryHash` is {@link DeclaredLazyScript.entryHash}; only the owner varies between renders.
12
+ */
13
+ export function createLazyEntryName(integrationName, entryHash) {
14
+ return `ecopages-${integrationName}-lazy-${entryHash}`;
15
+ }
16
+ /**
17
+ * Resolves the declarations of each component config once per app.
18
+ *
19
+ * @remarks
20
+ * A config is a value of the module that declares it, so its declarations are fixed for the life of that module
21
+ * instance: the file paths, the virtual imports discovery recorded and the lazy entry keys never change while the config
22
+ * exists. A development edit re-imports the module, and the new config is resolved afresh. Configs are held
23
+ * weakly, so a dropped module instance takes its declarations with it.
24
+ */
25
+ export class ComponentDeclarationStore {
26
+ declarationsByConfig = new WeakMap();
27
+ options;
28
+ constructor(options) {
29
+ this.options = options;
30
+ }
31
+ /** Returns the declarations of `config`, or `undefined` when it has no identity file and so takes no part. */
32
+ get(config) {
33
+ let declarations = this.declarationsByConfig.get(config);
34
+ if (declarations === undefined) {
35
+ declarations = this.resolve(config);
36
+ this.declarationsByConfig.set(config, declarations);
37
+ }
38
+ return declarations ?? undefined;
39
+ }
40
+ resolve(config) {
41
+ const identityFile = getComponentIdentity(config)?.file;
42
+ if (!identityFile)
43
+ return null;
44
+ const { rootDir, errors } = this.options;
45
+ const file = path.resolve(rootDir, identityFile);
46
+ const dir = path.dirname(file);
47
+ const stylesheetEntries = config.dependencies?.stylesheets ?? [];
48
+ const scriptEntries = config.dependencies?.scripts ?? [];
49
+ const discovered = resolveDiscoveredStylesheets(config, rootDir);
50
+ const assets = declareAssetEntries({ stylesheetEntries, scriptEntries, componentDir: dir, errors });
51
+ const lazyScripts = declareLazyScriptEntries({
52
+ scriptEntries,
53
+ rootDir,
54
+ componentFile: file,
55
+ componentDir: dir,
56
+ resolveLazyScripts: this.options.resolveLazyScripts,
57
+ lazyScriptMissingSrcMessage: errors.lazyScriptMissingSrc,
58
+ });
59
+ const modules = declareModules(config.dependencies?.modules, getDiscoveredVirtualImports(config));
60
+ return {
61
+ file,
62
+ assets,
63
+ lazyScripts,
64
+ modules,
65
+ explicitStylePaths: stylesheetEntries
66
+ .map((style) => (typeof style === 'string' ? style : style.src))
67
+ .filter((src) => Boolean(src))
68
+ .map((src) => resolveDeclaredPath(dir, src)),
69
+ inferredStyles: [
70
+ ...discovered.imported.map((filepath) => ({ filepath, plainModule: false })),
71
+ ...discovered.plainModules.map((filepath) => ({ filepath, plainModule: true })),
72
+ ],
73
+ packageStylePaths: discovered.packagePaths,
74
+ declaresScript: modules.length > 0 || lazyScripts.length > 0 || assets.some((asset) => asset.kind === 'script'),
75
+ };
76
+ }
77
+ }
@@ -1,6 +1,7 @@
1
1
  import type { EcoComponent } from '../../types/contracts.js';
2
2
  import type { AssetDefinition } from '../../assets/asset-processing-service/index.js';
3
- import { type LazyGroup } from './lazy-entry-collection.js';
3
+ import type { LazyGroup } from './lazy-entry-collection.js';
4
+ import { type ComponentDeclarationStore } from './component-declarations.js';
4
5
  /**
5
6
  * Aggregated dependency output for one dependency-collection pass.
6
7
  */
@@ -17,25 +18,12 @@ export type CollectedComponentDependencies = {
17
18
  type CollectComponentDependenciesOptions = {
18
19
  components: Array<EcoComponent | Partial<EcoComponent> | undefined | null>;
19
20
  integrationName: string;
20
- /** The app root that root-relative identity files and discovered stylesheets resolve against. */
21
- rootDir: string;
22
- /**
23
- * Skips nested components owned by another Integration, and their descendants.
24
- *
25
- * @remarks Those are Foreign Children: their owning Integration renders them and emits their assets.
26
- */
27
- excludeForeignChildren?: boolean;
21
+ /** The declarations of each component config the walk reaches. */
22
+ declarations: ComponentDeclarationStore;
28
23
  /**
29
24
  * Treats each supplied component as an already flattened graph node.
30
25
  */
31
26
  skipNestedComponents?: boolean;
32
- resolveLazyScripts: (componentDir: string, scripts: string[]) => string;
33
- createEcopagesJsxLazyEntryName: (integrationName: string, key: string) => string;
34
- errors: {
35
- invalidStylesheetEntry: string;
36
- invalidScriptEntry: string;
37
- lazyScriptMissingSrc: string;
38
- };
39
27
  };
40
28
  /**
41
29
  * Walks component dependency trees and collects declared assets, module declarations,
@@ -1,67 +1,45 @@
1
- import { path } from '@ecopages/file-system/path';
2
1
  import { assertEcoDeclaredComponent } from '../../eco/eco-declared-component.js';
3
2
  import { getComponentIdentity, getComponentOwner } from '../../eco/component-identity.js';
4
- import { resolveDiscoveredStylesheets } from '../../eco/discovered-dependency-paths.js';
5
3
  import { AssetFactory } from '../../assets/asset-processing-service/index.js';
6
- import { extractEcopagesVirtualImports } from './ecopages-virtual-imports.js';
7
- import { collectDeclaredAssetEntries } from './declared-asset-collection.js';
8
4
  import { getLazyTriggerKey } from './lazy-trigger-planning.js';
9
- import { collectLazyScriptEntries } from './lazy-entry-collection.js';
10
5
  import { createModuleScriptName, createNamedImportModuleSource, createNamespaceImportModuleSource, } from './module-declaration-scripts.js';
11
- import { collectModuleDeclarations } from './module-declaration-aggregation.js';
12
- const MODULE_SCRIPT_ATTRIBUTES = {
13
- type: 'module',
14
- defer: '',
15
- };
16
- function createModuleScriptAttributes(attributes) {
17
- return {
18
- ...MODULE_SCRIPT_ATTRIBUTES,
19
- ...attributes,
20
- };
21
- }
6
+ import { mergeModuleDeclaration } from './module-declaration-aggregation.js';
7
+ import { createModuleScriptAttributes } from './declared-asset-collection.js';
8
+ import { createLazyEntryName, } from './component-declarations.js';
22
9
  function pushUniqueDependency(keys, key, dependencies, dependency) {
23
- if (keys.has(key)) {
24
- return false;
25
- }
10
+ if (keys.has(key))
11
+ return;
26
12
  keys.add(key);
27
13
  dependencies.push(dependency);
28
- return true;
29
14
  }
30
- function resolveDependencyPath(componentDir, pathUrl) {
31
- return path.isAbsolute(pathUrl) ? pathUrl : path.join(componentDir, pathUrl);
32
- }
33
- function createLazyScriptRegistrar(config, lazyScriptsByConfig) {
34
- return ({ lazy, lazyKey, fallbackUrl }) => {
35
- let grouped = lazyScriptsByConfig.get(config);
36
- if (!grouped) {
37
- grouped = new Map();
38
- lazyScriptsByConfig.set(config, grouped);
39
- }
40
- const triggerKey = getLazyTriggerKey(lazy);
41
- const existing = grouped.get(triggerKey) ?? { lazy, scripts: [] };
42
- existing.scripts.push({ lazyKey, fallbackUrl });
43
- grouped.set(triggerKey, existing);
44
- };
45
- }
46
- function recordExplicitAndInferredStyles(config, dir, context) {
47
- const dependenciesConfig = config.dependencies;
48
- const explicitStyles = dependenciesConfig?.stylesheets ?? [];
49
- for (const style of explicitStyles) {
50
- const src = typeof style === 'string' ? style : style.src;
51
- if (src)
52
- context.explicitStylePaths.add(resolveDependencyPath(dir, src));
53
- }
54
- const discovered = resolveDiscoveredStylesheets(config, context.rootDir);
55
- for (const filepath of discovered.imported) {
56
- context.inferredStyles.push({ filepath, plainModule: false });
15
+ /** Adds one config's declarations to the pass, skipping assets an earlier config already added. */
16
+ function mergeComponentDeclarations(config, declarations, context) {
17
+ for (const filepath of declarations.explicitStylePaths)
18
+ context.explicitStylePaths.add(filepath);
19
+ context.inferredStyles.push(...declarations.inferredStyles);
20
+ for (const [filepath, packagePath] of declarations.packageStylePaths)
21
+ context.packageStylePaths.set(filepath, packagePath);
22
+ for (const { kind, key, asset } of declarations.assets) {
23
+ const keys = kind === 'stylesheet' ? context.stylesheetDependencyKeys : context.scriptDependencyKeys;
24
+ pushUniqueDependency(keys, key, context.dependencies, asset);
57
25
  }
58
- for (const filepath of discovered.plainModules) {
59
- context.inferredStyles.push({ filepath, plainModule: true });
26
+ for (const declaration of declarations.modules)
27
+ mergeModuleDeclaration(context.modulesMap, declaration);
28
+ if (declarations.lazyScripts.length === 0)
29
+ return;
30
+ const owner = getComponentOwner(config, context.integrationName);
31
+ let groups = context.lazyScriptsByConfig.get(config);
32
+ if (!groups) {
33
+ groups = new Map();
34
+ context.lazyScriptsByConfig.set(config, groups);
60
35
  }
61
- for (const [filepath, packagePath] of discovered.packagePaths) {
62
- context.packageStylePaths.set(filepath, packagePath);
36
+ for (const script of declarations.lazyScripts) {
37
+ pushUniqueDependency(context.lazyDependencyKeys, script.key, context.dependencies, script.createAsset(createLazyEntryName(owner, script.entryHash)));
38
+ const triggerKey = getLazyTriggerKey(script.lazy);
39
+ const group = groups.get(triggerKey) ?? { lazy: script.lazy, scripts: [] };
40
+ group.scripts.push({ lazyKey: script.lazyKey, fallbackUrl: script.fallbackUrl });
41
+ groups.set(triggerKey, group);
63
42
  }
64
- return explicitStyles;
65
43
  }
66
44
  /**
67
45
  * Orders inferred stylesheets once each, leaving out paths with an explicit entry.
@@ -85,12 +63,6 @@ function orderInferredStylesheets(styles, explicitStylePaths) {
85
63
  });
86
64
  return [...positions].sort(([, left], [, right]) => left.index - right.index).map(([filepath]) => filepath);
87
65
  }
88
- /**
89
- * @remarks A nested component without an Integration inherits its parent's, so it is not a Foreign Child.
90
- */
91
- function isForeignChild(component, integrationName) {
92
- return getComponentOwner(component, integrationName) !== integrationName;
93
- }
94
66
  /**
95
67
  * Recursively visits one component config and its declared child dependencies.
96
68
  */
@@ -98,45 +70,11 @@ function collectComponentConfigDependencies(config, context) {
98
70
  if (!config || context.visited.has(config))
99
71
  return;
100
72
  context.visited.add(config);
101
- const identityFile = getComponentIdentity(config)?.file;
102
- if (!identityFile)
73
+ const declarations = context.declarations.get(config);
74
+ if (!declarations)
103
75
  return;
104
- const file = path.resolve(context.rootDir, identityFile);
105
- const dir = path.dirname(file);
106
- const explicitStyles = recordExplicitAndInferredStyles(config, dir, context);
107
- const dependenciesConfig = config.dependencies;
108
- const registerLazyScript = createLazyScriptRegistrar(config, context.lazyScriptsByConfig);
109
- collectDeclaredAssetEntries({
110
- stylesheetEntries: explicitStyles,
111
- scriptEntries: dependenciesConfig?.scripts ?? [],
112
- componentDir: dir,
113
- dependencies: context.dependencies,
114
- stylesheetDependencyKeys: context.stylesheetDependencyKeys,
115
- scriptDependencyKeys: context.scriptDependencyKeys,
116
- resolveDependencyPath,
117
- createModuleScriptAttributes,
118
- pushUniqueDependency,
119
- getInvalidStylesheetEntryMessage: () => context.errors.invalidStylesheetEntry,
120
- getInvalidScriptEntryMessage: () => context.errors.invalidScriptEntry,
121
- });
122
- collectModuleDeclarations(context.modulesMap, dependenciesConfig?.modules, extractEcopagesVirtualImports(file));
123
- collectLazyScriptEntries({
124
- scriptEntries: dependenciesConfig?.scripts ?? [],
125
- rootDir: context.rootDir,
126
- componentFile: file,
127
- componentDir: dir,
128
- integrationName: getComponentOwner(config, context.integrationName),
129
- dependencies: context.dependencies,
130
- lazyDependencyKeys: context.lazyDependencyKeys,
131
- registerLazyScript,
132
- resolveDependencyPath,
133
- resolveLazyScripts: context.resolveLazyScripts,
134
- createModuleScriptAttributes,
135
- createEcopagesJsxLazyEntryName: context.createEcopagesJsxLazyEntryName,
136
- pushUniqueDependency,
137
- getLazyScriptMissingSrcMessage: () => context.errors.lazyScriptMissingSrc,
138
- });
139
- collectNestedComponentDependencies(dependenciesConfig?.components ?? [], file, context);
76
+ mergeComponentDeclarations(config, declarations, context);
77
+ collectNestedComponentDependencies(config.dependencies?.components ?? [], declarations.file, context);
140
78
  }
141
79
  function collectNestedComponentDependencies(nestedComponents, parentComponentFile, context) {
142
80
  for (const nestedComponent of nestedComponents) {
@@ -147,9 +85,6 @@ function collectNestedComponentDependencies(nestedComponents, parentComponentFil
147
85
  if (context.skipNestedComponents) {
148
86
  continue;
149
87
  }
150
- if (context.excludeForeignChildren && isForeignChild(nestedComponent, context.integrationName)) {
151
- continue;
152
- }
153
88
  collectComponentConfigDependencies(nestedComponent.config, context);
154
89
  }
155
90
  }
@@ -166,7 +101,7 @@ function collectNestedComponentDependencies(nestedComponents, parentComponentFil
166
101
  * later during render output.
167
102
  */
168
103
  export function collectComponentDependencies(options) {
169
- const { components, integrationName, rootDir, excludeForeignChildren = false, skipNestedComponents = false, resolveLazyScripts, createEcopagesJsxLazyEntryName, errors, } = options;
104
+ const { components, integrationName, declarations, skipNestedComponents = false } = options;
170
105
  const dependencies = [];
171
106
  const lazyScriptsByConfig = new Map();
172
107
  const lazyDependencyKeys = new Set();
@@ -185,12 +120,8 @@ export function collectComponentDependencies(options) {
185
120
  continue;
186
121
  const collectionContext = {
187
122
  integrationName,
188
- rootDir,
189
- excludeForeignChildren,
123
+ declarations,
190
124
  skipNestedComponents,
191
- resolveLazyScripts,
192
- createEcopagesJsxLazyEntryName,
193
- errors,
194
125
  dependencies,
195
126
  lazyScriptsByConfig,
196
127
  lazyDependencyKeys,
@@ -1,24 +1,29 @@
1
1
  import type { EcoComponentScriptEntry, EcoComponentStylesheetEntry } from '../../types/contracts.js';
2
2
  import type { AssetDefinition } from '../../assets/asset-processing-service/index.js';
3
- type CollectDeclaredAssetEntriesOptions = {
3
+ /** One declared stylesheet or eager script, with the key that dedupes it within a collection pass. */
4
+ export type DeclaredAsset = {
5
+ kind: 'stylesheet' | 'script';
6
+ key: string;
7
+ asset: AssetDefinition;
8
+ };
9
+ type DeclareAssetEntriesOptions = {
4
10
  stylesheetEntries: Array<string | EcoComponentStylesheetEntry>;
5
11
  scriptEntries: Array<string | EcoComponentScriptEntry>;
6
12
  componentDir: string;
7
- dependencies: AssetDefinition[];
8
- stylesheetDependencyKeys: Set<string>;
9
- scriptDependencyKeys: Set<string>;
10
- resolveDependencyPath: (componentDir: string, pathUrl: string) => string;
11
- createModuleScriptAttributes: (attributes?: Record<string, string>) => Record<string, string>;
12
- pushUniqueDependency: (keys: Set<string>, key: string, dependencies: AssetDefinition[], dependency: AssetDefinition) => boolean;
13
- getInvalidStylesheetEntryMessage: () => string;
14
- getInvalidScriptEntryMessage: () => string;
13
+ errors: {
14
+ invalidStylesheetEntry: string;
15
+ invalidScriptEntry: string;
16
+ };
15
17
  };
18
+ /** Resolves a declared asset path against the directory of the component that declares it. */
19
+ export declare function resolveDeclaredPath(componentDir: string, pathUrl: string): string;
20
+ /** Script attributes a declared module script gets unless it overrides them. */
21
+ export declare function createModuleScriptAttributes(attributes?: Record<string, string>): Record<string, string>;
16
22
  /**
17
- * Normalizes non-lazy stylesheet and script declarations into asset definitions.
23
+ * Normalizes a component's stylesheet and non-lazy script declarations into asset definitions, stylesheets first.
18
24
  *
19
- * Duplicate declarations are filtered through the provided key sets so callers can
20
- * compose this helper into larger recursive collection passes without re-emitting
21
- * the same asset multiple times.
25
+ * @remarks
26
+ * Each carries the key collection dedupes it by, so the same declaration from two components is emitted once.
22
27
  */
23
- export declare function collectDeclaredAssetEntries(options: CollectDeclaredAssetEntriesOptions): void;
28
+ export declare function declareAssetEntries(options: DeclareAssetEntriesOptions): DeclaredAsset[];
24
29
  export {};
@@ -1,78 +1,77 @@
1
1
  import { AssetFactory } from '../../assets/asset-processing-service/index.js';
2
+ import { path } from '@ecopages/file-system/path';
3
+ /** Resolves a declared asset path against the directory of the component that declares it. */
4
+ export function resolveDeclaredPath(componentDir, pathUrl) {
5
+ return path.isAbsolute(pathUrl) ? pathUrl : path.join(componentDir, pathUrl);
6
+ }
7
+ /** Script attributes a declared module script gets unless it overrides them. */
8
+ export function createModuleScriptAttributes(attributes) {
9
+ return { type: 'module', defer: '', ...attributes };
10
+ }
2
11
  function isDependencyEntryObject(entry) {
3
12
  return typeof entry === 'object' && entry !== null;
4
13
  }
5
- function getDependencyEntrySrc(entry) {
6
- return isDependencyEntryObject(entry) ? entry.src : entry;
14
+ function readEntry(entry) {
15
+ return isDependencyEntryObject(entry)
16
+ ? { src: entry.src, content: entry.content, attributes: entry.attributes }
17
+ : { src: entry };
7
18
  }
8
- function getDependencyEntryContent(entry) {
9
- return isDependencyEntryObject(entry) ? entry.content : undefined;
19
+ function declareStylesheet(style, componentDir, invalidEntry) {
20
+ const { src, content, attributes } = readEntry(style);
21
+ if (content) {
22
+ return {
23
+ kind: 'stylesheet',
24
+ key: `style:content:${content}:${JSON.stringify(attributes ?? {})}`,
25
+ asset: AssetFactory.createContentStylesheet({ content, position: 'head', attributes }),
26
+ };
27
+ }
28
+ if (!src)
29
+ throw new Error(invalidEntry);
30
+ const filepath = resolveDeclaredPath(componentDir, src);
31
+ return {
32
+ kind: 'stylesheet',
33
+ key: `style:file:${filepath}:${JSON.stringify(attributes ?? {})}`,
34
+ asset: AssetFactory.createFileStylesheet({
35
+ filepath,
36
+ position: 'head',
37
+ attributes: { rel: 'stylesheet', ...attributes },
38
+ }),
39
+ };
10
40
  }
11
- function getDependencyEntryAttributes(entry) {
12
- return isDependencyEntryObject(entry) ? entry.attributes : undefined;
41
+ function declareScript(script, componentDir, invalidEntry) {
42
+ const { src, content, attributes } = readEntry(script);
43
+ if (content) {
44
+ return {
45
+ kind: 'script',
46
+ key: `script:content:${content}:${JSON.stringify(attributes ?? {})}`,
47
+ asset: AssetFactory.createInlineContentScript({ position: 'head', content, bundle: false, attributes }),
48
+ };
49
+ }
50
+ if (!src)
51
+ throw new Error(invalidEntry);
52
+ const filepath = resolveDeclaredPath(componentDir, src);
53
+ return {
54
+ kind: 'script',
55
+ key: `script:file:${filepath}:${JSON.stringify(attributes ?? {})}`,
56
+ asset: AssetFactory.createFileScript({
57
+ filepath,
58
+ position: 'head',
59
+ attributes: createModuleScriptAttributes(attributes),
60
+ }),
61
+ };
13
62
  }
14
63
  /**
15
- * Normalizes non-lazy stylesheet and script declarations into asset definitions.
64
+ * Normalizes a component's stylesheet and non-lazy script declarations into asset definitions, stylesheets first.
16
65
  *
17
- * Duplicate declarations are filtered through the provided key sets so callers can
18
- * compose this helper into larger recursive collection passes without re-emitting
19
- * the same asset multiple times.
66
+ * @remarks
67
+ * Each carries the key collection dedupes it by, so the same declaration from two components is emitted once.
20
68
  */
21
- export function collectDeclaredAssetEntries(options) {
22
- const { stylesheetEntries, scriptEntries, componentDir, dependencies, stylesheetDependencyKeys, scriptDependencyKeys, resolveDependencyPath, createModuleScriptAttributes, pushUniqueDependency, getInvalidStylesheetEntryMessage, getInvalidScriptEntryMessage, } = options;
23
- for (const style of stylesheetEntries) {
24
- const content = getDependencyEntryContent(style);
25
- const src = getDependencyEntrySrc(style);
26
- const attributes = getDependencyEntryAttributes(style);
27
- if (content) {
28
- const depKey = `style:content:${content}:${JSON.stringify(attributes ?? {})}`;
29
- pushUniqueDependency(stylesheetDependencyKeys, depKey, dependencies, AssetFactory.createContentStylesheet({
30
- content,
31
- position: 'head',
32
- attributes,
33
- }));
34
- continue;
35
- }
36
- if (!src) {
37
- throw new Error(getInvalidStylesheetEntryMessage());
38
- }
39
- const resolvedPath = resolveDependencyPath(componentDir, src);
40
- const depKey = `style:file:${resolvedPath}:${JSON.stringify(attributes ?? {})}`;
41
- pushUniqueDependency(stylesheetDependencyKeys, depKey, dependencies, AssetFactory.createFileStylesheet({
42
- filepath: resolvedPath,
43
- position: 'head',
44
- attributes: {
45
- rel: 'stylesheet',
46
- ...attributes,
47
- },
48
- }));
49
- }
50
- for (const script of scriptEntries) {
51
- if (isDependencyEntryObject(script) && 'lazy' in script && script.lazy) {
52
- continue;
53
- }
54
- const content = getDependencyEntryContent(script);
55
- const src = getDependencyEntrySrc(script);
56
- const attributes = getDependencyEntryAttributes(script);
57
- if (content) {
58
- const depKey = `script:content:${content}:${JSON.stringify(attributes ?? {})}`;
59
- pushUniqueDependency(scriptDependencyKeys, depKey, dependencies, AssetFactory.createInlineContentScript({
60
- position: 'head',
61
- content,
62
- bundle: false,
63
- attributes,
64
- }));
65
- continue;
66
- }
67
- if (!src) {
68
- throw new Error(getInvalidScriptEntryMessage());
69
- }
70
- const resolvedPath = resolveDependencyPath(componentDir, src);
71
- const depKey = `script:file:${resolvedPath}:${JSON.stringify(attributes ?? {})}`;
72
- pushUniqueDependency(scriptDependencyKeys, depKey, dependencies, AssetFactory.createFileScript({
73
- filepath: resolvedPath,
74
- position: 'head',
75
- attributes: createModuleScriptAttributes(attributes),
76
- }));
77
- }
69
+ export function declareAssetEntries(options) {
70
+ const { componentDir, errors } = options;
71
+ return [
72
+ ...options.stylesheetEntries.map((style) => declareStylesheet(style, componentDir, errors.invalidStylesheetEntry)),
73
+ ...options.scriptEntries
74
+ .filter((script) => !(isDependencyEntryObject(script) && 'lazy' in script && script.lazy))
75
+ .map((script) => declareScript(script, componentDir, errors.invalidScriptEntry)),
76
+ ];
78
77
  }