@lwrjs/loader 0.6.0-alpha.13 → 0.6.0-alpha.14

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.
@@ -4,244 +4,307 @@
4
4
  * SPDX-License-Identifier: MIT
5
5
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
6
  */
7
- /* LWR Legacy Module Loader Shim v0.6.0-alpha.13 */
7
+ /* LWR Legacy Module Loader Shim v0.6.0-alpha.14 */
8
8
  (function () {
9
- 'use strict';
9
+ 'use strict';
10
10
 
11
- function createLoader(name, definition, baseUrl, externalModules) {
12
- if (!definition || typeof definition[2] !== 'function') {
13
- throw new Error(`Expected loader with specifier "${name}" to be a module`);
14
- }
15
- // Create a Loader instance
16
- const exports = {};
17
- definition[2].call(null, exports);
18
- const { Loader } = exports;
19
- const loader = new Loader(baseUrl);
20
- // register externally loaded modules
21
- if (externalModules && externalModules.length) {
22
- loader.registerExternalModules(externalModules);
23
- }
24
- // Define the loader module with public API: { define, load, services }
25
- const exporter = (exports) => {
26
- Object.assign(exports, {
27
- define: loader.define.bind(loader),
28
- load: loader.load.bind(loader),
29
- services: loader.services,
30
- });
31
- return;
32
- };
33
- loader.define(name, ['exports'], exporter, definition[3]);
34
- return loader;
35
- }
11
+ // Bootstrap / shim
12
+ const BOOTSTRAP_PREFIX = 'lwr.bootstrap.';
13
+ const BOOTSTRAP_ERROR = `${BOOTSTRAP_PREFIX}error`;
36
14
 
37
- const REQUIRED_MODULES_TIMEOUT = 300 * 1000;
15
+ var Phase;
38
16
 
39
- var Phase;
17
+ (function (Phase) {
18
+ Phase[Phase["Start"] = 0] = "Start";
19
+ Phase[Phase["End"] = 1] = "End";
20
+ })(Phase || (Phase = {}));
40
21
 
41
- (function (Phase) {
42
- Phase[Phase["Start"] = 0] = "Start";
43
- Phase[Phase["End"] = 1] = "End";
44
- })(Phase || (Phase = {}));
45
- function attachDispatcher(dispatcher) {
46
- } // Check if the Performance API is available
22
+ // Attach a custom dispatcher
23
+ let customDispatcher;
24
+ function attachDispatcher(dispatcher) {
25
+ customDispatcher = dispatcher;
26
+ } // Check if the Performance API is available
27
+ // e.g. JSDom (used in Jest) doesn't implement these
47
28
 
48
- // Check for errors with autoBoot and customInit
49
- function validatePreInit(autoBoot, customInit) {
50
- // If autoBoot === false, there must be a customInit hook
51
- if (!autoBoot && !customInit) {
52
- throw new Error('The customInit hook is required when autoBoot is false');
53
- }
54
- // If autoBoot === true, there must NOT be a customInit hook
55
- if (autoBoot && customInit) {
56
- throw new Error('The customInit hook must not be defined when autoBoot is true');
57
- }
58
- }
59
- // Process the customInit hook
60
- function customInit(config, initializeApp, define, onBootstrapError) {
61
- // Validate config
62
- const { autoBoot, customInit } = config;
63
- validatePreInit(autoBoot, customInit);
64
- // Set up arguments and call the customInit hook, if available
65
- if (customInit) {
66
- const lwr = {
67
- initializeApp,
68
- define,
69
- onBootstrapError,
70
- attachDispatcher,
71
- };
72
- customInit(lwr, config);
73
- }
74
- }
29
+ const perf = globalThis.performance;
30
+ const isPerfSupported = typeof perf !== 'undefined' && typeof perf.mark === 'function' && typeof perf.clearMarks === 'function' && typeof perf.measure === 'function' && typeof perf.clearMeasures === 'function'; // For marking request metrics
31
+ // Fallback to the Performance API if there is no custom dispatcher
75
32
 
76
- /* global document */
77
- /* eslint-disable lwr/no-unguarded-apis */
78
- const hasSetTimeout = typeof setTimeout === 'function';
79
- const hasConsole = typeof console !== 'undefined';
80
- /* eslint-enable lwr/no-unguarded-apis */
81
- class LoaderShim {
82
- constructor(global) {
83
- this.defineCache = {};
84
- this.orderedDefs = [];
85
- // Parse configuration
86
- this.global = global;
87
- this.config = global.LWR;
88
- this.loaderModule = 'lwr/loaderLegacy/v/0_6_0-alpha_13';
89
- // Set up the temporary LWR.define function and customInit hook
90
- const tempDefine = this.tempDefine.bind(this);
91
- global.LWR.define = tempDefine;
92
- this.bootReady = this.config.autoBoot;
93
- // Start watchdog timer
94
- if (hasSetTimeout) {
95
- this.watchdogTimerId = this.startWatchdogTimer();
96
- }
97
- try {
98
- customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
99
- this.errorHandler = e;
100
- });
101
- }
102
- catch (e) {
103
- this.enterErrorState(e);
104
- }
105
- }
106
- // Return true if the app can be initialized
107
- canInit() {
108
- // Initialize the app if:
109
- // - bootReady: autoBoot is on OR customInit has finished
110
- // - all required modules are defined
111
- const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
112
- return this.bootReady && allDefined;
113
- }
114
- /**
115
- * Create a temporary LWR.define() function which captures all
116
- * calls that occur BEFORE the full loader module is available
117
- *
118
- * Each call to LWR.define() is stored in 2 ways:
119
- * - in a map as [moduleName, arguments] pairs
120
- * - each moduleName is pushed onto an array, to preserve
121
- * the order in which the modules were defined
122
- */
123
- tempDefine(...args) {
124
- // Cache the incoming module
125
- const moduleName = args[0];
126
- this.defineCache[moduleName] = args;
127
- this.orderedDefs.push(moduleName);
128
- if (this.canInit()) {
129
- if (hasSetTimeout) {
130
- // requiredModules are defined, clear watchdog timer
131
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
132
- clearTimeout(this.watchdogTimerId);
133
- }
134
- this.initApp();
135
- }
136
- }
137
- // Called by the customInit hook via lwr.initializeApp()
138
- postCustomInit() {
139
- this.bootReady = true;
140
- if (this.canInit()) {
141
- this.initApp();
142
- }
143
- }
144
- // Create the loader and initialize the application
145
- initApp() {
146
- try {
147
- const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], this.config.baseUrl, this.config.preloadModules);
148
- this.mountApp(loader);
149
- }
150
- catch (e) {
151
- this.enterErrorState(e);
152
- }
153
- }
154
- waitForDOMContentLoaded() {
155
- // eslint-disable-next-line lwr/no-unguarded-apis
156
- if (typeof document === undefined) {
157
- return Promise.resolve();
158
- }
159
- // Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
160
- // eslint-disable-next-line lwr/no-unguarded-apis
161
- if (document.readyState === 'interactive' || document.readyState === 'complete') {
162
- return Promise.resolve();
163
- }
164
- return new Promise((resolve) => {
165
- // eslint-disable-next-line lwr/no-unguarded-apis
166
- document.addEventListener('DOMContentLoaded', () => {
167
- resolve();
168
- });
169
- });
170
- }
171
- // Set up the application globals, import map, root custom element...
172
- mountApp(loader) {
173
- const { bootstrapModule, rootComponent, importMappings, rootComponents, endpoints } = this.config;
174
- // Set global LWR.define to loader.define
175
- this.global.LWR = Object.freeze({
176
- define: loader.define.bind(loader),
177
- rootComponent,
178
- rootComponents,
179
- importMappings,
180
- endpoints,
181
- });
182
- // Redefine all modules in the temporary cache
183
- this.orderedDefs.forEach((specifier) => {
184
- if (specifier !== this.loaderModule) {
185
- loader.define(...this.defineCache[specifier]);
186
- }
187
- });
188
- // Define a dummy profiler since the "lwr/profiler" module is used in "lwr/init" (which is global to all server modes)
189
- const profilerExporter = (exports) => {
190
- Object.assign(exports, {
191
- logOperationStart: () => {
192
- /* noop */
193
- },
194
- });
195
- return;
196
- };
197
- loader.define('lwr/profiler/v/0_6_0-alpha_13', ['exports'], profilerExporter, {});
198
- // by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
199
- const { disableInitDefer } = this.config;
200
- // Load the import mappings and application bootstrap module
201
- loader
202
- .registerImportMappings(importMappings)
203
- .then(() => {
204
- if (!disableInitDefer) {
205
- return this.waitForDOMContentLoaded();
206
- }
207
- })
208
- .then(() => loader.load(bootstrapModule))
209
- .catch((reason) => {
210
- this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
211
- });
212
- }
213
- // Trigger bootstrap error state, and call error handler if registered
214
- enterErrorState(error) {
215
- if (this.errorHandler) {
216
- this.errorHandler(error);
217
- }
218
- else {
219
- if (hasConsole) {
220
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
221
- console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
222
- }
223
- }
224
- }
225
- // eslint-disable-next-line no-undef, lwr/no-unguarded-apis
226
- startWatchdogTimer() {
227
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
228
- return setTimeout(() => {
229
- this.enterErrorState(new Error('Failed to load required modules - timed out'));
230
- }, REQUIRED_MODULES_TIMEOUT);
231
- }
33
+ function logOperationStart({
34
+ id,
35
+ specifier
36
+ }) {
37
+ if (customDispatcher) {
38
+ customDispatcher({
39
+ id,
40
+ phase: Phase.Start,
41
+ specifier
42
+ });
43
+ } else if (isPerfSupported) {
44
+ perf.mark(id + (specifier ? `.${specifier}` : ''));
232
45
  }
46
+ } // For measuring duration metrics
47
+ // Fallback to the Performance API if there is no custom dispatcher
48
+
49
+ /* istanbul ignore next */
233
50
 
