@lwrjs/view-registry 0.5.11 → 0.6.0-0.6.0-alpha.15.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.
@@ -29,7 +29,6 @@ __export(exports, {
29
29
  });
30
30
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
31
  var import_utils = __toModule(require("./utils.cjs"));
32
- var import_link_assets = __toModule(require("./linkers/link-assets.cjs"));
33
32
  var import_link_lwr_resources = __toModule(require("./linkers/link-lwr-resources.cjs"));
34
33
  var import_view_handler = __toModule(require("./view-handler.cjs"));
35
34
  var LwrViewRegistry = class {
@@ -45,6 +44,7 @@ var LwrViewRegistry = class {
45
44
  this.moduleRegistry = context.moduleRegistry;
46
45
  this.moduleBundler = context.moduleBundler;
47
46
  this.assetRegistry = context.assetRegistry;
47
+ this.viewTransformers = context.viewTransformers || [];
48
48
  this.globalConfig = globalConfig;
49
49
  this.globalData = context.globalData;
50
50
  this.appEmitter = context.appEmitter;
@@ -106,6 +106,9 @@ var LwrViewRegistry = class {
106
106
  addViewProviders(providers) {
107
107
  this.viewProviders.push(...providers);
108
108
  }
109
+ addViewTransformers(transformers) {
110
+ this.viewTransformers.push(...transformers);
111
+ }
109
112
  initializeViewProviders() {
110
113
  return Promise.all(this.viewProviders.map((vp) => vp.initialize()));
111
114
  }
@@ -272,20 +275,24 @@ var LwrViewRegistry = class {
272
275
  } = viewContext;
273
276
  const {skipMetadataCollection, freezeAssets} = renderOptions;
274
277
  const {lwrResourcesId} = contentIds;
275
- const {moduleRegistry, assetRegistry, resourceRegistry, moduleBundler} = this;
278
+ const {moduleRegistry, resourceRegistry, moduleBundler} = this;
276
279
  const runtimeEnvironment = {...runtimeEnv, immutableAssets: freezeAssets};
277
280
  const {
278
281
  renderedView: renderedViewContent,
279
282
  metadata: renderedViewMetadata,
280
283
  compiledView: {immutable = true}
281
284
  } = renderedView;
282
- const linkedMetadata = skipMetadataCollection ? await (0, import_shared_utils.extractMetadataFromHtml)(renderedViewContent) : renderedViewMetadata;
283
- const {source: linkedAssetContent, assetReferences: linkedAssetReferences} = await (0, import_link_assets.linkAssets)(renderedViewContent, linkedMetadata.assetReferences, {
284
- assetRegistry,
285
+ const linkedMetadata = skipMetadataCollection ? renderedViewMetadata : await (0, import_shared_utils.extractMetadataFromHtml)(renderedViewContent);
286
+ const mergedViewContext = {
287
+ ...viewContext,
285
288
  runtimeEnvironment,
286
289
  importer: importer || renderedView.compiledView.filePath
287
- });
288
- linkedMetadata.assetReferences = linkedAssetReferences;
290
+ };
291
+ const stringBuilder = (0, import_shared_utils.createStringBuilder)(renderedViewContent);
292
+ for (const viewTransformer of this.viewTransformers) {
293
+ await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
294
+ }
295
+ const linkedAssetContent = stringBuilder.toString();
289
296
  if (linkedAssetContent.includes(lwrResourcesId)) {
290
297
  const {renderedView: linkedView, viewRecord} = await (0, import_link_lwr_resources.linkLwrResources)(linkedAssetContent, view, viewParams, {
291
298
  lwrResourcesId,
@@ -300,7 +307,7 @@ var LwrViewRegistry = class {
300
307
  renderedView: linkedView,
301
308
  immutable,
302
309
  viewRecord: {
303
- assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedAssetReferences),
310
+ assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedMetadata.assetReferences),
304
311
  ...viewRecord
305
312
  }
306
313
  };
@@ -309,7 +316,7 @@ var LwrViewRegistry = class {
309
316
  renderedView: linkedAssetContent,
310
317
  immutable,
311
318
  viewRecord: {
312
- assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedAssetReferences)
319
+ assetReferences: (0, import_utils.reduceSourceAssetReferences)(linkedMetadata.assetReferences)
313
320
  }
314
321
  };
315
322
  }
@@ -98,7 +98,8 @@ async function getHtmlResources(view, viewParams, resourceContext) {
98
98
  }
99
99
  }
100
100
  const customElementsRecords = [];
101
- await Promise.all(customElements.map(async (element) => {
101
+ const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements);
102
+ await Promise.all(flattenedElements.map(async ({tagName: element}) => {
102
103
  const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabcaseToCamelcase)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
103
104
  customElementsRecords.push({elementName: element, flatGraph: graph});
104
105
  const specifier = graph.graphs[0].specifier;
@@ -123,6 +124,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
123
124
  url: viewParams?.page?.url,
124
125
  configAsSrc: view.bootstrap?.configAsSrc || false
125
126
  }, {
127
+ appId: appIdentity.appName,
126
128
  bootstrapModule: versionedSpecifier,
127
129
  autoBoot: view.bootstrap?.autoBoot === false ? false : true,
128
130
  importMappings: {
@@ -24,6 +24,7 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/view-registry/src/linkers/utils.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ flattenCustomElements: () => flattenCustomElements,
27
28
  getViewBootstrapConfigurationResource: () => getViewBootstrapConfigurationResource,
28
29
  getViewHmrConfigurationResource: () => getViewHmrConfigurationResource
29
30
  });
@@ -97,3 +98,23 @@ function getViewHmrConfigurationResource(view, viewMetadata) {
97
98
  content: configString
98
99
  };
99
100
  }
101
+ function flattenCustomElements(arr) {
102
+ const ret = [];
103
+ const visitedTags = new Set();
104
+ function flatten(arr2) {
105
+ for (const val of arr2) {
106
+ const {tagName, children} = val;
107
+ if (!visitedTags.has(tagName)) {
108
+ ret.push({
109
+ tagName
110
+ });
111
+ visitedTags.add(tagName);
112
+ }
113
+ if (children) {
114
+ flatten(children);
115
+ }
116
+ }
117
+ }
118
+ flatten(arr);
119
+ return ret;
120
+ }
@@ -72,7 +72,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
72
72
  }, moduleRegistry, runtimeEnvironment, runtimeParams));
