@lwrjs/loader 0.6.0-alpha.11 → 0.6.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.
@@ -4,291 +4,303 @@
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 Module Loader Shim v0.6.0-alpha.11 */
7
+ /* LWR Module Loader Shim v0.6.0-alpha.15 */
8
8
  (function () {
9
- 'use strict';
9
+ 'use strict';
10
10
 
11
- // Bootstrap / shim
12
- const BOOTSTRAP_PREFIX = 'lwr.bootstrap.';
13
- const BOOTSTRAP_ERROR = `${BOOTSTRAP_PREFIX}error`;
11
+ // Bootstrap / shim
12
+ const BOOTSTRAP_PREFIX = 'lwr.bootstrap.';
13
+ const BOOTSTRAP_ERROR = `${BOOTSTRAP_PREFIX}error`;
14
14
 
15
- function createLoader(name, definition, config, externalModules) {
16
- if (!definition || typeof definition[2] !== 'function') {
17
- throw new Error(`Expected loader with specifier "${name}" to be a module`);
18
- }
19
- // Create a Loader instance
20
- const exports = {};
21
- definition[2].call(null, exports);
22
- const { Loader } = exports;
23
- const loader = new Loader(config);
24
- if (externalModules && externalModules.length) {
25
- loader.registerExternalModules(externalModules);
26
- }
27
- // Define the loader module with public API: { define, load, services }
28
- const exporter = (exports) => {
29
- Object.assign(exports, {
30
- define: loader.define.bind(loader),
31
- load: loader.load.bind(loader),
32
- services: loader.services,
33
- });
34
- };
35
- loader.define(name, ['exports'], exporter);
36
- return loader;
37
- }
15
+ var Phase;
38
16
 
39
- const REQUIRED_MODULES_TIMEOUT = 300 * 1000;
17
+ (function (Phase) {
18
+ Phase[Phase["Start"] = 0] = "Start";
19
+ Phase[Phase["End"] = 1] = "End";
20
+ })(Phase || (Phase = {}));
40
21
 
41
- var Phase;
42
- (function (Phase) {
43
- Phase[Phase["Start"] = 0] = "Start";
44
- Phase[Phase["End"] = 1] = "End";
45
- })(Phase || (Phase = {}));
46
- // Attach a custom dispatcher
47
- let customDispatcher;
48
- function attachDispatcher(dispatcher) {
49
- customDispatcher = dispatcher;
50
- }
51
- // Check if the Performance API is available
52
- // e.g. JSDom (used in Jest) doesn't implement these
53
- const perf = globalThis.performance;
54
- const isPerfSupported = typeof perf !== 'undefined' &&
55
- typeof perf.mark === 'function' &&
56
- typeof perf.clearMarks === 'function' &&
57
- typeof perf.measure === 'function' &&
58
- typeof perf.clearMeasures === 'function';
59
- // For marking request metrics
60
- // Fallback to the Performance API if there is no custom dispatcher
61
- function logOperationStart({ id, specifier }) {
62
- if (customDispatcher) {
63
- customDispatcher({ id, phase: Phase.Start, specifier });
64
- }
65
- else if (isPerfSupported) {
66
- perf.mark(id + (specifier ? `.${specifier}` : ''));
67
- }
68
- }
69
- // For measuring duration metrics
70
- // Fallback to the Performance API if there is no custom dispatcher
71
- /* istanbul ignore next */
72
- function logOperationEnd({ id, specifier }) {
73
- if (customDispatcher) {
74
- customDispatcher({ id, phase: Phase.End, specifier });
75
- }
76
- else if (isPerfSupported) {
77
- const suffix = specifier ? `.${specifier}` : '';
78
- const markName = id + suffix;
79
- const measureName = `${id}.duration${suffix}`;
80
- perf.measure(measureName, markName);
81
- // Clear the created mark and measure to avoid filling the performance entry buffer
82
- // Even if they get deleted, existing PerformanceObservers preserve copies of the entries
83
- perf.clearMarks(markName);
84
- perf.clearMeasures(measureName);
85
- }
86
- }
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
87
28
 
88
- // Check for errors with autoBoot and customInit
89
- function validatePreInit(autoBoot, customInit) {
90
- // If autoBoot === false, there must be a customInit hook
91
- if (!autoBoot && !customInit) {
92
- throw new Error('The customInit hook is required when autoBoot is false');
93
- }
94
- // If autoBoot === true, there must NOT be a customInit hook
95
- if (autoBoot && customInit) {
96
- throw new Error('The customInit hook must not be defined when autoBoot is true');
97
- }
98
- }
99
- // Process the customInit hook
100
- function customInit(config, initializeApp, define, onBootstrapError) {
101
- // Validate config
102
- const { autoBoot, customInit } = config;
103
- validatePreInit(autoBoot, customInit);
104
- // Set up arguments and call the customInit hook, if available
105
- if (customInit) {
106
- const lwr = {
107
- initializeApp,
108
- define,
109
- onBootstrapError,
110
- attachDispatcher,
111
- };
112
- customInit(lwr, config);
113
- }
114
- }
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
115
32
 
116
- /* global document */
117
- /* eslint-disable lwr/no-unguarded-apis */
118
- const hasSetTimeout = typeof setTimeout === 'function';
119
- const hasConsole = typeof console !== 'undefined';
120
- /* eslint-enable lwr/no-unguarded-apis */
121
- class LoaderShim {
122
- constructor(global) {
123
- this.defineCache = {};
124
- this.orderedDefs = [];
125
- // Parse configuration
126
- this.global = global;
127
- this.config = global.LWR;
128
- this.loaderSpecifier = 'lwr/loader/v/0_6_0-alpha_11';
129
- // Set up the temporary LWR.define function and customInit hook
130
- const tempDefine = this.tempDefine.bind(this);
131
- global.LWR.define = tempDefine;
132
- this.bootReady = this.config.autoBoot;
133
- // Start watchdog timer
134
- if (hasSetTimeout) {
135
- this.watchdogTimerId = this.startWatchdogTimer();
136
- }
137
- try {
138
- customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
139
- this.errorHandler = e;
140
- });
141
- }
142
- catch (e) {
143
- this.enterErrorState(e);
144
- }
145
- }
146
- // Return true if the app can be initialized
147
- canInit() {
148
- // Initialize the app if:
149
- // - bootReady: autoBoot is on OR customInit has finished
150
- // - all required modules are defined
151
- const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
152
- return this.bootReady && allDefined;
153
- }
154
- /**
155
- * Create a temporary LWR.define() function which captures all
156
- * calls that occur BEFORE the full loader module is available
157
- *
158
- * Each call to LWR.define() is stored in 2 ways:
159
- * - in a map as [moduleName, arguments] pairs
160
- * - each moduleName is pushed onto an array, to preserve
161
- * the order in which the modules were defined
162
- */
163
- tempDefine(...args) {
164
- // Cache the incoming module
165
- const moduleName = args[0];
166
- this.defineCache[moduleName] = args;
167
- this.orderedDefs.push(moduleName);
168
- if (this.canInit()) {
169
- if (hasSetTimeout) {
170
- // requiredModules are defined, clear watchdog timer
171
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
172
- clearTimeout(this.watchdogTimerId);
173
- }
174
- this.initApp();
175
- }
176
- }
177
- // Called by the customInit hook via lwr.initializeApp()
178
- postCustomInit() {
179
- this.bootReady = true;
180
- if (this.canInit()) {
181
- this.initApp();
182
- }
183
- }
184
- // Create the loader and initialize the application
185
- initApp() {
186
- try {
187
- const loaderConfig = {
188
- endpoints: this.config.endpoints,
189
- baseUrl: this.config.baseUrl,
190
- profiler: { logOperationStart, logOperationEnd },
191
- // TODO: can be removed following https://github.com/salesforce/lwr/issues/1087
192
- appMetadata: {
193
- bootstrapModule: this.config.bootstrapModule,
194
- rootComponent: this.config.rootComponent,
195
- rootComponents: this.config.rootComponents,
196
- },
197
- };
198
- const loader = createLoader(this.loaderSpecifier, this.defineCache[this.loaderSpecifier], loaderConfig, this.config.preloadModules);
199
- this.createProfilerModule(loader);
200
- this.mountApp(loader);
201
- }
202
- catch (e) {
203
- this.enterErrorState(e);
204
- }
205
- }
206
- waitForDOMContentLoaded() {
207
- // eslint-disable-next-line lwr/no-unguarded-apis
208
- if (typeof document === undefined) {
209
- return Promise.resolve();
210
- }
211
- // Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
212
- // eslint-disable-next-line lwr/no-unguarded-apis
213
- if (document.readyState === 'interactive' || document.readyState === 'complete') {
214
- return Promise.resolve();
215
- }
216
- return new Promise((resolve) => {
217
- // eslint-disable-next-line lwr/no-unguarded-apis
218
- document.addEventListener('DOMContentLoaded', () => {
219
- resolve();
220
- });
221
- });
222
- }
223
- // Create a module out of the profiler
224
- // Note: The profiler is also available as a module through lwc module resolution (see package.json)
225
- createProfilerModule(loader) {
226
- const exporter = (exports) => {
227
- Object.assign(exports, { logOperationStart, logOperationEnd });
228
- };
229
- loader.define('lwr/profiler/v/0_6_0-alpha_11', ['exports'], exporter);
230
- }
231
- // Set up the application globals, import map, root custom element...
232
- mountApp(loader) {
233
- const { bootstrapModule, rootComponent, rootComponents, endpoints, imports, index } = this.config;
234
- // Set global LWR.define to loader.define
235
- this.global.LWR = Object.freeze({
236
- define: loader.define.bind(loader),
237
- rootComponent,
238
- rootComponents,
239
- endpoints,
240
- imports: imports || {},
241
- index: index || {},
242
- });
243
- // Redefine all modules in the temporary cache
244
- this.orderedDefs.forEach((specifier) => {
245
- if (specifier !== this.loaderSpecifier) {
246
- loader.define(...this.defineCache[specifier]);
247
- }
248
- });
249
- // by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
250
- const { disableInitDefer } = this.config;
251
- // Load the import mappings and application bootstrap module
252
- loader
253
- .registerImportMappings({ imports, index }, [bootstrapModule, rootComponent])
254
- .then(() => {
255
- if (!disableInitDefer) {
256
- return this.waitForDOMContentLoaded();
257
- }
258
- })
259
- .then(() => loader.load(bootstrapModule))
260
- .catch((reason) => {
261
- this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
262
- });
263
- }
264
- // Trigger bootstrap error state, and call error handler if registered
265
- enterErrorState(error) {
266
- logOperationStart({ id: BOOTSTRAP_ERROR });
267
- if (this.errorHandler) {
268
- this.errorHandler(error);
269
- }
270
- else {
271
- if (hasConsole) {
272
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
273
- console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
274
- }
275
- }
276
- }
277
- // eslint-disable-next-line no-undef, lwr/no-unguarded-apis
278
- startWatchdogTimer() {
279
- // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
280
- return setTimeout(() => {
281
- this.enterErrorState(new Error('Failed to load required modules - timed out'));
282
- }, REQUIRED_MODULES_TIMEOUT);
283
- }
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}` : ''));
284
45
  }
46
+ } // For measuring duration metrics
47
+ // Fallback to the Performance API if there is no custom dispatcher
48
+
49
+ /* istanbul ignore next */
285
50
 
286
- // The loader module is ALWAYS required
287
- const GLOBAL = globalThis;
288
- GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
289
- if (GLOBAL.LWR.requiredModules.indexOf('lwr/loader/v/0_6_0-alpha_11') < 0) {
290
- GLOBAL.LWR.requiredModules.push('lwr/loader/v/0_6_0-alpha_11');
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);
291
70
  }
292
- 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
+ if (externalModules && externalModules.length) {
83
+ loader.registerExternalModules(externalModules);
84
+ }
85
+ // Define the loader module with public API: { define, load, services }
86
+ const exporter = (exports) => {
87
+ Object.assign(exports, {
88
+ define: loader.define.bind(loader),
89
+ load: loader.load.bind(loader),
90
+ services: loader.services,
91
+ });
92
+ };
93
+ loader.define(name, ['exports'], exporter);
94
+ return loader;
95
+ }
96
+
97
+ const REQUIRED_MODULES_TIMEOUT = 300 * 1000;
98
+
99
+ // Check for errors with autoBoot and customInit
100
+ function validatePreInit(autoBoot, customInit) {
101
+ // If autoBoot === false, there must be a customInit hook
102
+ if (!autoBoot && !customInit) {
103
+ throw new Error('The customInit hook is required when autoBoot is false');
104
+ }
105
+ // If autoBoot === true, there must NOT be a customInit hook
106
+ if (autoBoot && customInit) {
107
+ throw new Error('The customInit hook must not be defined when autoBoot is true');
108
+ }
109
+ }
110
+ // Process the customInit hook
111
+ function customInit(config, initializeApp, define, onBootstrapError) {
112
+ // Validate config
113
+ const { autoBoot, customInit } = config;
114
+ validatePreInit(autoBoot, customInit);
115
+ // Set up arguments and call the customInit hook, if available
116
+ if (customInit) {
117
+ const lwr = {
118
+ initializeApp,
119
+ define,
120
+ onBootstrapError,
121
+ attachDispatcher,
122
+ };
123
+ customInit(lwr, config);
124
+ }
125
+ }
126
+
127
+ /* global document */
128
+ /* eslint-disable lwr/no-unguarded-apis */
129
+ const hasSetTimeout = typeof setTimeout === 'function';
130
+ const hasConsole = typeof console !== 'undefined';
131
+ /* eslint-enable lwr/no-unguarded-apis */
132
+ class LoaderShim {
133
+ constructor(global) {
134
+ this.defineCache = {};
135
+ this.orderedDefs = [];
136
+ // Parse configuration
137
+ this.global = global;
138
+ this.config = global.LWR;
139
+ this.loaderSpecifier = 'lwr/loader/v/0_6_0-alpha_15';
140
+ // Set up the temporary LWR.define function and customInit hook
141
+ const tempDefine = this.tempDefine.bind(this);
142
+ global.LWR.define = tempDefine;
143
+ this.bootReady = this.config.autoBoot;
144
+ // Start watchdog timer
145
+ if (hasSetTimeout) {
146
+ this.watchdogTimerId = this.startWatchdogTimer();
147
+ }
148
+ try {
149
+ customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
150
+ this.errorHandler = e;
151
+ });
152
+ }
153
+ catch (e) {
154
+ this.enterErrorState(e);
155
+ }
156
+ }
157
+ // Return true if the app can be initialized
158
+ canInit() {
159
+ // Initialize the app if:
160
+ // - bootReady: autoBoot is on OR customInit has finished
161
+ // - all required modules are defined
162
+ const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
163
+ return this.bootReady && allDefined;
164
+ }
165
+ /**
166
+ * Create a temporary LWR.define() function which captures all
167
+ * calls that occur BEFORE the full loader module is available
168
+ *
169
+ * Each call to LWR.define() is stored in 2 ways:
170
+ * - in a map as [moduleName, arguments] pairs
171
+ * - each moduleName is pushed onto an array, to preserve
172
+ * the order in which the modules were defined
173
+ */
174
+ tempDefine(...args) {
175
+ // Cache the incoming module
176
+ const moduleName = args[0];
177
+ this.defineCache[moduleName] = args;
178
+ this.orderedDefs.push(moduleName);
179
+ if (this.canInit()) {
180
+ if (hasSetTimeout) {
181
+ // requiredModules are defined, clear watchdog timer
182
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
183
+ clearTimeout(this.watchdogTimerId);
184
+ }
185
+ this.initApp();
186
+ }
187
+ }
188
+ // Called by the customInit hook via lwr.initializeApp()
189
+ postCustomInit() {
190
+ this.bootReady = true;
191
+ if (this.canInit()) {
192
+ this.initApp();
193
+ }
194
+ }
195
+ // Create the loader and initialize the application
196
+ initApp() {
197
+ try {
198
+ const loaderConfig = {
199
+ endpoints: this.config.endpoints,
200
+ baseUrl: this.config.baseUrl,
201
+ profiler: { logOperationStart, logOperationEnd },
202
+ // TODO: can be removed following https://github.com/salesforce/lwr/issues/1087
203
+ appMetadata: {
204
+ appId: this.config.appId,
205
+ bootstrapModule: this.config.bootstrapModule,
206
+ rootComponent: this.config.rootComponent,
207
+ rootComponents: this.config.rootComponents,
208
+ },
209
+ };
210
+ const loader = createLoader(this.loaderSpecifier, this.defineCache[this.loaderSpecifier], 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_15', ['exports'], exporter);
242
+ }
243
+ // Set up the application globals, import map, root custom element...
244
+ mountApp(loader) {
245
+ const { bootstrapModule, rootComponent, rootComponents, endpoints, imports, index } = 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
+ endpoints,
252
+ imports: imports || {},
253
+ index: index || {},
254
+ });
255
+ // Redefine all modules in the temporary cache
256
+ this.orderedDefs.forEach((specifier) => {
257
+ if (specifier !== this.loaderSpecifier) {
258
+ loader.define(...this.defineCache[specifier]);
259
+ }
260
+ });
261
+ // by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
262
+ const { disableInitDefer } = this.config;
263
+ // Load the import mappings and application bootstrap module
264
+ loader
265
+ .registerImportMappings({ imports, index }, [bootstrapModule, rootComponent])
266
+ .then(() => {
267
+ if (!disableInitDefer) {
268
+ return this.waitForDOMContentLoaded();
269
+ }
270
+ })
271
+ .then(() => loader.load(bootstrapModule))
272
+ .catch((reason) => {
273
+ this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
274
+ });
275
+ }
276
+ // Trigger bootstrap error state, and call error handler if registered
277
+ enterErrorState(error) {
278
+ logOperationStart({ id: BOOTSTRAP_ERROR });
279
+ if (this.errorHandler) {
280
+ this.errorHandler(error);
281
+ }
282
+ else {
283
+ if (hasConsole) {
284
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
285
+ console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
286
+ }
287
+ }
288
+ }
289
+ // eslint-disable-next-line no-undef, lwr/no-unguarded-apis
290
+ startWatchdogTimer() {
291
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
292
+ return setTimeout(() => {
293
+ this.enterErrorState(new Error('Failed to load required modules - timed out'));
294
+ }, REQUIRED_MODULES_TIMEOUT);
295
+ }
296
+ }
297
+
298
+ // The loader module is ALWAYS required
299
+ const GLOBAL = globalThis;
300
+ GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
301
+ if (GLOBAL.LWR.requiredModules.indexOf('lwr/loader/v/0_6_0-alpha_15') < 0) {
302
+ GLOBAL.LWR.requiredModules.push('lwr/loader/v/0_6_0-alpha_15');
303
+ }
304
+ new LoaderShim(GLOBAL);
293
305
 
294
306
  }());
@@ -1 +1 @@
1
- class t{constructor(t){if(this.importURICache=new Map,t&&t.imports)for(const[r,e]of Object.entries(t.imports))if(r&&e){(Array.isArray(e)?e:[]).forEach((t=>{this.importURICache.set(t,r)}))}}resolve(t){return this.importURICache.get(t)}}class r{constructor(t){this.importURICache=t&&t.imports?t:{imports:{}}}legacyResolve(t){return this.importURICache.imports[t]}}let e,o,i;function n(n){e=n;const{imports:s,index:c,importMappings:p}=n;o=new t({imports:s,index:c}),i=new r(p)}function s(t,r){const n=function(t,r){let n;if(!o||!i)throw new Error("The ESM Loader was not initialized");if(n=o.resolve(t),n)return n;if(n=i.legacyResolve(t),n)return n;if(n=t,n.indexOf("://")<0&&!n.startsWith("/")){const{endpoints:o}=e;o&&o.uris&&o.uris.module&&(n=o.uris.module+encodeURIComponent(t),r&&(n+=`?importer=${encodeURIComponent(r)}`))}return n}(t,r);return import(n)}export{n as init,s as load};
1
+ class i{constructor(i){var t,e;this.importURICache=new Map,this.modifiers="",this.normalizeMetadata(i),this.mappingEndpoint=(null==i?void 0:i.importMappings)||null===(t=null==i?void 0:i.endpoints)||void 0===t?void 0:t.uris.mapping,(null===(e=null==i?void 0:i.endpoints)||void 0===e?void 0:e.modifiers)&&(this.modifiers=Object.entries(i.endpoints.modifiers).reduce(((i,[t,e])=>i+`${t}=${e}&`),"?"))}normalizeMetadata(i){if(i&&i.imports)for(const[t,e]of Object.entries(i.imports))if(t&&e){(Array.isArray(e)?e:[]).forEach((i=>{this.importURICache.set(i,t)}))}}async fetchMappings(i){const t=`${this.mappingEndpoint}${encodeURIComponent(i)}${this.modifiers}`,e=await globalThis.fetch(t);if(e.ok){const i=await e.json();this.normalizeMetadata(i)}}async resolve(i){let t=this.importURICache.get(i);return!t&&this.mappingEndpoint&&(await this.fetchMappings(i),t=this.importURICache.get(i)),t}}class t{constructor(i){this.importURICache=i&&i.imports?i:{imports:{}}}legacyResolve(i){return this.importURICache.imports[i]}}let e,o,n;function s(s){e=s;const{imports:r,index:a,importMappings:p,endpoints:c}=s;o=new i({imports:r,index:a,endpoints:c,importMappings:p}),n=new t(p)}async function r(i,t){const s=await async function(i,t){let s;if(!o||!n)throw new Error("The ESM Loader was not initialized");if(s=await o.resolve(i),s)return s;if(s=n.legacyResolve(i),s)return s;if(s=i,s.indexOf("://")<0&&!s.startsWith("/")){const{endpoints:o}=e;o&&o.uris&&o.uris.module&&(s=o.uris.module+encodeURIComponent(i),t&&(s+=`?importer=${encodeURIComponent(t)}`),o.modifiers&&(s+=Object.entries(o.modifiers).reduce(((i,[t,e])=>i+`${t}=${e}&`),t?"&":"?")))}return s}(i,t);return import(s)}export{s as init,r as load};
@@ -34,20 +34,20 @@ var resolver;
34
34
  var resolverLegacy;
35
35
  function init(config) {
36
36
  esmLoaderConfig = config;
37
- const {imports, index, importMappings} = config;
38
- resolver = new import_importResolver.default({imports, index});
37
+ const {imports, index, importMappings, endpoints} = config;
38
+ resolver = new import_importResolver.default({imports, index, endpoints, importMappings});
39
39
  resolverLegacy = new import_importResolverLegacy.default(importMappings);
40
40
  }
41
- function load(specifier, importer) {
42
- const uri = resolveUrl(specifier, importer);
41
+ async function load(specifier, importer) {
42
+ const uri = await resolveUrl(specifier, importer);
43
43
  return Promise.resolve().then(() => __toModule(require(uri)));
44
44
  }
45
- function resolveUrl(specifier, importer) {
45
+ async function resolveUrl(specifier, importer) {
46
46
  let uri;
47
47
  if (!resolver || !resolverLegacy) {
48
48
  throw new Error("The ESM Loader was not initialized");
49
49
  }
50
- uri = resolver.resolve(specifier);
50
+ uri = await resolver.resolve(specifier);
51
51
  if (uri) {
52
52
  return uri;
53
53
  }
@@ -63,6 +63,9 @@ function resolveUrl(specifier, importer) {
63
63
  if (importer) {
64
64
  uri += `?importer=${encodeURIComponent(importer)}`;
65
65
  }
66
+ if (endpoints.modifiers) {
67
+ uri += Object.entries(endpoints.modifiers).reduce((q, [k, v]) => q += `${k}=${v}&`, importer ? "&" : "?");
68
+ }
66
69
  }
67
70
  }
68
71
  return uri;
@@ -11,8 +11,16 @@ __export(exports, {
11
11
  default: () => importResolver_default
12
12
  });
13
13
  var ImportResolver = class {
14
- constructor(importMetada) {
14
+ constructor(config) {
15
15
  this.importURICache = new Map();
16
+ this.modifiers = "";
17
+ this.normalizeMetadata(config);
18
+ this.mappingEndpoint = config?.importMappings ? void 0 : config?.endpoints?.uris.mapping;
19
+ if (config?.endpoints?.modifiers) {
20
+ this.modifiers = Object.entries(config.endpoints.modifiers).reduce((q, [k, v]) => q += `${k}=${v}&`, "?");
21
+ }
22
+ }
23
+ normalizeMetadata(importMetada) {
16
24
  if (importMetada && importMetada.imports) {
17
25
  for (const [uri, value] of Object.entries(importMetada.imports)) {
18
26
  if (uri && value) {
@@ -24,8 +32,21 @@ var ImportResolver = class {
24
32
  }
25
33
  }
26
34
  }
27
- resolve(specifier) {
28
- return this.importURICache.get(specifier);
35
+ async fetchMappings(specifier) {
36
+ const mappingUri = `${this.mappingEndpoint}${encodeURIComponent(specifier)}${this.modifiers}`;
37
+ const res = await globalThis.fetch(mappingUri);
38
+ if (res.ok) {
39
+ const mappings = await res.json();
40
+ this.normalizeMetadata(mappings);
41
+ }
42
+ }
43
+ async resolve(specifier) {
44
+ let uri = this.importURICache.get(specifier);
45
+ if (!uri && this.mappingEndpoint) {
46
+ await this.fetchMappings(specifier);
47
+ uri = this.importURICache.get(specifier);
48
+ }
49
+ return uri;
29
50
  }
30
51
  };
31
52
  var importResolver_default = ImportResolver;
@@ -28,7 +28,7 @@ __export(exports, {
28
28
  });
29
29
  var import_messages = __toModule(require("../errors/messages"));
30
30
  var import_url = __toModule(require("../utils/url"));
31
- var import_metrics = __toModule(require("../../../../metrics"));
31
+ var import_metrics = __toModule(require("lwr/metrics"));
32
32
  var ImportMetadataResolver = class {
33
33
  constructor(config, invalidationCallback) {
34
34
  this.importURICache = new Map();