234
- // The loader module is ALWAYS required
235
- const GLOBAL = globalThis;
236
- GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
237
- if (GLOBAL.LWR.requiredModules.indexOf('lwr/loaderLegacy/v/0_6_0-alpha_13') < 0) {
238
- GLOBAL.LWR.requiredModules.push('lwr/loaderLegacy/v/0_6_0-alpha_13');
51
+ function logOperationEnd({
52
+ id,
53
+ specifier
54
+ }) {
55
+ if (customDispatcher) {
56
+ customDispatcher({
57
+ id,
58
+ phase: Phase.End,
59
+ specifier
60
+ });
61
+ } else if (isPerfSupported) {
62
+ const suffix = specifier ? `.${specifier}` : '';
63
+ const markName = id + suffix;
64
+ const measureName = `${id}.duration${suffix}`;
65
+ perf.measure(measureName, markName); // Clear the created mark and measure to avoid filling the performance entry buffer
66
+ // Even if they get deleted, existing PerformanceObservers preserve copies of the entries
67
+
68
+ perf.clearMarks(markName);
69
+ perf.clearMeasures(measureName);
239
70
  }
240
- new LoaderShim(GLOBAL);
71
+ }
72
+
73
+ function createLoader(name, definition, config, externalModules) {
74
+ if (!definition || typeof definition[2] !== 'function') {
75
+ throw new Error(`Expected loader with specifier "${name}" to be a module`);
76
+ }
77
+ // Create a Loader instance
78
+ const exports = {};
79
+ definition[2].call(null, exports);
80
+ const { Loader } = exports;
81
+ const loader = new Loader(config);
82
+ // register externally loaded modules
83
+ if (externalModules && externalModules.length) {
84
+ loader.registerExternalModules(externalModules);
85
+ }
86
+ // Define the loader module with public API: { define, load, services }
87
+ const exporter = (exports) => {
88
+ Object.assign(exports, {
89
+ define: loader.define.bind(loader),
90
+ load: loader.load.bind(loader),
91
+ services: loader.services,
92
+ });
93
+ return;
94
+ };
95
+ loader.define(name, ['exports'], exporter, definition[3]);
96
+ return loader;
97
+ }
98
+
99
+ const REQUIRED_MODULES_TIMEOUT = 300 * 1000;
100
+
101
+ // Check for errors with autoBoot and customInit
102
+ function validatePreInit(autoBoot, customInit) {
103
+ // If autoBoot === false, there must be a customInit hook
104
+ if (!autoBoot && !customInit) {
105
+ throw new Error('The customInit hook is required when autoBoot is false');
106
+ }
107
+ // If autoBoot === true, there must NOT be a customInit hook
108
+ if (autoBoot && customInit) {
109
+ throw new Error('The customInit hook must not be defined when autoBoot is true');
110
+ }
111
+ }
112
+ // Process the customInit hook
113
+ function customInit(config, initializeApp, define, onBootstrapError) {
114
+ // Validate config
115
+ const { autoBoot, customInit } = config;
116
+ validatePreInit(autoBoot, customInit);
117
+ // Set up arguments and call the customInit hook, if available
118
+ if (customInit) {
119
+ const lwr = {
120
+ initializeApp,
121
+ define,
122
+ onBootstrapError,
123
+ attachDispatcher,
124
+ };
125
+ customInit(lwr, config);
126
+ }
127
+ }
128
+
129
+ /* global document */
130
+ /* eslint-disable lwr/no-unguarded-apis */
131
+ const hasSetTimeout = typeof setTimeout === 'function';
132
+ const hasConsole = typeof console !== 'undefined';
133
+ /* eslint-enable lwr/no-unguarded-apis */
134
+ class LoaderShim {
135
+ constructor(global) {
136
+ this.defineCache = {};
137
+ this.orderedDefs = [];
138
+ // Parse configuration
139
+ this.global = global;
140
+ this.config = global.LWR;
141
+ this.loaderModule = 'lwr/loaderLegacy/v/0_6_0-alpha_14';
142
+ // Set up the temporary LWR.define function and customInit hook
143
+ const tempDefine = this.tempDefine.bind(this);
144
+ global.LWR.define = tempDefine;
145
+ this.bootReady = this.config.autoBoot;
146
+ // Start watchdog timer
147
+ if (hasSetTimeout) {
148
+ this.watchdogTimerId = this.startWatchdogTimer();
149
+ }
150
+ try {
151
+ customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
152
+ this.errorHandler = e;
153
+ });
154
+ }
155
+ catch (e) {
156
+ this.enterErrorState(e);
157
+ }
158
+ }
159
+ // Return true if the app can be initialized
160
+ canInit() {
161
+ // Initialize the app if:
162
+ // - bootReady: autoBoot is on OR customInit has finished
163
+ // - all required modules are defined
164
+ const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
165
+ return this.bootReady && allDefined;
166
+ }
167
+ /**
168
+ * Create a temporary LWR.define() function which captures all
169
+ * calls that occur BEFORE the full loader module is available
170
+ *
171
+ * Each call to LWR.define() is stored in 2 ways:
172
+ * - in a map as [moduleName, arguments] pairs
173
+ * - each moduleName is pushed onto an array, to preserve
174
+ * the order in which the modules were defined
175
+ */
176
+ tempDefine(...args) {
177
+ // Cache the incoming module
178
+ const moduleName = args[0];
179
+ this.defineCache[moduleName] = args;
180
+ this.orderedDefs.push(moduleName);
181
+ if (this.canInit()) {
182
+ if (hasSetTimeout) {
183
+ // requiredModules are defined, clear watchdog timer
184
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
185
+ clearTimeout(this.watchdogTimerId);
186
+ }
187
+ this.initApp();
188
+ }
189
+ }
190
+ // Called by the customInit hook via lwr.initializeApp()
191
+ postCustomInit() {
192
+ this.bootReady = true;
193
+ if (this.canInit()) {
194
+ this.initApp();
195
+ }
196
+ }
197
+ // Create the loader and initialize the application
198
+ initApp() {
199
+ try {
200
+ const loaderConfig = {
201
+ baseUrl: this.config.baseUrl,
202
+ profiler: { logOperationStart, logOperationEnd },
203
+ // TODO: can be removed following https://github.com/salesforce/lwr/issues/1087
204
+ appMetadata: {
205
+ bootstrapModule: this.config.bootstrapModule,
206
+ rootComponent: this.config.rootComponent,
207
+ rootComponents: this.config.rootComponents,
208
+ },
209
+ };
210
+ const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], loaderConfig, this.config.preloadModules);
211
+ this.createProfilerModule(loader);
212
+ this.mountApp(loader);
213
+ }
214
+ catch (e) {
215
+ this.enterErrorState(e);
216
+ }
217
+ }
218
+ waitForDOMContentLoaded() {
219
+ // eslint-disable-next-line lwr/no-unguarded-apis
220
+ if (typeof document === undefined) {
221
+ return Promise.resolve();
222
+ }
223
+ // Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
224
+ // eslint-disable-next-line lwr/no-unguarded-apis
225
+ if (document.readyState === 'interactive' || document.readyState === 'complete') {
226
+ return Promise.resolve();
227
+ }
228
+ return new Promise((resolve) => {
229
+ // eslint-disable-next-line lwr/no-unguarded-apis
230
+ document.addEventListener('DOMContentLoaded', () => {
231
+ resolve();
232
+ });
233
+ });
234
+ }
235
+ // Create a module out of the profiler
236
+ // Note: The profiler is also available as a module through lwc module resolution (see package.json)
237
+ createProfilerModule(loader) {
238
+ const exporter = (exports) => {
239
+ Object.assign(exports, { logOperationStart, logOperationEnd });
240
+ };
241
+ loader.define('lwr/profiler/v/0_6_0-alpha_14', ['exports'], exporter, {});
242
+ }
243
+ // Set up the application globals, import map, root custom element...
244
+ mountApp(loader) {
245
+ const { bootstrapModule, rootComponent, importMappings, rootComponents, endpoints } = this.config;
246
+ // Set global LWR.define to loader.define
247
+ this.global.LWR = Object.freeze({
248
+ define: loader.define.bind(loader),
249
+ rootComponent,
250
+ rootComponents,
251
+ importMappings,
252
+ endpoints,
253
+ });
254
+ // Redefine all modules in the temporary cache
255
+ this.orderedDefs.forEach((specifier) => {
256
+ if (specifier !== this.loaderModule) {
257
+ loader.define(...this.defineCache[specifier]);
258
+ }
259
+ });
260
+ // by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
261
+ const { disableInitDefer } = this.config;
262
+ // Load the import mappings and application bootstrap module
263
+ loader
264
+ .registerImportMappings(importMappings)
265
+ .then(() => {
266
+ if (!disableInitDefer) {
267
+ return this.waitForDOMContentLoaded();
268
+ }
269
+ })
270
+ .then(() => loader.load(bootstrapModule))
271
+ .catch((reason) => {
272
+ this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
273
+ });
274
+ }
275
+ // Trigger bootstrap error state, and call error handler if registered
276
+ enterErrorState(error) {
277
+ logOperationStart({ id: BOOTSTRAP_ERROR });
278
+ if (this.errorHandler) {
279
+ this.errorHandler(error);
280
+ }
281
+ else {
282
+ if (hasConsole) {
283
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
284
+ console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
285
+ }
286
+ }
287
+ }
288
+ // eslint-disable-next-line no-undef, lwr/no-unguarded-apis
289
+ startWatchdogTimer() {
290
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
291
+ return setTimeout(() => {
292
+ this.enterErrorState(new Error('Failed to load required modules - timed out'));
293
+ }, REQUIRED_MODULES_TIMEOUT);
294
+ }
295
+ }
296
+
297
+ // The loader module is ALWAYS required
298
+ const GLOBAL = globalThis;
299
+ GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
300
+ if (GLOBAL.LWR.requiredModules.indexOf('lwr/loaderLegacy/v/0_6_0-alpha_14') < 0) {
301
+ GLOBAL.LWR.requiredModules.push('lwr/loaderLegacy/v/0_6_0-alpha_14');
302
+ }
303
+ new LoaderShim(GLOBAL);
241
304
 