73
73
  }
74
74
  }
75
- const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 0};
75
+ const depth = isAMD ? {static: import_shared_utils.GraphDepth.ALL, dynamic: 1} : {static: import_shared_utils.GraphDepth.NONE, dynamic: 1};
76
76
  const bootstrapModuleGraph = await (0, import_shared_utils.getModuleGraphs)(bootstrapSpecifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
77
77
  const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
78
78
  const uri = bootstrapModuleGraph.uriMap[versionedSpecifier];
@@ -99,7 +99,8 @@ async function getHtmlResources(view, viewParams, resourceContext) {
99
99
  }
100
100
  let importMetadata = await (0, import_shared_utils.toImportMetadata)(bootstrapModuleGraph, {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams);
101
101
  const customElementsRecords = [];
102
- await Promise.all(customElements.map(async (element) => {
102
+ const flattenedElements = (0, import_utils2.flattenCustomElements)(customElements);
103
+ await Promise.all(flattenedElements.map(async ({tagName: element}) => {
103
104
  const graph = await (0, import_shared_utils.getModuleGraphs)((0, import_shared_utils.kebabcaseToCamelcase)(element), {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
104
105
  customElementsRecords.push({elementName: element, flatGraph: graph});
105
106
  const specifier = graph.graphs[0].specifier;
@@ -125,6 +126,7 @@ async function getHtmlResources(view, viewParams, resourceContext) {
125
126
  url: viewParams?.page?.url,
126
127
  configAsSrc: view.bootstrap?.configAsSrc || false
127
128
  }, {
129
+ appId: appIdentity.appName,
128
130
  bootstrapModule: versionedSpecifier,
129
131
  autoBoot: view.bootstrap?.autoBoot === false ? false : true,
130
132
  imports: importMetadata?.imports,
@@ -137,11 +139,19 @@ async function getHtmlResources(view, viewParams, resourceContext) {
137
139
  configResources.unshift(await (0, import_utils2.getViewHmrConfigurationResource)(view, viewMetadata));
138
140
  }
139
141
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(import_utils.generateHtmlTag));
142
+ const mapping = (0, import_shared_utils.getMappingUriPrefix)(runtimeEnvironment, runtimeParams);
143
+ const endpoints = {
144
+ uris: {
145
+ mapping
146
+ }
147
+ };
140
148
  return {
141
149
  partial: htmlResources.join("\n"),
142
150
  viewRecord: {
143
151
  resources: requiredResources,
144
152
  customElements: customElementsRecords,
153
+ endpoints,
154
+ importMetadata,
145
155
  bootstrapModule: {
146
156
  specifier: bootstrapModuleGraph.graphs[0].specifier,
147
157
  flatGraph: bootstrapModuleGraph,
@@ -39,8 +39,9 @@ __export(exports, {
39
39
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
40
40
  var import_path = __toModule(require("path"));
41
41
  var import_path2 = __toModule(require("path"));
42
- var import_mime_types = __toModule(require("mime-types"));
42
+ var import_shared_utils2 = __toModule(require("@lwrjs/shared-utils"));
43
43
  var import_identity = __toModule(require("@lwrjs/app-service/identity"));
44
+ var import_shared_utils3 = __toModule(require("@lwrjs/shared-utils"));
44
45
  function streamToString(stream) {
45
46
  const chunks = [];
46
47
  return new Promise((resolve, reject) => {
@@ -102,7 +103,7 @@ function normalizeRenderedResult({
102
103
  assetReferences: metadata ? metadata.assetReferences : []
103
104
  },
104
105
  options: {
105
- skipMetadataCollection: options ? options.skipMetadataCollection : true
106
+ skipMetadataCollection: options ? options.skipMetadataCollection : false
106
107
  }
107
108
  };
108
109
  }
@@ -117,7 +118,7 @@ function reduceSourceAssetReferences(assets) {
117
118
  }
118
119
  function normalizeRenderOptions(runtimeEnvironment, overrideRenderOptions, baseRenderOptions) {
119
120
  return {
120
- skipMetadataCollection: true,
121
+ skipMetadataCollection: false,
121
122
  freezeAssets: runtimeEnvironment.immutableAssets,
122
123
  skipCaching: false,
123
124
  viewParamCacheKey: null,
@@ -180,7 +181,7 @@ async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironme
180
181
  });
181
182
  resources.push({
182
183
  type: "application/javascript",
183
- src: (0, import_shared_utils.getClientBootstrapConfigurationUri)({url: viewRequest.url, id: route.id}, runtimeEnvironment, runtimeParams)
184
+ src: (0, import_shared_utils3.getClientBootstrapConfigurationUri)({url: viewRequest.url, id: route.id}, runtimeEnvironment, runtimeParams)
184
185
  });
185
186
  }
186
187
  const mappingUrl = (0, import_shared_utils.getMappingUriPrefix)(runtimeEnvironment, runtimeParams);
@@ -200,7 +201,7 @@ async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEnvironme
200
201
  });
201
202
  viewRecord.assetReferences?.forEach((asset) => {
202
203
  if (asset.override?.uri) {
203
- const type = (0, import_mime_types.lookup)(asset.override?.uri);
204
+ const type = (0, import_shared_utils2.mimeLookup)(asset.override?.uri);
204
205
  resources.push({type, src: asset.override?.uri});
205
206
  }
206
207
  });
@@ -1,7 +1,8 @@
1
- import { AssetRegistry, CompiledView, Json, LinkedModuleDefinition, LinkedViewDefinition, LwrAppEmitter, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, PublicViewRegistry, RenderOptions, ResourceRegistry, RuntimeEnvironment, RuntimeParams, View, ViewIdentity, ViewParams, ViewProvider, ViewRegistry } from '@lwrjs/types';
1
+ import { AssetRegistry, CompiledView, Json, LinkedModuleDefinition, LinkedViewDefinition, LwrAppEmitter, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, PublicViewRegistry, RenderOptions, ResourceRegistry, RuntimeEnvironment, RuntimeParams, View, ViewIdentity, ViewParams, ViewProvider, ViewRegistry, ViewTransformPlugin } from '@lwrjs/types';
2
2
  import { AssetSource } from '@lwrjs/types';
3
3
  export { LwrViewHandler } from './view-handler.js';
4
4
  interface ViewProviderContext {
5
+ viewTransformers?: ViewTransformPlugin[];
5
6
  appObserver: LwrAppObserver;
6
7
  appEmitter: LwrAppEmitter;
7
8
  assetRegistry: AssetRegistry;
@@ -30,6 +31,7 @@ export declare class LwrViewRegistry implements ViewRegistry {
30
31
  globalData: Json;
31
32
  runtimeEnvironment: RuntimeEnvironment;
32
33
  immutableAssets: Map<string, string>;
34
+ viewTransformers: ViewTransformPlugin[];
33
35
  private pendingViewDefinitions;
34
36
  constructor(context: ViewProviderContext, globalConfig: NormalizedLwrGlobalConfig);
35
37
  onModuleDefinitionChange(moduleDefinition: LinkedModuleDefinition): Promise<void>;
@@ -37,6 +39,7 @@ export declare class LwrViewRegistry implements ViewRegistry {
37
39
  onAssetSourceChange(asset: AssetSource): void;
38
40
  getPublicApi(): PublicViewRegistry;
39
41
  addViewProviders(providers: ViewProvider[]): void;
42
+ addViewTransformers(transformers: ViewTransformPlugin[]): void;
40
43
  initializeViewProviders(): Promise<void[]>;
41
44
  delegateGetView(viewId: ViewIdentity): Promise<CompiledView>;
42
45
  getView(viewId: ViewIdentity, skipCaching?: boolean): Promise<CompiledView>;
package/build/es/index.js CHANGED
@@ -1,6 +1,5 @@
1
- import { extractMetadataFromHtml, getCacheKeyFromJson, getSpecifier, normalizeResourcePath, } from '@lwrjs/shared-utils';
1
+ import { extractMetadataFromHtml, getCacheKeyFromJson, getSpecifier, normalizeResourcePath, createStringBuilder, } from '@lwrjs/shared-utils';
2
2
  import { normalizeRenderOptions, normalizeRenderedResult, reduceSourceAssetReferences } from './utils.js';
3
- import { linkAssets } from './linkers/link-assets.js';
4
3
  import { linkLwrResources } from './linkers/link-lwr-resources.js';
5
4
  export { LwrViewHandler } from './view-handler.js';
6
5
  export class LwrViewRegistry {
@@ -26,6 +25,7 @@ export class LwrViewRegistry {
26
25
  this.moduleRegistry = context.moduleRegistry;
27
26
  this.moduleBundler = context.moduleBundler;
28
27
  this.assetRegistry = context.assetRegistry;
28
+ this.viewTransformers = context.viewTransformers || [];
29
29
  this.globalConfig = globalConfig;
30
30
  this.globalData = context.globalData;
31
31
  this.appEmitter = context.appEmitter;
@@ -96,6 +96,9 @@ export class LwrViewRegistry {
96
96
  addViewProviders(providers) {
97
97
  this.viewProviders.push(...providers);
98
98
  }
99
+ addViewTransformers(transformers) {
100
+ this.viewTransformers.push(...transformers);
101
+ }
99
102
  initializeViewProviders() {
100
103
  return Promise.all(this.viewProviders.map((vp) => vp.initialize()));
101
104
  }
@@ -291,21 +294,25 @@ export class LwrViewRegistry {
291
294
  const { view, viewParams, runtimeEnvironment: runtimeEnv, runtimeParams, renderOptions, contentIds, importer, } = viewContext;
292
295
  const { skipMetadataCollection, freezeAssets } = renderOptions;
293
296
  const { lwrResourcesId } = contentIds;
294
- const { moduleRegistry, assetRegistry, resourceRegistry, moduleBundler } = this;
297
+ const { moduleRegistry, resourceRegistry, moduleBundler } = this;
295
298
  const runtimeEnvironment = { ...runtimeEnv, immutableAssets: freezeAssets };
296
299
  // normalize/extract metadata
297
300
  const { renderedView: renderedViewContent, metadata: renderedViewMetadata, compiledView: { immutable = true }, } = renderedView;
298
301
  const linkedMetadata = skipMetadataCollection
299
- ? await extractMetadataFromHtml(renderedViewContent)
300
- : renderedViewMetadata;
301
- // link assets
302
- const { source: linkedAssetContent, assetReferences: linkedAssetReferences } = await linkAssets(renderedViewContent, linkedMetadata.assetReferences, {
303
- assetRegistry,
302
+ ? renderedViewMetadata
303
+ : await extractMetadataFromHtml(renderedViewContent);
304
+ const mergedViewContext = {
305
+ ...viewContext,
304
306
  runtimeEnvironment,
305
307
  importer: importer || renderedView.compiledView.filePath,
306
- });
307
- linkedMetadata.assetReferences = linkedAssetReferences;
308
- // link LWR related resources
308
+ };
309
+ const stringBuilder = createStringBuilder(renderedViewContent);
310
+ for (const viewTransformer of this.viewTransformers) {
311
+ // eslint-disable-next-line no-await-in-loop
312
+ await viewTransformer.link?.(stringBuilder, mergedViewContext, linkedMetadata);
313
+ }
314
+ const linkedAssetContent = stringBuilder.toString();
315
+ // Link LWR related resources
309
316
  if (linkedAssetContent.includes(lwrResourcesId)) {
310
317
  // This calculation is expensive, only do it if lwr_resources is needed
311
318
  // Link LWR View resources if necessary
@@ -323,7 +330,7 @@ export class LwrViewRegistry {
323
330
  renderedView: linkedView,
324
331
  immutable,
325
332
  viewRecord: {
326
- assetReferences: reduceSourceAssetReferences(linkedAssetReferences),
333
+ assetReferences: reduceSourceAssetReferences(linkedMetadata.assetReferences),
327
334
  ...viewRecord,
328
335
  },
329
336
  };
@@ -332,7 +339,7 @@ export class LwrViewRegistry {
332
339
  renderedView: linkedAssetContent,
333
340
  immutable,
334
341
  viewRecord: {
335
- assetReferences: reduceSourceAssetReferences(linkedAssetReferences),
342
+ assetReferences: reduceSourceAssetReferences(linkedMetadata.assetReferences),
336
343
  },
337
344
  };
338
345
  }
@@ -1,7 +1,7 @@
1
1
  import { kebabcaseToCamelcase, getModuleGraphs, GraphDepth, getModuleUriPrefix } from '@lwrjs/shared-utils';
2
2
  import { AppResourceEnum, getAppSpecifier } from '@lwrjs/app-service/identity';
3
3
  import { generateHtmlTag, getModuleResource, getModuleResourceByUri } from '../utils.js';
4
- import { getViewBootstrapConfigurationResource, getViewHmrConfigurationResource } from './utils.js';
4
+ import { flattenCustomElements, getViewBootstrapConfigurationResource, getViewHmrConfigurationResource, } from './utils.js';
5
5
  export async function getHtmlResources(view, viewParams, resourceContext) {
6
6
  const { runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler, resourceRegistry, viewMetadata, } = resourceContext;
7
7
  const { id: appName, bootstrap: { services } = { services: [] } } = view;
@@ -102,7 +102,8 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
102
102
  }
103
103
  // ------- View related custom element moduleResources
104
104
  const customElementsRecords = [];
105
- await Promise.all(customElements.map(async (element) => {
105
+ const flattenedElements = flattenCustomElements(customElements);
106
+ await Promise.all(flattenedElements.map(async ({ tagName: element }) => {
106
107
  const graph = await getModuleGraphs(kebabcaseToCamelcase(element), { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
107
108
  // add to the viewRecord
108
109
  customElementsRecords.push({ elementName: element, flatGraph: graph });
@@ -133,6 +134,7 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
133
134
  url: viewParams?.page?.url,
134
135
  configAsSrc: view.bootstrap?.configAsSrc || false,
135
136
  }, {
137
+ appId: appIdentity.appName,
136
138
  bootstrapModule: versionedSpecifier,
137
139
  autoBoot: view.bootstrap?.autoBoot === false ? false : true,
138
140
  importMappings: {
@@ -1,5 +1,6 @@
1
- import { ClientBootstrapConfig, RuntimeEnvironment, RuntimeParams, ResourceDefinition, RenderedViewMetadata } from '@lwrjs/types';
1
+ import { ClientBootstrapConfig, RuntimeEnvironment, RuntimeParams, ResourceDefinition, RenderedViewMetadata, CustomElementReference } from '@lwrjs/types';
2
2
  import { View, ViewInfo } from '@lwrjs/types';
3
3
  export declare function getViewBootstrapConfigurationResource(viewInfo: ViewInfo, config: ClientBootstrapConfig, runtimeEnvironment: RuntimeEnvironment, runtimeParams?: RuntimeParams): ResourceDefinition;
4
4
  export declare function getViewHmrConfigurationResource(view: View, viewMetadata: RenderedViewMetadata): ResourceDefinition;
5
+ export declare function flattenCustomElements(arr: CustomElementReference[]): CustomElementReference[];
5
6
  //# sourceMappingURL=utils.d.ts.map
@@ -76,4 +76,24 @@ export function getViewHmrConfigurationResource(view, viewMetadata) {
76
76
  content: configString,
77
77
  };
78
78
  }
79
+ export function flattenCustomElements(arr) {
80
+ const ret = [];
81
+ const visitedTags = new Set();
82
+ function flatten(arr) {
83
+ for (const val of arr) {
84
+ const { tagName, children } = val;
85
+ if (!visitedTags.has(tagName)) {
86
+ ret.push({
87
+ tagName,
88
+ });
89
+ visitedTags.add(tagName);
90
+ }
91
+ if (children) {
92
+ flatten(children);
93
+ }
94
+ }
95
+ }
96
+ flatten(arr);
97
+ return ret;
98
+ }
79
99
  //# sourceMappingURL=utils.js.map
@@ -1,7 +1,7 @@
1
- import { kebabcaseToCamelcase, toImportMetadata, getModuleGraphs, GraphDepth } from '@lwrjs/shared-utils';
1
+ import { kebabcaseToCamelcase, toImportMetadata, getModuleGraphs, getMappingUriPrefix, GraphDepth, } from '@lwrjs/shared-utils';
2
2
  import { AppResourceEnum, getAppSpecifier } from '@lwrjs/app-service/identity';
3
3
  import { generateHtmlTag, getModuleResource, getModuleResourceByUri } from '../utils.js';
4
- import { getViewBootstrapConfigurationResource, getViewHmrConfigurationResource } from './utils.js';
4
+ import { flattenCustomElements, getViewBootstrapConfigurationResource, getViewHmrConfigurationResource, } from './utils.js';
5
5
  export async function getHtmlResources(view, viewParams, resourceContext) {
6
6
  const { runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler, resourceRegistry, viewMetadata, } = resourceContext;
7
7
  const { id: appName, bootstrap: { services } = { services: [] } } = view;
@@ -63,10 +63,9 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
63
63
  // ------- Application Bootstrap module
64
64
  // Traversal of the Bootstrap Module Graph is done to get all the URLS for discoverable static dependencies.
65
65
  // Reasoning: This is to avoid unnecessary HTTP 302's during initial application module fetching.
66
- // Scope: ESM currently only exposes immutable URI references, optimize for AMD formats
67
66
  const depth = isAMD
68
67
  ? { static: GraphDepth.ALL, dynamic: 1 }
69
- : { static: GraphDepth.NONE, dynamic: 0 };
68
+ : { static: GraphDepth.NONE, dynamic: 1 };
70
69
  const bootstrapModuleGraph = await getModuleGraphs(bootstrapSpecifier, { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
71
70
  // ADD bootstrap module uri as a script resource
72
71
  const versionedSpecifier = bootstrapModuleGraph.graphs[0].specifier;
@@ -75,12 +74,12 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
75
74
  if (isAMD) {
76
75
  // ADD bootstrap module as required
77
76
  requiredAmdModules.push(versionedSpecifier);
78
- // AMD ADD bootstrap module uri addressability
77
+ // ADD bootstrap module uri addressability
79
78
  imports[versionedSpecifier] = uri;
80
79
  // PRELOAD bootstrap module static deps as link resource
81
80
  for (const staticDep of bootstrapModuleGraph.graphs[0].static) {
82
81
  const uri = bootstrapModuleGraph.uriMap[staticDep];
83
- // AMD ADD static module dep uri addressability
82
+ // ADD static module dep uri addressability
84
83
  imports[staticDep] = uri;
85
84
  // ADD bootstrap module static deps to requiredAmdModules if services, otherwise preloadModules
86
85
  if (services && services.length) {
@@ -89,7 +88,7 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
89
88
  requiredAmdModules.push(staticDep);
90
89
  }
91
90
  }
92
- // AMD Add import mappings for known dynamic imports
91
+ // Add import mappings for known dynamic imports
93
92
  for (const dynamicDep of bootstrapModuleGraph.graphs[0].dynamicRefs) {
94
93
  const uri = bootstrapModuleGraph.uriMap[dynamicDep];
95
94
  if (uri) {
@@ -103,7 +102,8 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
103
102
  let importMetadata = await toImportMetadata(bootstrapModuleGraph, { imports: {}, index: {} }, moduleRegistry, runtimeEnvironment, runtimeParams);
104
103
  // ------- View related custom element moduleResources
105
104
  const customElementsRecords = [];
106
- await Promise.all(customElements.map(async (element) => {
105
+ const flattenedElements = flattenCustomElements(customElements);
106
+ await Promise.all(flattenedElements.map(async ({ tagName: element }) => {
107
107
  const graph = await getModuleGraphs(kebabcaseToCamelcase(element), { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams ? runtimeParams : {}, visitedCache);
108
108
  // add to the viewRecord
109
109
  customElementsRecords.push({ elementName: element, flatGraph: graph });
@@ -135,6 +135,7 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
135
135
  url: viewParams?.page?.url,
136
136
  configAsSrc: view.bootstrap?.configAsSrc || false,
137
137
  }, {
138
+ appId: appIdentity.appName,
138
139
  bootstrapModule: versionedSpecifier,
139
140
  autoBoot: view.bootstrap?.autoBoot === false ? false : true,
140
141
  imports: importMetadata?.imports,
@@ -148,11 +149,19 @@ export async function getHtmlResources(view, viewParams, resourceContext) {
148
149
  }
149
150
  // generate html partial
150
151
  const htmlResources = await Promise.all([...configResources, ...requiredResources, ...moduleResources].map(generateHtmlTag));
152
+ const mapping = getMappingUriPrefix(runtimeEnvironment, runtimeParams);
153
+ const endpoints = {
154
+ uris: {
155
+ mapping,
156
+ },
157
+ };
151
158
  return {
152
159
  partial: htmlResources.join('\n'),
153
160
  viewRecord: {
154
161
  resources: requiredResources,
155
162
  customElements: customElementsRecords,
163
+ endpoints,
164
+ importMetadata,
156
165
  bootstrapModule: {
157
166
  specifier: bootstrapModuleGraph.graphs[0].specifier,
158
167
  flatGraph: bootstrapModuleGraph,
package/build/es/utils.js CHANGED
@@ -1,8 +1,9 @@
1
- import { explodeSpecifier, getMappingUriPrefix, getSpecifier, resolveFileExtension, transpileTs, DEFAULT_TITLE, getClientBootstrapConfigurationUri, } from '@lwrjs/shared-utils';
1
+ import { explodeSpecifier, getMappingUriPrefix, getSpecifier, resolveFileExtension, transpileTs, DEFAULT_TITLE, } from '@lwrjs/shared-utils';
2
2
  import libPath from 'path';
3
3
  import { basename, extname } from 'path';
4
- import { lookup } from 'mime-types';
4
+ import { mimeLookup } from '@lwrjs/shared-utils';
5
5
  import { AppResourceEnum, getAppSpecifier, ResourceIdentityTypes, } from '@lwrjs/app-service/identity';
6
+ import { getClientBootstrapConfigurationUri } from '@lwrjs/shared-utils';
6
7
  function streamToString(stream) {
7
8
  const chunks = [];
8
9
  return new Promise((resolve, reject) => {
@@ -64,7 +65,7 @@ export function normalizeRenderedResult({ renderedView, metadata, options, }) {
64
65
  assetReferences: metadata ? metadata.assetReferences : [],
65
66
  },
66
67
  options: {
67
- skipMetadataCollection: options ? options.skipMetadataCollection : true,
68
+ skipMetadataCollection: options ? options.skipMetadataCollection : false,
68
69
  },
69
70
  };
70
71
  }
@@ -80,7 +81,7 @@ export function reduceSourceAssetReferences(assets) {
80
81
  export function normalizeRenderOptions(runtimeEnvironment, overrideRenderOptions, baseRenderOptions) {
81
82
  return {
82
83
  // Default render options
83
- skipMetadataCollection: true,
84
+ skipMetadataCollection: false,
84
85
  freezeAssets: runtimeEnvironment.immutableAssets,
85
86
  skipCaching: false,
86
87
  viewParamCacheKey: null,
@@ -182,7 +183,7 @@ export async function toJsonFormat(viewRequest, viewDefinition, route, runtimeEn
182
183
  // ADD assetReferences to the JSON resources
183
184
  viewRecord.assetReferences?.forEach((asset) => {
184
185
  if (asset.override?.uri) {
185
- const type = lookup(asset.override?.uri);
186
+ const type = mimeLookup(asset.override?.uri);
186
187
  resources.push({ type, src: asset.override?.uri });
187
188
  }
188
189
  });
@@ -3,6 +3,7 @@ import { generateHtmlTag, generatePageContext, getRouteHandler, isViewResponse,
3
3
  import { resolve } from 'path';
4
4
  export class LwrViewHandler {
5
5
  constructor(context, globalConfig) {
6
+ // TODO convert to using InflightTasks in the shared utils
6
7
  this.inflightRouteHandlerEvalMap = new Map();
7
8
  this.routeHandlerFunctionMap = new Map();
8
9
  this.viewRegistry = context.viewRegistry;
package/package.json CHANGED
@@ -4,8 +4,8 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.5.11",
8
- "homepage": "https://lwr.dev/",
7
+ "version": "0.6.0-0.6.0-alpha.15.0",
8
+ "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "https://github.com/salesforce/lwr.git",
@@ -30,16 +30,15 @@
30
30
  "build/**/*.d.ts"
31
31
  ],
32
32
  "dependencies": {
33
- "@lwrjs/app-service": "0.5.11",
34
- "@lwrjs/diagnostics": "0.5.11",
35
- "@lwrjs/shared-utils": "0.5.11",
36
- "magic-string": "^0.25.7"
33
+ "@lwrjs/app-service": "0.6.0-0.6.0-alpha.15.0",
34
+ "@lwrjs/diagnostics": "0.6.0-0.6.0-alpha.15.0",
35
+ "@lwrjs/shared-utils": "0.6.0-0.6.0-alpha.15.0"
37
36
  },
38
37
  "devDependencies": {
39
- "@lwrjs/types": "0.5.11"
38
+ "@lwrjs/types": "0.6.0-0.6.0-alpha.15.0"
40
39
  },
41
40
  "engines": {
42
41
  "node": ">=14.15.4 <17"
43
42
  },
44
- "gitHead": "2ee810c0a4f1f36d1e0f8378a1fe00c19b4c5bc1"
43
+ "gitHead": "101ad971f5a7b0631dd149ee4e3aa1d1799a7af2"
45
44
  }
@@ -1,60 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getProtoOf = Object.getPrototypeOf;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, {get: all[name], enumerable: true});
11
- };
12
- var __exportStar = (target, module2, desc) => {
13
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
- for (let key of __getOwnPropNames(module2))
15
- if (!__hasOwnProp.call(target, key) && key !== "default")
16
- __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
- }
18
- return target;
19
- };
20
- var __toModule = (module2) => {
21
- return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
- };
23
-
24
- // packages/@lwrjs/view-registry/src/linkers/link-assets.ts
25
- __markAsModule(exports);
26
- __export(exports, {
27
- linkAssets: () => linkAssets
28
- });
29
- var import_magic_string = __toModule(require("magic-string"));
30
- var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
- function rewriteDocumentWithImmutableUrls(renderedView, assetReferences) {
32
- const codeStringBuilder = new import_magic_string.default(renderedView);
33
- assetReferences.forEach((assetRef) => {
34
- const {
35
- override,
36
- url,
37
- location: {startOffset, endOffset}
38
- } = assetRef;
39
- startOffset !== endOffset && codeStringBuilder.overwrite(startOffset, endOffset, override?.uri || url);
40
- });
41
- return codeStringBuilder.toString();
42
- }
43
- async function linkAssets(source, assetReferences, {assetRegistry, runtimeEnvironment, importer}) {
44
- const imageRefs = await Promise.all(assetReferences.map(async (ref) => {
45
- const {url: specifier} = ref;
46
- let override;
47
- try {
48
- override = await assetRegistry.resolveAssetUri({specifier, importer}, runtimeEnvironment);
49
- } catch (e) {
50
- if (e instanceof import_diagnostics.LwrUnresolvableError) {
51
- console.warn("[WARN]: Asset was not found for " + ref.url);
52
- return ref;
53
- }
54
- throw e;
55
- }
56
- return {...ref, override};
57
- }));
58
- const modifiedSource = rewriteDocumentWithImmutableUrls(source, imageRefs);
59
- return {source: modifiedSource, assetReferences: imageRefs};
60
- }
@@ -1,12 +0,0 @@
1
- import { AssetReference, PublicAssetRegistry, RuntimeEnvironment } from '@lwrjs/types';
2
- interface LinkedAssetsContext {
3
- assetRegistry: PublicAssetRegistry;
4
- runtimeEnvironment: RuntimeEnvironment;
5
- importer: string;
6
- }
7
- export declare function linkAssets(source: string, assetReferences: AssetReference[], { assetRegistry, runtimeEnvironment, importer }: LinkedAssetsContext): Promise<{
8
- source: string;
9
- assetReferences: AssetReference[];
10
- }>;
11
- export {};
12
- //# sourceMappingURL=link-assets.d.ts.map
@@ -1,33 +0,0 @@
1
- import MagicString from 'magic-string';
2
- import { LwrUnresolvableError } from '@lwrjs/diagnostics';
3
- function rewriteDocumentWithImmutableUrls(renderedView, assetReferences) {
4
- const codeStringBuilder = new MagicString(renderedView);
5
- assetReferences.forEach((assetRef) => {
6
- const { override, url, location: { startOffset, endOffset }, } = assetRef;
7
- startOffset !== endOffset &&
8
- codeStringBuilder.overwrite(startOffset, endOffset, override?.uri || url);
9
- });
10
- return codeStringBuilder.toString();
11
- }
12
- export async function linkAssets(source, assetReferences, { assetRegistry, runtimeEnvironment, importer }) {
13
- const imageRefs = await Promise.all(assetReferences.map(async (ref) => {
14
- const { url: specifier } = ref;
15
- let override;
16
- try {
17
- override = await assetRegistry.resolveAssetUri({ specifier, importer }, runtimeEnvironment);
18
- }
19
- catch (e) {
20
- if (e instanceof LwrUnresolvableError) {
21
- // the asset may not exist, in which case the URL is not overwritten. Log a warning so
22
- // the developer can see the bad asset URL, and be able to correct it
23
- console.warn('[WARN]: Asset was not found for ' + ref.url);
24
- return ref;
25
- }
26
- throw e;
27
- }
28
- return { ...ref, override };
29
- }));
30
- const modifiedSource = rewriteDocumentWithImmutableUrls(source, imageRefs);
31
- return { source: modifiedSource, assetReferences: imageRefs };
32
- }
33
- //# sourceMappingURL=link-assets.js.map