@lwrjs/view-registry 0.10.0-alpha.13 → 0.10.0-alpha.15

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.
@@ -30,6 +30,7 @@ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
30
30
  var import_identity = __toModule(require("@lwrjs/app-service/identity"));
31
31
  var import_utils = __toModule(require("../utils.cjs"));
32
32
  var import_utils2 = __toModule(require("./utils.cjs"));
33
+ var import_preload_utils = __toModule(require("./preload-utils.cjs"));
33
34
  async function getHtmlResources(view, viewParams, resourceContext) {
34
35
  const {
35
36
  runtimeEnvironment,
@@ -39,8 +40,15 @@ async function getHtmlResources(view, viewParams, resourceContext) {
39
40
  resourceRegistry,
40
41
  viewMetadata
41
42
  } = resourceContext;
43
+ const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
44
+ const {customElements} = viewMetadata;
45
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
46
+ const version = lwrVersion;
47
+ const isAMD = format === "amd";
48
+ const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 1};
42
49
  const {bundleConfig} = resourceContext;
43
50
  const {external = {}} = bundleConfig;
51
+ const groups = isAMD ? bundleConfig.groups || {} : {};
44
52
  const isExternal = function(rawSpecifier) {
45
53
  const {specifier} = (0, import_shared_utils.explodeSpecifier)(rawSpecifier);
46
54
  return Object.keys(external).some((e) => specifier === e);
@@ -52,11 +60,6 @@ async function getHtmlResources(view, viewParams, resourceContext) {
52
60
  preloadModules: []
53
61
  }
54
62
  } = view;
55
- const {lwrVersion, format, hmrEnabled, bundle, debug, minify} = runtimeEnvironment;
56
- const {customElements} = viewMetadata;
57
- const defRegistry = bundle ? moduleBundler : moduleRegistry;
58
- const version = lwrVersion;
59
- const isAMD = format === "amd";
60
63
  const appIdentity = {
61
64
  appName,
62
65
  format: runtimeEnvironment.format,
@@ -70,105 +73,123 @@ async function getHtmlResources(view, viewParams, resourceContext) {
70
73
  const imports = {};
71
74
  const rootComponents = [];
72
75
  const requiredAmdModules = [];
73
- const preloadModulesMeta = new Map();
76
+ const viewPreloads = {
77
+ uris: [],
78
+ specifiers: [],
79
+ groups: new Map()
80
+ };
74
81
  const isSSR = view.bootstrap?.ssr;
75
- if (isAMD) {
76
- const shimBundle = debug || minify === false ? "lwr-loader-shim.bundle.js" : "lwr-loader-shim.bundle.min.js";
77
- const def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
78
- if (!def) {
79
- throw Error("Failed to find definition of resource: " + shimBundle);
80
- }
81
- requiredResources.push(def);
82
- const errorShimDef = await resourceRegistry.getResource({specifier: "lwr-error-shim.js", version}, runtimeEnvironment, runtimeParams);
83
- if (!errorShimDef) {
84
- throw Error("Failed to find definition of resource: lwr-error-shim.js");
85
- }
86
- requiredResources.push(errorShimDef);
87
- }
88
- const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 1};
89
- const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
90
- const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
91
- const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
92
- moduleResources.push((0, import_utils.getModuleResourceByUri)(uri, runtimeEnvironment, {isPreload: false, isSSR}));
93
- for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
94
- if (!isExternal(depSpecifier)) {
95
- const uri2 = bootstrapModuleGraph.uriMap[depSpecifier];
96
- preloadModulesMeta.set(depSpecifier, uri2);
82
+ let bootstrapModuleRef, versionedSpecifier = bootstrapSpecifier;
83
+ let importMetadata = {imports: {}};
84
+ const customElementsRecords = [];
85
+ const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
86
+ const viewContainsLiveElements = !isSSR || flattenedElements.some(({props}) => (0, import_shared_utils.getHydrateDirective)(props));
87
+ if (viewContainsLiveElements) {
88
+ if (isAMD) {
89
+ const shimBundle = debug || minify === false ? "lwr-loader-shim.bundle.js" : "lwr-loader-shim.bundle.min.js";
90
+ const def = await resourceRegistry.getResource({specifier: shimBundle, version}, runtimeEnvironment, runtimeParams);
91
+ if (!def) {
92
+ throw Error("Failed to find definition of resource: " + shimBundle);
93
+ }
94
+ requiredResources.push(def);
95
+ const errorShimDef = await resourceRegistry.getResource({specifier: "lwr-error-shim.js", version}, runtimeEnvironment, runtimeParams);
96
+ if (!errorShimDef) {
97
+ throw Error("Failed to find definition of resource: lwr-error-shim.js");
98
+ }
99
+ requiredResources.push(errorShimDef);
97
100
  }
98
- }
99
- if ((0, import_shared_utils.isBundler)(defRegistry)) {
100
- for (const specifier of preloadModules) {
101
- await (0, import_utils2.getPreloadModulesMeta)(specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
101
+ const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
102
+ bootstrapModuleRef = {
103
+ specifier: bootstrapModuleGraph.graphs[0].specifier,
104
+ flatGraph: bootstrapModuleGraph,
105
+ resources: configResources
106
+ };
107
+ versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
108
+ const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
109
+ moduleResources.push((0, import_utils.getModuleResourceByUri)(uri, runtimeEnvironment, {isPreload: false, isSSR}));
110
+ for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
111
+ if (!isExternal(depSpecifier)) {
112
+ const uri2 = bootstrapModuleGraph.uriMap[depSpecifier];
113
+ (0, import_preload_utils.setPreloadModulesMeta)(depSpecifier, uri2, groups, viewPreloads);
114
+ }
102
115
  }
103
- }
104
- if (isAMD) {
105
- requiredAmdModules.push(versionedSpecifier);
106
- imports[versionedSpecifier] = uri;
107
- for (const staticDep of bootstrapModuleGraph.graphs[0].static) {
108
- const uri2 = bootstrapModuleGraph.uriMap[staticDep];
109
- imports[staticDep] = uri2;
110
- if (services && services.length) {
111
- requiredAmdModules.push(staticDep);
116
+ if ((0, import_shared_utils.isBundler)(defRegistry)) {
117
+ for (const specifier of preloadModules) {
118
+ await (0, import_preload_utils.getPreloadModulesMeta)(specifier, viewPreloads, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
112
119
  }
113
120
  }
114
- for (const dynamicDep of bootstrapModuleGraph.graphs[0].dynamicRefs) {
115
- const uri2 = bootstrapModuleGraph.uriMap[dynamicDep];
116
- if (uri2) {
117
- imports[dynamicDep] = uri2;
118
- } else {
119
- import_shared_utils.logger.warn("Skipping unknown dynamic import " + dynamicDep);
121
+ if (isAMD) {
122
+ requiredAmdModules.push(versionedSpecifier);
123
+ imports[versionedSpecifier] = uri;
124
+ for (const staticDep of bootstrapModuleGraph.graphs[0].static) {
125
+ const uri2 = bootstrapModuleGraph.uriMap[staticDep];
126
+ imports[staticDep] = uri2;
127
+ if (services && services.length) {
128
+ requiredAmdModules.push(staticDep);
129
+ }
130
+ }
131
+ for (const dynamicDep of bootstrapModuleGraph.graphs[0].dynamicRefs) {
132
+ const uri2 = bootstrapModuleGraph.uriMap[dynamicDep];
133
+ if (uri2) {
134
+ imports[dynamicDep] = uri2;
135
+ } else {
136
+ import_shared_utils.logger.warn("Skipping unknown dynamic import " + dynamicDep);
137
+ }
120
138
  }
121
139
  }
140
+ importMetadata = await (0, import_shared_utils.toImportMetadata)(bootstrapModuleGraph, {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams);
122
141
  }
123
- let importMetadata = await (0, import_shared_utils.toImportMetadata)(bootstrapModuleGraph, {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams);
124
- const customElementsRecords = [];
125
- const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements, isSSR);
126
- for (const {tagName: element} of flattenedElements) {
142
+ for (const {tagName: element, props} of flattenedElements) {
127
143
  const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabCaseToModuleSpecifier)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
128
144
  customElementsRecords.push({elementName: element, flatGraph: graph});
129
- const specifier = graph.graphs[0].specifier;
130
- const uri2 = graph.uriMap[specifier];
131
- preloadModulesMeta.set(specifier, uri2);
132
- if (bundle) {
133
- for (const depSpecifier of graph.graphs[0].static) {
134
- if (!isExternal(depSpecifier)) {
135
- const uri3 = graph.uriMap[depSpecifier];
136
- preloadModulesMeta.set(depSpecifier, uri3);
145
+ if (!isSSR || (0, import_shared_utils.getHydrateDirective)(props)) {
146
+ const specifier = graph.graphs[0].specifier;
147
+ const uri = graph.uriMap[specifier];
148
+ (0, import_preload_utils.setPreloadModulesMeta)(specifier, uri, groups, viewPreloads);
149
+ if (bundle) {
150
+ for (const depSpecifier of graph.graphs[0].static) {
151
+ if (!isExternal(depSpecifier)) {
152
+ const uri2 = graph.uriMap[depSpecifier];
153
+ (0, import_preload_utils.setPreloadModulesMeta)(depSpecifier, uri2, groups, viewPreloads);
154
+ }
137
155
  }
138
156
  }
139
- }
140
- rootComponents.push(specifier);
141
- imports[specifier] = uri2;
142
- if (isAMD) {
143
- for (const staticDep of graph.graphs[0].static) {
144
- const uri3 = graph.uriMap[staticDep];
145
- imports[staticDep] = uri3;
146
- }
147
- for (const dynamicDep of graph.graphs[0].dynamicRefs) {
148
- const uri3 = graph.uriMap[dynamicDep];
149
- imports[dynamicDep] = uri3;
157
+ rootComponents.push(specifier);
158
+ imports[specifier] = uri;
159
+ if (isAMD) {
160
+ for (const staticDep of graph.graphs[0].static) {
161
+ const uri2 = graph.uriMap[staticDep];
162
+ imports[staticDep] = uri2;
163
+ }
164
+ for (const dynamicDep of graph.graphs[0].dynamicRefs) {
165
+ const uri2 = graph.uriMap[dynamicDep];
166
+ imports[dynamicDep] = uri2;
167
+ }
150
168
  }
169
+ importMetadata = await (0, import_shared_utils.toImportMetadata)(graph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
151
170
  }
152
- importMetadata = await (0, import_shared_utils.toImportMetadata)(graph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
153
171
  }
154
- configResources.unshift(await (0, import_utils2.getViewBootstrapConfigurationResource)({
155
- id: view.id,
156
- url: viewParams?.page?.url,
157
- configAsSrc: view.bootstrap?.configAsSrc || false
158
- }, {
159
- appId: appIdentity.appName,
160
- bootstrapModule: versionedSpecifier,
161
- autoBoot: view.bootstrap?.autoBoot === false ? false : true,
162
- imports: importMetadata?.imports,
163
- index: importMetadata?.index,
164
- rootComponents,
165
- ...isAMD && {requiredModules: requiredAmdModules},
166
- ...isAMD && {preloadModules: Array.from(preloadModulesMeta.keys())}
167
- }, runtimeEnvironment, runtimeParams));
172
+ if (viewContainsLiveElements) {
173
+ configResources.unshift((0, import_utils2.getViewBootstrapConfigurationResource)({
174
+ id: view.id,
175
+ url: viewParams?.page?.url,
176
+ configAsSrc: view.bootstrap?.configAsSrc || false
177
+ }, {
178
+ appId: appIdentity.appName,
179
+ bootstrapModule: versionedSpecifier,
180
+ autoBoot: view.bootstrap?.autoBoot === false ? false : true,
181
+ imports: importMetadata?.imports,
182
+ index: importMetadata?.index,
183
+ rootComponents,
184
+ ...isAMD && {requiredModules: requiredAmdModules},
185
+ ...isAMD && {preloadModules: viewPreloads.specifiers}
186
+ }, runtimeEnvironment, runtimeParams));
187
+ }
168
188
  if (!isAMD && hmrEnabled) {
169
- configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
189
+ configResources.unshift((0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
170
190
  }
171
- for (const [, preloadUri] of preloadModulesMeta) {
191
+ const dedupedUris = [...new Set(viewPreloads.uris)];
192
+ for (const preloadUri of dedupedUris) {
172
193
  moduleResources.push((0, import_utils.getModuleResourceByUri)(preloadUri, runtimeEnvironment, {isPreload: true, isSSR}));
173
194
  }
174
195
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
@@ -185,11 +206,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
185
206
  customElements: customElementsRecords,
186
207
  endpoints,
187
208
  importMetadata,
188
- bootstrapModule: {
189
- specifier: bootstrapModuleGraph.graphs[0].specifier,
190
- flatGraph: bootstrapModuleGraph,
191
- resources: configResources
192
- }
209
+ bootstrapModule: bootstrapModuleRef
193
210
  }
194
211
  };
195
212
  }
@@ -1,11 +1,18 @@
1
- import { kebabCaseToModuleSpecifier, getModuleGraphs, GraphDepth, getModuleUriPrefix, logger, explodeSpecifier, isBundler, } from '@lwrjs/shared-utils';
1
+ import { kebabCaseToModuleSpecifier, getModuleGraphs, GraphDepth, getModuleUriPrefix, logger, explodeSpecifier, isBundler, getHydrateDirective, } from '@lwrjs/shared-utils';
2
2
  import { AppResourceEnum, getAppSpecifier } from '@lwrjs/app-service/identity';
3
3
  import { generateHtmlTag, getModuleResourceByUri } from '../utils.js';
4
- import { flattenCustomElements, getViewBootstrapConfigurationResource, getViewHmrConfigurationResource, getPreloadModulesMeta, } from './utils.js';
4
+ import { flattenCustomElements, getViewBootstrapConfigurationResource, getViewHmrConfigurationResource, } from './utils.js';
5
+ import { setPreloadModulesMeta, getPreloadModulesMeta } from './preload-utils.js';
5
6
  export async function getHtmlResources(view, viewParams, resourceContext) {
6
7
  const { runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler, resourceRegistry, viewMetadata, } = resourceContext;
8
+ const { lwrVersion, format, hmrEnabled, bundle, debug, minify } = runtimeEnvironment;
9
+ const { customElements } = viewMetadata;
10
+ const version = lwrVersion;
11
+ const isAMD = format === 'amd';
7
12
  const { bundleConfig } = resourceContext;
8
13
  const { external = {} } = bundleConfig;
14
+ // Bundling groups is only supported in AMD for now
15
+ const groups = isAMD ? bundleConfig.groups || {} : {};
9
16
  const isExternal = function (rawSpecifier) {
10
17
  const { specifier } = explodeSpecifier(rawSpecifier);
11
18
  return Object.keys(external).some((e) => specifier === e);
@@ -14,10 +21,10 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
14
21
  services: [],
15
22
  preloadModules: [],
16
23
  }, } = view;
17
- const { lwrVersion, format, hmrEnabled, bundle, debug, minify } = runtimeEnvironment;
18
- const { customElements } = viewMetadata;
19
- const version = lwrVersion;
20
- const isAMD = format === 'amd';
24
+ const defRegistry = bundle ? moduleBundler : moduleRegistry;
25
+ const depth = isAMD
26
+ ? { static: GraphDepth.ALL, dynamic: 1 }
27
+ : { static: GraphDepth.NONE, dynamic: 0 };
21
28
  // The Application Bootstrap (ABS) module resource is EITHER
22
29
  // - configured as routes[x].bootstrap.module OR
23
30
  // - defaulted as "@lwrjs/app-service/{appName}/module/{format}"
@@ -48,155 +55,170 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
48
55
  const rootComponents = [];
49
56
  // Collection of modules specifiers that MUST be loaded in the view
50
57
  const requiredAmdModules = [];
51
- // Collection of modules that will be preloaded. For AMD, it will be part of
52
- // the preloadModules client bootstrap config
53
- const preloadModulesMeta = new Map();
58
+ // Collect preload modules metadata
59
+ const viewPreloads = {
60
+ uris: [],
61
+ specifiers: [],
62
+ groups: new Map(),
63
+ };
54
64
  // Determine if server side rendering view modules
55
65
  const isSSR = view.bootstrap?.ssr;
56
- // ------ AMD Required module resources
57
- if (isAMD) {
58
- // Keep shim format in sync with view_bootstrap.ts
59
- const shimBundle = debug || minify === false
60
- ? 'lwr-loader-shim-legacy.bundle.js'
61
- : 'lwr-loader-shim-legacy.bundle.min.js';
62
- let def = (await resourceRegistry.getResource({ specifier: shimBundle, version }, runtimeEnvironment, runtimeParams));
63
- if (!def) {
64
- // HACK: fallback to looking for the other shim.
65
- // TODO: remove this once we solve debug mode in MRT
66
- let fallbackShimBundle;
67
- if (shimBundle === 'lwr-loader-shim-legacy.bundle.js') {
68
- fallbackShimBundle = 'lwr-loader-shim-legacy.bundle.min.js';
69
- def = (await resourceRegistry.getResource({ specifier: fallbackShimBundle, version }, runtimeEnvironment, runtimeParams));
70
- }
71
- else {
72
- fallbackShimBundle = 'lwr-loader-shim-legacy.bundle.js';
73
- def = (await resourceRegistry.getResource({ specifier: fallbackShimBundle, version }, runtimeEnvironment, runtimeParams));
74
- }
75
- // throw if we still can't find it
66
+ // ABS module and custom element references
67
+ let bootstrapModuleRef, versionedSpecifier = bootstrapSpecifier;
68
+ const customElementsRecords = [];
69
+ const flattenedElements = flattenCustomElements(customElements, isSSR);
70
+ const viewContainsLiveElements = !isSSR || flattenedElements.some(({ props }) => getHydrateDirective(props));
71
+ if (viewContainsLiveElements) {
72
+ // ------ AMD Required module resources
73
+ if (isAMD) {
74
+ // Keep shim format in sync with view_bootstrap.ts
75
+ const shimBundle = debug || minify === false
76
+ ? 'lwr-loader-shim-legacy.bundle.js'
77
+ : 'lwr-loader-shim-legacy.bundle.min.js';
78
+ let def = (await resourceRegistry.getResource({ specifier: shimBundle, version }, runtimeEnvironment, runtimeParams));
76
79
  if (!def) {
77
- throw Error('Failed to find definition of resource: ' + shimBundle);
80
+ // HACK: fallback to looking for the other shim.
81
+ // TODO: remove this once we solve debug mode in MRT
82
+ let fallbackShimBundle;
83
+ if (shimBundle === 'lwr-loader-shim-legacy.bundle.js') {
84
+ fallbackShimBundle = 'lwr-loader-shim-legacy.bundle.min.js';
85
+ def = (await resourceRegistry.getResource({ specifier: fallbackShimBundle, version }, runtimeEnvironment, runtimeParams));
86
+ }
87
+ else {
88
+ fallbackShimBundle = 'lwr-loader-shim-legacy.bundle.js';
89
+ def = (await resourceRegistry.getResource({ specifier: fallbackShimBundle, version }, runtimeEnvironment, runtimeParams));
90
+ }
91
+ // throw if we still can't find it
92
+ if (!def) {
93
+ throw Error('Failed to find definition of resource: ' + shimBundle);
94
+ }
78
95
  }
96
+ requiredResources.push(def);
97
+ // Always inline the error shim script after the shim
98
+ const errorShimDef = (await resourceRegistry.getResource({ specifier: 'lwr-error-shim.js', version }, runtimeEnvironment, runtimeParams));
99
+ if (!errorShimDef) {
100
+ throw Error('Failed to find definition of resource: lwr-error-shim.js');
101
+ }
102
+ requiredResources.push(errorShimDef);
79
103
  }
80
- requiredResources.push(def);
81
- // Always inline the error shim script after the shim
82
- const errorShimDef = (await resourceRegistry.getResource({ specifier: 'lwr-error-shim.js', version }, runtimeEnvironment, runtimeParams));
83
- if (!errorShimDef) {
84
- throw Error('Failed to find definition of resource: lwr-error-shim.js');
85
- }
86
- requiredResources.push(errorShimDef);
87
- }
88
- // ------- Application Bootstrap module
89
- // Traversal of the Bootstrap Module Graph is done to get all the URLS for discoverable static dependencies.
90
- // Reasoning: This is to avoid unnecessary HTTP 302's during initial application module fetching.
91
- // Scope: ESM currently only exposes immutable URI references, optimize for AMD formats
92
- const depth = isAMD
93
- ? { static: GraphDepth.ALL, dynamic: 1 }
94
- : { static: GraphDepth.NONE, dynamic: 0 };
95
- const defRegistry = bundle ? moduleBundler : moduleRegistry;
96
- const bootstrapModuleGraph = await getModuleGraphs(bootstrapSpecifier, { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
97
- // ADD bootstrap module uri as a script resource
98
- const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
99
- const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
100
- moduleResources.push(getModuleResourceByUri(uri, runtimeEnvironment, { isPreload: false, isSSR }));
101
- // PRELOAD the bootstrap module static dependencies as preloaded script resources
102
- for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
103
- if (!isExternal(depSpecifier)) {
104
- const uri = bootstrapModuleGraph.uriMap[depSpecifier];
105
- preloadModulesMeta.set(depSpecifier, uri);
106
- }
107
- }
108
- // PRELOAD configured preloadModules as preloaded script resources
109
- if (isBundler(defRegistry)) {
110
- for (const specifier of preloadModules) {
111
- // eslint-disable-next-line no-await-in-loop
112
- await getPreloadModulesMeta(specifier, preloadModulesMeta, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
104
+ // ------- Application Bootstrap module
105
+ // Traversal of the Bootstrap Module Graph is done to get all the URLS for discoverable static dependencies.
106
+ // Reasoning: This is to avoid unnecessary HTTP 302's during initial application module fetching.
107
+ // Scope: ESM currently only exposes immutable URI references, optimize for AMD formats
108
+ const bootstrapModuleGraph = await getModuleGraphs(bootstrapSpecifier, { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visitedCache);
109
+ bootstrapModuleRef = {
110
+ specifier: bootstrapModuleGraph.graphs[0].specifier,
111
+ flatGraph: bootstrapModuleGraph,
112
+ resources: configResources,
113
+ };
114
+ // ADD bootstrap module uri as a script resource
115
+ versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
116
+ const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
117
+ moduleResources.push(getModuleResourceByUri(uri, runtimeEnvironment, { isPreload: false, isSSR }));
118
+ // PRELOAD the bootstrap module static dependencies as preloaded script resources
119
+ for (const depSpecifier of bootstrapModuleGraph.graphs[0].static) {
120
+ if (!isExternal(depSpecifier)) {
121
+ const uri = bootstrapModuleGraph.uriMap[depSpecifier];
122
+ setPreloadModulesMeta(depSpecifier, uri, groups, viewPreloads);
123
+ }
113
124
  }
114
- }
115
- if (isAMD) {
116
- // ADD bootstrap module as required
117
- requiredAmdModules.push(versionedSpecifier);
118
- // AMD ADD bootstrap module uri addressability
119
- imports[versionedSpecifier] = uri;
120
- // PRELOAD bootstrap module static deps as link resource
121
- for (const staticDep of bootstrapModuleGraph.graphs[0].static) {
122
- const uri = bootstrapModuleGraph.uriMap[staticDep];
123
- // AMD ADD static module dep uri addressability
124
- imports[staticDep] = uri;
125
- // ADD bootstrap module static deps to requiredAmdModules if services, otherwise preloadModules
126
- if (services && services.length) {
127
- requiredAmdModules.push(staticDep);
125
+ // PRELOAD configured preloadModules as preloaded script resources
126
+ if (isBundler(defRegistry)) {
127
+ for (const specifier of preloadModules) {
128
+ // eslint-disable-next-line no-await-in-loop
129
+ await getPreloadModulesMeta(specifier, viewPreloads, bundleConfig, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams);
128
130
  }
129
131
  }
130
- // AMD Add import mappings for known dynamic imports
131
- for (const dynamicDep of bootstrapModuleGraph.graphs[0].dynamicRefs) {
132
- const uri = bootstrapModuleGraph.uriMap[dynamicDep];
133
- if (uri) {
134
- imports[dynamicDep] = uri;
132
+ if (isAMD) {
133
+ // ADD bootstrap module as required
134
+ requiredAmdModules.push(versionedSpecifier);
135
+ // AMD ADD bootstrap module uri addressability
136
+ imports[versionedSpecifier] = uri;
137
+ // PRELOAD bootstrap module static deps as link resource
138
+ for (const staticDep of bootstrapModuleGraph.graphs[0].static) {
139
+ const uri = bootstrapModuleGraph.uriMap[staticDep];
140
+ // AMD ADD static module dep uri addressability
141
+ imports[staticDep] = uri;
142
+ // ADD bootstrap module static deps to requiredAmdModules if services, otherwise preloadModules
143
+ if (services && services.length) {
144
+ requiredAmdModules.push(staticDep);
145
+ }
135
146
  }
136
- else {
137
- logger.warn('Skipping unknown dynamic import ' + dynamicDep);
147
+ // AMD Add import mappings for known dynamic imports
148
+ for (const dynamicDep of bootstrapModuleGraph.graphs[0].dynamicRefs) {
149
+ const uri = bootstrapModuleGraph.uriMap[dynamicDep];
150
+ if (uri) {
151
+ imports[dynamicDep] = uri;
152
+ }
153
+ else {
154
+ logger.warn('Skipping unknown dynamic import ' + dynamicDep);
155
+ }
138
156
  }
139
157
  }
140
158
  }
141
159
  // ------- View related custom element moduleResources
142
- const customElementsRecords = [];
143
- const flattenedElements = flattenCustomElements(customElements, isSSR);
144
160
  await Promise.all(flattenedElements.map(async ({ tagName: element, props }) => {
145
161
  const graph = await getModuleGraphs(kebabCaseToModuleSpecifier(element), { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
146
162
  // add to the viewRecord
147
163
  customElementsRecords.push({ elementName: element, flatGraph: graph });
148
- // PRELOAD the custom element module as a link resource
149
- const specifier = graph.graphs[0].specifier;
150
- const uri = graph.uriMap[specifier];
151
- preloadModulesMeta.set(specifier, uri);
152
- // PRELOAD custom element static deps as link resources when bundling is ON
153
- if (bundle) {
154
- for (const depSpecifier of graph.graphs[0].static) {
155
- if (!isExternal(depSpecifier)) {
156
- const uri = graph.uriMap[depSpecifier];
157
- preloadModulesMeta.set(depSpecifier, uri);
164
+ // Only process custom elements that are to be hydrated or are CSR-only islands
165
+ if (!isSSR || getHydrateDirective(props)) {
166
+ // PRELOAD the custom element module as a link resource
167
+ const specifier = graph.graphs[0].specifier;
168
+ const uri = graph.uriMap[specifier];
169
+ setPreloadModulesMeta(specifier, uri, groups, viewPreloads);
170
+ // PRELOAD custom element static deps as link resources when bundling is ON
171
+ if (bundle) {
172
+ for (const depSpecifier of graph.graphs[0].static) {
173
+ if (!isExternal(depSpecifier)) {
174
+ const uri = graph.uriMap[depSpecifier];
175
+ setPreloadModulesMeta(depSpecifier, uri, groups, viewPreloads);
176
+ }
158
177
  }
159
178
  }
160
- }
161
- // ADD register custom elements as a uri addressable rootComponents
162
- rootComponents.push(specifier);
163
- imports[specifier] = uri;
164
- if (isAMD) {
165
- for (const staticDep of graph.graphs[0].static) {
166
- const uri = graph.uriMap[staticDep];
167
- // AMD ADD static module dep uri addressability
168
- imports[staticDep] = uri;
169
- }
170
- // AMD ADD dynamic imports module dep uri addressability
171
- for (const dynamicDep of graph.graphs[0].dynamicRefs) {
172
- const uri = graph.uriMap[dynamicDep];
173
- imports[dynamicDep] = uri;
179
+ // ADD register custom elements as a uri addressable rootComponents
180
+ rootComponents.push(specifier);
181
+ imports[specifier] = uri;
182
+ if (isAMD) {
183
+ for (const staticDep of graph.graphs[0].static) {
184
+ const uri = graph.uriMap[staticDep];
185
+ // AMD ADD static module dep uri addressability
186
+ imports[staticDep] = uri;
187
+ }
188
+ // AMD ADD dynamic imports module dep uri addressability
189
+ for (const dynamicDep of graph.graphs[0].dynamicRefs) {
190
+ const uri = graph.uriMap[dynamicDep];
191
+ imports[dynamicDep] = uri;
192
+ }
174
193
  }
175
194
  }
176
195
  }));
177
- // ADD configuration of the bootstrapModule
178
- configResources.unshift(await getViewBootstrapConfigurationResource({
179
- id: view.id,
180
- url: viewParams?.page?.url,
181
- configAsSrc: view.bootstrap?.configAsSrc || false,
182
- }, {
183
- appId: appIdentity.appName,
184
- bootstrapModule: versionedSpecifier,
185
- autoBoot: view.bootstrap?.autoBoot === false ? false : true,
186
- importMappings: {
187
- imports,
188
- default: getModuleUriPrefix(runtimeEnvironment, runtimeParams),
189
- },
190
- rootComponents,
191
- ...(isAMD && { requiredModules: requiredAmdModules }),
192
- // in AMD we need to tell the loader what modules we are preloading
193
- ...(isAMD && { preloadModules: Array.from(preloadModulesMeta.keys()) }),
194
- }, runtimeEnvironment, runtimeParams));
196
+ if (viewContainsLiveElements) {
197
+ // ADD configuration of the bootstrapModule
198
+ configResources.unshift(getViewBootstrapConfigurationResource({
199
+ id: view.id,
200
+ url: viewParams?.page?.url,
201
+ configAsSrc: view.bootstrap?.configAsSrc || false,
202
+ }, {
203
+ appId: appIdentity.appName,
204
+ bootstrapModule: versionedSpecifier,
205
+ autoBoot: view.bootstrap?.autoBoot === false ? false : true,
206
+ importMappings: {
207
+ imports,
208
+ default: getModuleUriPrefix(runtimeEnvironment, runtimeParams),
209
+ },
210
+ rootComponents,
211
+ ...(isAMD && { requiredModules: requiredAmdModules }),
212
+ // in AMD we need to tell the loader what modules we are preloading
213
+ ...(isAMD && { preloadModules: viewPreloads.specifiers }),
214
+ }, runtimeEnvironment, runtimeParams));
215
+ }
195
216
  if (!isAMD && hmrEnabled) {
196
- configResources.unshift(await getViewHmrConfigurationResource(view, viewMetadata));
217
+ configResources.unshift(getViewHmrConfigurationResource(view, viewMetadata));
197
218
  }
219
+ const dedupedUris = [...new Set(viewPreloads.uris)];
198
220
  // PRELOAD script resources for preload module URIs after the bootstrap module
199
- for (const [, preloadUri] of preloadModulesMeta) {
221
+ for (const preloadUri of dedupedUris) {
200
222
  moduleResources.push(getModuleResourceByUri(preloadUri, runtimeEnvironment, { isPreload: true, isSSR }));
201
223
  }
202
224
  // generate html partial
@@ -206,11 +228,7 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
206
228
  viewRecord: {
207
229
  resources: requiredResources,
208
230
  customElements: customElementsRecords,
209
- bootstrapModule: {
210
- specifier: bootstrapModuleGraph.graphs[0].specifier,
211
- flatGraph: bootstrapModuleGraph,
212
- resources: configResources,
213
- },
231
+ bootstrapModule: bootstrapModuleRef,
214
232
  },
215
233
  };
216
234
  }
@@ -0,0 +1,17 @@
1
+ import { RuntimeEnvironment, RuntimeParams, BundleGroups, BundleConfig, ModuleBundler, ModuleRegistry } from '@lwrjs/types';
2
+ export declare type ViewPreloads = {
3
+ groups: Map<string, boolean>;
4
+ uris: string[];
5
+ specifiers: string[];
6
+ };
7
+ /**
8
+ * keeps track of preloadModules metadata
9
+ */
10
+ export declare function setPreloadModulesMeta(specifier: string, uri: string, groups: BundleGroups, preloads: ViewPreloads): void;
11
+ /**
12
+ * Recursively gets preloadModules metadata starting with a specifer
13
+ * Note: don't call me unless you got bundles
14
+ */
15
+ export declare function getPreloadModulesMeta(specifier: string, // unversioned specifier
16
+ viewPreloads: ViewPreloads, bundleConfig: BundleConfig, moduleRegistry: ModuleRegistry, defRegistry: ModuleBundler, runtimeEnvironment: RuntimeEnvironment, runtimeParams?: RuntimeParams, pending?: Map<string, boolean>): Promise<void>;
17
+ //# sourceMappingURL=preload-utils.d.ts.map