242
305
  }());
243
306
 
244
- LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports) { 'use strict';
307
+ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_14', ['exports'], function (exports) { 'use strict';
245
308
 
246
309
  const templateRegex = /\{([0-9]+)\}/g;
247
310
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -639,14 +702,22 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
639
702
  }
640
703
  }
641
704
 
705
+ // Bootstrap / shim
706
+
707
+ const LOADER_PREFIX = 'lwr.loader.';
708
+ const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
709
+ const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
710
+ const MODULE_ERROR = `${LOADER_PREFIX}module.error`; // Loader: mappings
711
+
642
712
  /* global console,process */
643
713
  class ModuleRegistry {
644
- constructor(baseUrl) {
714
+ constructor(config) {
645
715
  // A registry for named AMD defines containing the *metadata* of AMD module
646
716
  this.namedDefineRegistry = new Map();
647
717
  // The evaluted module registry where the module identifier (name or URL?) is the key
648
718
  this.moduleRegistry = new Map();
649
- this.baseUrl = baseUrl;
719
+ this.baseUrl = config.baseUrl;
720
+ this.profiler = config.profiler;
650
721
  }
651
722
  async load(id, importer) {
652
723
  const resolvedId = await this.resolve(id, importer);
@@ -761,6 +832,7 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
761
832
  // if module is "external", resolve the external promise to notify any dependees
762
833
  mod.external.resolveExternal(moduleDef);
763
834
  }
835
+ this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
764
836
  this.namedDefineRegistry.set(name, moduleDef);
765
837
  this.lastDefine = moduleDef;
766
838
  // Check signatures of dependencies against those in the namedDefineRegistry
@@ -1031,6 +1103,8 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
1031
1103
  return moduleDef;
1032
1104
  }
1033
1105
  const parentUrl = this.baseUrl; // only support baseUrl for now
1106
+ const specifier = moduleName || originalId;
1107
+ this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
1034
1108
  return Promise.resolve()
1035
1109
  .then(async () => {
1036
1110
  const loadHooks = this.loadHook;
@@ -1068,9 +1142,11 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
1068
1142
  if (!moduleDef) {
1069
1143
  throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
1070
1144
  }
1145
+ this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
1071
1146
  return moduleDef;
1072
1147
  })
1073
1148
  .catch((e) => {
1149
+ this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
1074
1150
  throw e;
1075
1151
  });
1076
1152
  }
@@ -1298,7 +1374,10 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
1298
1374
  * The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
1299
1375
  */
1300
1376
  class Loader {
1301
- constructor(baseUrl) {
1377
+ constructor(config) {
1378
+ config = config || {};
1379
+ let baseUrl = config.baseUrl;
1380
+ let profiler = config.profiler;
1302
1381
  if (baseUrl) {
1303
1382
  // add a trailing slash, if it does not exist
1304
1383
  baseUrl = baseUrl.replace(/\/?$/, '/');
@@ -1310,10 +1389,23 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_13', ['exports'], function (exports)
1310
1389
  throw new LoaderError(NO_BASE_URL);
1311
1390
  }
1312
1391
  this.baseUrl = baseUrl;
1313
- this.registry = new ModuleRegistry(baseUrl);
1392
+ if (!profiler) {
1393
+ // default noop profiler
1394
+ profiler = {
1395
+ logOperationStart: () => {
1396
+ /* noop */
1397
+ },
1398
+ logOperationEnd: () => {
1399
+ /* noop */
1400
+ },
1401
+ };
1402
+ }
1403
+ this.registry = new ModuleRegistry({ baseUrl, profiler });
1404
+ // TODO: https://github.com/salesforce/lwr/issues/1087
1314
1405
  this.services = Object.freeze({
1315
1406
  addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
1316
1407
  handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
1408
+ appMetadata: config.appMetadata,
1317
1409
  });
1318
1410
  }
1319
1411
  /**