@lwrjs/loader 0.10.0-alpha.2 → 0.10.0-alpha.20
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.
- package/build/assets/prod/lwr-error-shim.js +2 -1
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.js +636 -256
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +6 -2
- package/build/assets/prod/lwr-loader-shim-legacy.js +150 -55
- package/build/assets/prod/lwr-loader-shim.bundle.js +583 -259
- package/build/assets/prod/lwr-loader-shim.bundle.min.js +6 -2
- package/build/assets/prod/lwr-loader-shim.js +149 -55
- package/build/bundle/prod/lwr/esmLoader/esmLoader.js +2 -1
- package/build/cjs/index.cjs +1 -1
- package/build/cjs/modules/lwr/esmLoader/importResolver.cjs +17 -0
- package/build/cjs/modules/lwr/esmLoader/importResolverLegacy.cjs +17 -0
- package/build/cjs/modules/lwr/loader/constants/constants.cjs +1 -1
- package/build/cjs/modules/lwr/loader/errors/reportError.cjs +1 -0
- package/build/cjs/modules/lwr/loader/hooks/moduleInvalidation.cjs +1 -0
- package/build/cjs/modules/lwr/loader/hooks/resolveAndLoadHook.cjs +3 -1
- package/build/cjs/modules/lwr/loader/moduleRegistry/importMetadataResolver.cjs +2 -0
- package/build/cjs/modules/lwr/loader/moduleRegistry/moduleRegistry.cjs +2 -0
- package/build/cjs/modules/lwr/loaderLegacy/constants/constants.cjs +1 -1
- package/build/cjs/modules/lwr/loaderLegacy/errors/reportError.cjs +1 -0
- package/build/cjs/modules/lwr/loaderLegacy/hooks/moduleInvalidation.cjs +1 -0
- package/build/cjs/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.cjs +3 -1
- package/build/cjs/modules/lwr/loaderLegacy/importMap/importMapResolver.cjs +1 -0
- package/build/cjs/modules/lwr/loaderLegacy/importResolver/importResolver.cjs +17 -0
- package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.cjs +3 -0
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/modules/lwr/esmLoader/esmLoader.js +45 -15
- package/build/modules/lwr/loader/loader.js +433 -203
- package/build/modules/lwr/loaderLegacy/loaderLegacy.js +485 -200
- package/package.json +16 -9
|
@@ -4,7 +4,7 @@
|
|
|
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.10.0-alpha.
|
|
7
|
+
/* LWR Module Loader Shim v0.10.0-alpha.20 */
|
|
8
8
|
(function () {
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
const BOOTSTRAP_PREFIX = 'lwr.bootstrap.';
|
|
13
13
|
const BOOTSTRAP_ERROR = `${BOOTSTRAP_PREFIX}error`;
|
|
14
14
|
|
|
15
|
-
var Phase
|
|
16
|
-
(function (Phase) {
|
|
15
|
+
var Phase = /*#__PURE__*/function (Phase) {
|
|
17
16
|
Phase[Phase["Start"] = 0] = "Start";
|
|
18
17
|
Phase[Phase["End"] = 1] = "End";
|
|
19
|
-
|
|
18
|
+
return Phase;
|
|
19
|
+
}(Phase || {});
|
|
20
20
|
// Attach a custom dispatcher
|
|
21
21
|
let customDispatcher;
|
|
22
22
|
function attachDispatcher(dispatcher) {
|
|
@@ -27,21 +27,46 @@
|
|
|
27
27
|
// e.g. JSDom (used in Jest) doesn't implement these
|
|
28
28
|
const perf = globalThis.performance;
|
|
29
29
|
const isPerfSupported = typeof perf !== 'undefined' && typeof perf.mark === 'function' && typeof perf.clearMarks === 'function' && typeof perf.measure === 'function' && typeof perf.clearMeasures === 'function';
|
|
30
|
+
function getMeasureName(id, specifier) {
|
|
31
|
+
return specifier ? `${id}-${specifier}` : id;
|
|
32
|
+
}
|
|
33
|
+
function getMarkName(id, specifier, specifierIndex) {
|
|
34
|
+
const measureName = getMeasureName(id, specifier);
|
|
35
|
+
return specifier && specifierIndex ? `${measureName}_${specifierIndex}` : measureName;
|
|
36
|
+
}
|
|
37
|
+
function getDetail(specifier, metadata) {
|
|
38
|
+
const detail = specifier || metadata ? {
|
|
39
|
+
...metadata
|
|
40
|
+
} : null;
|
|
41
|
+
if (detail && specifier) {
|
|
42
|
+
detail['specifier'] = specifier;
|
|
43
|
+
}
|
|
44
|
+
return detail;
|
|
45
|
+
}
|
|
30
46
|
|
|
31
47
|
// For marking request metrics
|
|
32
48
|
// Fallback to the Performance API if there is no custom dispatcher
|
|
33
49
|
function logOperationStart({
|
|
34
50
|
id,
|
|
35
|
-
specifier
|
|
51
|
+
specifier,
|
|
52
|
+
specifierIndex,
|
|
53
|
+
metadata
|
|
36
54
|
}) {
|
|
37
55
|
if (customDispatcher) {
|
|
38
56
|
customDispatcher({
|
|
39
57
|
id,
|
|
40
58
|
phase: Phase.Start,
|
|
41
|
-
specifier
|
|
59
|
+
specifier,
|
|
60
|
+
metadata
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (isPerfSupported) {
|
|
65
|
+
const markName = getMarkName(id, specifier, specifierIndex);
|
|
66
|
+
const detail = getDetail(specifier, metadata);
|
|
67
|
+
perf.mark(markName, {
|
|
68
|
+
detail
|
|
42
69
|
});
|
|
43
|
-
} else if (isPerfSupported) {
|
|
44
|
-
perf.mark(id + (specifier ? `.${specifier}` : ''));
|
|
45
70
|
}
|
|
46
71
|
}
|
|
47
72
|
|
|
@@ -50,19 +75,25 @@
|
|
|
50
75
|
/* istanbul ignore next */
|
|
51
76
|
function logOperationEnd({
|
|
52
77
|
id,
|
|
53
|
-
specifier
|
|
78
|
+
specifier,
|
|
79
|
+
specifierIndex,
|
|
80
|
+
metadata
|
|
54
81
|
}) {
|
|
55
82
|
if (customDispatcher) {
|
|
56
83
|
customDispatcher({
|
|
57
84
|
id,
|
|
58
85
|
phase: Phase.End,
|
|
59
|
-
specifier
|
|
86
|
+
specifier,
|
|
87
|
+
metadata
|
|
60
88
|
});
|
|
61
89
|
} else if (isPerfSupported) {
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
perf.measure(measureName,
|
|
90
|
+
const markName = getMarkName(id, specifier, specifierIndex);
|
|
91
|
+
const measureName = getMeasureName(id, specifier);
|
|
92
|
+
const detail = getDetail(specifier, metadata);
|
|
93
|
+
perf.measure(measureName, {
|
|
94
|
+
start: markName,
|
|
95
|
+
detail
|
|
96
|
+
});
|
|
66
97
|
|
|
67
98
|
// Clear the created mark and measure to avoid filling the performance entry buffer
|
|
68
99
|
// Even if they get deleted, existing PerformanceObservers preserve copies of the entries
|
|
@@ -71,18 +102,26 @@
|
|
|
71
102
|
}
|
|
72
103
|
}
|
|
73
104
|
|
|
74
|
-
function createLoader(
|
|
105
|
+
function createLoader(
|
|
106
|
+
name,
|
|
107
|
+
definition,
|
|
108
|
+
config,
|
|
109
|
+
externalModules,
|
|
110
|
+
) {
|
|
75
111
|
if (!definition || typeof definition[2] !== 'function') {
|
|
76
112
|
throw new Error(`Expected loader with specifier "${name}" to be a module`);
|
|
77
113
|
}
|
|
114
|
+
|
|
78
115
|
// Create a Loader instance
|
|
79
116
|
const exports = {};
|
|
80
117
|
definition[2].call(null, exports);
|
|
81
118
|
const { Loader } = exports;
|
|
82
119
|
const loader = new Loader(config);
|
|
120
|
+
|
|
83
121
|
if (externalModules && externalModules.length) {
|
|
84
122
|
loader.registerExternalModules(externalModules);
|
|
85
123
|
}
|
|
124
|
+
|
|
86
125
|
// Define the loader module with public API: { define, load, services }
|
|
87
126
|
const exporter = (exports) => {
|
|
88
127
|
Object.assign(exports, {
|
|
@@ -92,10 +131,11 @@
|
|
|
92
131
|
});
|
|
93
132
|
};
|
|
94
133
|
loader.define(name, ['exports'], exporter);
|
|
134
|
+
|
|
95
135
|
return loader;
|
|
96
136
|
}
|
|
97
137
|
|
|
98
|
-
const REQUIRED_MODULES_TIMEOUT =
|
|
138
|
+
const REQUIRED_MODULES_TIMEOUT = 60 * 1000; // 2m
|
|
99
139
|
|
|
100
140
|
// Check for errors with autoBoot and customInit
|
|
101
141
|
function validatePreInit(autoBoot, customInit) {
|
|
@@ -108,11 +148,18 @@
|
|
|
108
148
|
throw new Error('The customInit hook must not be defined when autoBoot is true');
|
|
109
149
|
}
|
|
110
150
|
}
|
|
151
|
+
|
|
111
152
|
// Process the customInit hook
|
|
112
|
-
function customInit(
|
|
153
|
+
function customInit(
|
|
154
|
+
config,
|
|
155
|
+
initializeApp,
|
|
156
|
+
define,
|
|
157
|
+
onBootstrapError,
|
|
158
|
+
) {
|
|
113
159
|
// Validate config
|
|
114
160
|
const { autoBoot, customInit } = config;
|
|
115
161
|
validatePreInit(autoBoot, customInit);
|
|
162
|
+
|
|
116
163
|
// Set up arguments and call the customInit hook, if available
|
|
117
164
|
if (customInit) {
|
|
118
165
|
const lwr = {
|
|
@@ -126,48 +173,74 @@
|
|
|
126
173
|
}
|
|
127
174
|
|
|
128
175
|
/* global document */
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
129
184
|
/* eslint-disable lwr/no-unguarded-apis */
|
|
130
185
|
const hasSetTimeout = typeof setTimeout === 'function';
|
|
131
186
|
const hasConsole = typeof console !== 'undefined';
|
|
132
187
|
/* eslint-enable lwr/no-unguarded-apis */
|
|
188
|
+
|
|
133
189
|
class LoaderShim {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
__init() {this.defineCache = {};}
|
|
195
|
+
__init2() {this.orderedDefs = [];}
|
|
196
|
+
|
|
197
|
+
// eslint-disable-line no-undef, lwr/no-unguarded-apis
|
|
198
|
+
|
|
199
|
+
constructor(global) {LoaderShim.prototype.__init.call(this);LoaderShim.prototype.__init2.call(this);
|
|
137
200
|
// Start watchdog timer
|
|
138
201
|
if (hasSetTimeout) {
|
|
139
202
|
this.watchdogTimerId = this.startWatchdogTimer();
|
|
140
203
|
}
|
|
204
|
+
|
|
141
205
|
// Parse configuration
|
|
142
206
|
this.global = global;
|
|
143
|
-
this.config = global.LWR;
|
|
144
|
-
this.loaderSpecifier = 'lwr/loader/v/0_10_0-
|
|
207
|
+
this.config = global.LWR ;
|
|
208
|
+
this.loaderSpecifier = 'lwr/loader/v/0_10_0-alpha_20';
|
|
209
|
+
|
|
145
210
|
// Set up error handler
|
|
146
211
|
this.errorHandler = this.config.onError;
|
|
212
|
+
|
|
147
213
|
// Set up the temporary LWR.define function and customInit hook
|
|
148
214
|
const tempDefine = this.tempDefine.bind(this);
|
|
149
215
|
global.LWR.define = tempDefine;
|
|
150
216
|
this.bootReady = this.config.autoBoot;
|
|
217
|
+
|
|
151
218
|
try {
|
|
152
219
|
this.createProfilerModule(this.config);
|
|
153
|
-
customInit(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
220
|
+
customInit(
|
|
221
|
+
Object.freeze(this.config),
|
|
222
|
+
this.postCustomInit.bind(this),
|
|
223
|
+
tempDefine,
|
|
224
|
+
(e) => {
|
|
225
|
+
// customInit handlers can overwrite
|
|
226
|
+
// the error handler with onBootstrapError
|
|
227
|
+
this.errorHandler = e;
|
|
228
|
+
},
|
|
229
|
+
);
|
|
230
|
+
} catch (e) {
|
|
160
231
|
this.enterErrorState(e);
|
|
161
232
|
}
|
|
162
233
|
}
|
|
234
|
+
|
|
163
235
|
// Return true if the app can be initialized
|
|
164
|
-
|
|
236
|
+
canInit() {
|
|
165
237
|
// Initialize the app if:
|
|
166
238
|
// - bootReady: autoBoot is on OR customInit has finished
|
|
167
239
|
// - all required modules are defined
|
|
168
240
|
const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
|
|
169
241
|
return this.bootReady && allDefined;
|
|
170
242
|
}
|
|
243
|
+
|
|
171
244
|
/**
|
|
172
245
|
* Create a temporary LWR.define() function which captures all
|
|
173
246
|
* calls that occur BEFORE the full loader module is available
|
|
@@ -177,7 +250,7 @@
|
|
|
177
250
|
* - each moduleName is pushed onto an array, to preserve
|
|
178
251
|
* the order in which the modules were defined
|
|
179
252
|
*/
|
|
180
|
-
|
|
253
|
+
tempDefine(...args) {
|
|
181
254
|
// Cache the incoming module
|
|
182
255
|
const moduleName = args[0];
|
|
183
256
|
this.defineCache[moduleName] = args;
|
|
@@ -191,15 +264,17 @@
|
|
|
191
264
|
this.initApp();
|
|
192
265
|
}
|
|
193
266
|
}
|
|
267
|
+
|
|
194
268
|
// Called by the customInit hook via lwr.initializeApp()
|
|
195
|
-
|
|
269
|
+
postCustomInit() {
|
|
196
270
|
this.bootReady = true;
|
|
197
271
|
if (this.canInit()) {
|
|
198
272
|
this.initApp();
|
|
199
273
|
}
|
|
200
274
|
}
|
|
275
|
+
|
|
201
276
|
// Create the loader and initialize the application
|
|
202
|
-
|
|
277
|
+
initApp() {
|
|
203
278
|
try {
|
|
204
279
|
const loaderConfig = {
|
|
205
280
|
endpoints: this.config.endpoints,
|
|
@@ -213,18 +288,24 @@
|
|
|
213
288
|
rootComponents: this.config.rootComponents,
|
|
214
289
|
},
|
|
215
290
|
};
|
|
216
|
-
const loader = createLoader(
|
|
291
|
+
const loader = createLoader(
|
|
292
|
+
this.loaderSpecifier,
|
|
293
|
+
this.defineCache[this.loaderSpecifier],
|
|
294
|
+
loaderConfig,
|
|
295
|
+
this.config.preloadModules,
|
|
296
|
+
);
|
|
217
297
|
this.mountApp(loader);
|
|
218
|
-
}
|
|
219
|
-
catch (e) {
|
|
298
|
+
} catch (e) {
|
|
220
299
|
this.enterErrorState(e);
|
|
221
300
|
}
|
|
222
301
|
}
|
|
223
|
-
|
|
302
|
+
|
|
303
|
+
waitForDOMContentLoaded() {
|
|
224
304
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
225
305
|
if (typeof document === undefined) {
|
|
226
306
|
return Promise.resolve();
|
|
227
307
|
}
|
|
308
|
+
|
|
228
309
|
// Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
|
|
229
310
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
230
311
|
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
@@ -237,17 +318,21 @@
|
|
|
237
318
|
});
|
|
238
319
|
});
|
|
239
320
|
}
|
|
321
|
+
|
|
240
322
|
// Create a module out of the profiler
|
|
241
323
|
// Note: The profiler is also available as a module through lwc module resolution (see package.json)
|
|
242
|
-
|
|
324
|
+
createProfilerModule(globalLWR) {
|
|
243
325
|
const exporter = (exports) => {
|
|
244
326
|
Object.assign(exports, { logOperationStart, logOperationEnd });
|
|
245
327
|
};
|
|
246
|
-
globalLWR.define('lwr/profiler/v/0_10_0-
|
|
328
|
+
globalLWR.define('lwr/profiler/v/0_10_0-alpha_20', ['exports'], exporter);
|
|
247
329
|
}
|
|
330
|
+
|
|
248
331
|
// Set up the application globals, import map, root custom element...
|
|
249
|
-
|
|
250
|
-
const { bootstrapModule, rootComponent, rootComponents, ssrProps, endpoints, imports, index } =
|
|
332
|
+
mountApp(loader) {
|
|
333
|
+
const { bootstrapModule, rootComponent, rootComponents, ssrProps, endpoints, imports, index } =
|
|
334
|
+
this.config;
|
|
335
|
+
|
|
251
336
|
// Set global LWR.define to loader.define
|
|
252
337
|
this.global.LWR = Object.freeze({
|
|
253
338
|
define: loader.define.bind(loader),
|
|
@@ -258,42 +343,50 @@
|
|
|
258
343
|
imports: imports || {},
|
|
259
344
|
index: index || {},
|
|
260
345
|
});
|
|
346
|
+
|
|
261
347
|
// Redefine all modules in the temporary cache
|
|
262
348
|
this.orderedDefs.forEach((specifier) => {
|
|
263
349
|
if (specifier !== this.loaderSpecifier) {
|
|
264
350
|
loader.define(...this.defineCache[specifier]);
|
|
265
351
|
}
|
|
266
352
|
});
|
|
353
|
+
|
|
267
354
|
// by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
|
|
268
355
|
const { disableInitDefer } = this.config;
|
|
356
|
+
|
|
269
357
|
// Load the import mappings and application bootstrap module
|
|
270
358
|
loader
|
|
271
359
|
.registerImportMappings({ imports, index }, [bootstrapModule, rootComponent])
|
|
272
360
|
.then(() => {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
361
|
+
if (!disableInitDefer) {
|
|
362
|
+
return this.waitForDOMContentLoaded();
|
|
363
|
+
}
|
|
364
|
+
})
|
|
277
365
|
.then(() => loader.load(bootstrapModule))
|
|
278
366
|
.catch((reason) => {
|
|
279
|
-
|
|
280
|
-
|
|
367
|
+
this.enterErrorState(
|
|
368
|
+
new Error(
|
|
369
|
+
`Application ${rootComponent || bootstrapModule} could not be loaded: ${reason}`,
|
|
370
|
+
),
|
|
371
|
+
);
|
|
372
|
+
});
|
|
281
373
|
}
|
|
374
|
+
|
|
282
375
|
// Trigger bootstrap error state, and call error handler if registered
|
|
283
|
-
|
|
376
|
+
enterErrorState(error) {
|
|
284
377
|
logOperationStart({ id: BOOTSTRAP_ERROR });
|
|
285
378
|
if (this.errorHandler) {
|
|
286
379
|
this.errorHandler(error);
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
380
|
+
} else {
|
|
289
381
|
if (hasConsole) {
|
|
290
382
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
291
383
|
console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
|
|
292
384
|
}
|
|
293
385
|
}
|
|
294
386
|
}
|
|
387
|
+
|
|
295
388
|
// eslint-disable-next-line no-undef, lwr/no-unguarded-apis
|
|
296
|
-
|
|
389
|
+
startWatchdogTimer() {
|
|
297
390
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
298
391
|
return setTimeout(() => {
|
|
299
392
|
this.enterErrorState(new Error('Failed to load required modules - timed out'));
|
|
@@ -302,16 +395,17 @@
|
|
|
302
395
|
}
|
|
303
396
|
|
|
304
397
|
// The loader module is ALWAYS required
|
|
305
|
-
const GLOBAL = globalThis;
|
|
398
|
+
const GLOBAL = globalThis ;
|
|
306
399
|
GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
|
|
307
|
-
if (GLOBAL.LWR.requiredModules.indexOf('lwr/loader/v/0_10_0-
|
|
308
|
-
GLOBAL.LWR.requiredModules.push('lwr/loader/v/0_10_0-
|
|
400
|
+
if (GLOBAL.LWR.requiredModules.indexOf('lwr/loader/v/0_10_0-alpha_20') < 0) {
|
|
401
|
+
GLOBAL.LWR.requiredModules.push('lwr/loader/v/0_10_0-alpha_20');
|
|
309
402
|
}
|
|
310
403
|
new LoaderShim(GLOBAL);
|
|
311
404
|
|
|
312
|
-
}()
|
|
405
|
+
})();
|
|
406
|
+
//# sourceMappingURL=lwr-loader-shim.js.map
|
|
313
407
|
|
|
314
|
-
LWR.define('lwr/loader/v/0_10_0-
|
|
408
|
+
LWR.define('lwr/loader/v/0_10_0-alpha_20', ['exports'], (function (exports) { 'use strict';
|
|
315
409
|
|
|
316
410
|
const templateRegex = /\{([0-9]+)\}/g;
|
|
317
411
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -326,17 +420,26 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
326
420
|
const message = Array.isArray(args) ? templateString(errorInfo.message, args) : errorInfo.message;
|
|
327
421
|
return `LWR${errorInfo.code}: ${message}`;
|
|
328
422
|
}
|
|
423
|
+
|
|
329
424
|
class LoaderError extends Error {
|
|
330
425
|
constructor(errorInfo, errorArgs) {
|
|
331
426
|
super();
|
|
332
427
|
this.message = generateErrorMessage(errorInfo, errorArgs);
|
|
333
428
|
}
|
|
334
429
|
}
|
|
430
|
+
|
|
335
431
|
function invariant(condition, errorInfo) {
|
|
336
432
|
if (!condition) {
|
|
337
433
|
throw new LoaderError(errorInfo);
|
|
338
434
|
}
|
|
339
435
|
}
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
340
443
|
const MISSING_NAME = Object.freeze({
|
|
341
444
|
code: 3000,
|
|
342
445
|
message: 'A module name is required.',
|
|
@@ -427,6 +530,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
427
530
|
level: 0,
|
|
428
531
|
message: 'Invalid import metadata: {0} {1}',
|
|
429
532
|
});
|
|
533
|
+
|
|
430
534
|
/* importMap errors */
|
|
431
535
|
Object.freeze({
|
|
432
536
|
code: 3011,
|
|
@@ -436,7 +540,9 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
436
540
|
|
|
437
541
|
/* eslint-disable lwr/no-unguarded-apis */
|
|
438
542
|
const hasDocument = typeof document !== 'undefined';
|
|
543
|
+
|
|
439
544
|
const hasSetTimeout = typeof setTimeout === 'function';
|
|
545
|
+
|
|
440
546
|
const hasConsole = typeof console !== 'undefined';
|
|
441
547
|
/* eslint-enable lwr/no-unguarded-apis */
|
|
442
548
|
|
|
@@ -444,7 +550,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
444
550
|
let baseUrl = undefined;
|
|
445
551
|
if (hasDocument) {
|
|
446
552
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
447
|
-
const baseEl = document.querySelector('base[href]');
|
|
553
|
+
const baseEl = document.querySelector('base[href]') ;
|
|
448
554
|
baseUrl = baseEl && baseEl.href;
|
|
449
555
|
}
|
|
450
556
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
@@ -456,8 +562,10 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
456
562
|
baseUrl = baseUrl.slice(0, lastSepIndex + 1);
|
|
457
563
|
}
|
|
458
564
|
}
|
|
565
|
+
|
|
459
566
|
return baseUrl;
|
|
460
567
|
}
|
|
568
|
+
|
|
461
569
|
/**
|
|
462
570
|
* Check if a string is a URL based on Common Internet Scheme Syntax
|
|
463
571
|
* https://www.ietf.org/rfc/rfc1738.txt
|
|
@@ -484,24 +592,26 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
484
592
|
function isUrl(url) {
|
|
485
593
|
return url.indexOf('://') !== -1;
|
|
486
594
|
}
|
|
595
|
+
|
|
487
596
|
// Borrowed and adapted from https://github.com/systemjs/systemjs/blob/master/src/common.js
|
|
488
597
|
// Resolves the first path segment relative to the second/parent URL
|
|
489
598
|
// eg: resolveIfNotPlainOrUrl('../test', 'http://www.site.com/one/two') => 'http://www.site.com/test'
|
|
490
599
|
// eg: resolveIfNotPlainOrUrl('./x/y/z', 'https://my.com/segment')).toBe('https://my.com/x/y/z')
|
|
491
600
|
function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
492
601
|
const backslashRegEx = /\\/g;
|
|
493
|
-
if (relUrl.indexOf('\\') !== -1)
|
|
494
|
-
relUrl = relUrl.replace(backslashRegEx, '/');
|
|
602
|
+
if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
|
|
495
603
|
// protocol-relative
|
|
496
604
|
if (relUrl[0] === '/' && relUrl[1] === '/') {
|
|
497
605
|
return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
|
|
498
606
|
}
|
|
499
607
|
// relative-url
|
|
500
|
-
else if (
|
|
501
|
-
(relUrl[
|
|
502
|
-
(relUrl[1] === '
|
|
503
|
-
|
|
504
|
-
|
|
608
|
+
else if (
|
|
609
|
+
(relUrl[0] === '.' &&
|
|
610
|
+
(relUrl[1] === '/' ||
|
|
611
|
+
(relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
|
|
612
|
+
(relUrl.length === 1 && (relUrl += '/')))) ||
|
|
613
|
+
relUrl[0] === '/'
|
|
614
|
+
) {
|
|
505
615
|
const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
|
|
506
616
|
let pathname;
|
|
507
617
|
if (parentUrl[parentProtocol.length + 1] === '/') {
|
|
@@ -509,21 +619,23 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
509
619
|
if (parentProtocol !== 'file:') {
|
|
510
620
|
pathname = parentUrl.slice(parentProtocol.length + 2);
|
|
511
621
|
pathname = pathname.slice(pathname.indexOf('/') + 1);
|
|
512
|
-
}
|
|
513
|
-
else {
|
|
622
|
+
} else {
|
|
514
623
|
pathname = parentUrl.slice(8);
|
|
515
624
|
}
|
|
516
|
-
}
|
|
517
|
-
else {
|
|
625
|
+
} else {
|
|
518
626
|
// resolving to :/ so pathname is the /... part
|
|
519
|
-
pathname = parentUrl.slice(
|
|
627
|
+
pathname = parentUrl.slice(
|
|
628
|
+
parentProtocol.length + (parentUrl[parentProtocol.length] === '/' ? 1 : 0),
|
|
629
|
+
);
|
|
520
630
|
}
|
|
521
|
-
|
|
522
|
-
|
|
631
|
+
|
|
632
|
+
if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
|
|
633
|
+
|
|
523
634
|
// join together and split for removal of .. and . segments
|
|
524
635
|
// looping the string instead of anything fancy for perf reasons
|
|
525
636
|
// '../../../../../z' resolved to 'x/y' is just 'z'
|
|
526
637
|
const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
|
|
638
|
+
|
|
527
639
|
const output = [];
|
|
528
640
|
let segmentIndex = -1;
|
|
529
641
|
for (let i = 0; i < segmented.length; i++) {
|
|
@@ -534,6 +646,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
534
646
|
segmentIndex = -1;
|
|
535
647
|
}
|
|
536
648
|
}
|
|
649
|
+
|
|
537
650
|
// new segment - check if it is relative
|
|
538
651
|
else if (segmented[i] === '.') {
|
|
539
652
|
// ../ segment
|
|
@@ -544,8 +657,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
544
657
|
// ./ segment
|
|
545
658
|
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
|
|
546
659
|
i += 1;
|
|
547
|
-
}
|
|
548
|
-
else {
|
|
660
|
+
} else {
|
|
549
661
|
// the start of a new segment as below
|
|
550
662
|
segmentIndex = i;
|
|
551
663
|
}
|
|
@@ -556,13 +668,14 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
556
668
|
}
|
|
557
669
|
}
|
|
558
670
|
// finish reading out the last segment
|
|
559
|
-
if (segmentIndex !== -1)
|
|
560
|
-
output.push(segmented.slice(segmentIndex));
|
|
671
|
+
if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
|
|
561
672
|
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
|
|
562
673
|
}
|
|
563
674
|
}
|
|
675
|
+
|
|
564
676
|
function resolveUrl(relUrl, parentUrl) {
|
|
565
|
-
const resolvedUrl =
|
|
677
|
+
const resolvedUrl =
|
|
678
|
+
resolveIfNotPlainOrUrl(relUrl, parentUrl) ||
|
|
566
679
|
(isUrl(relUrl) ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
|
|
567
680
|
return resolvedUrl;
|
|
568
681
|
}
|
|
@@ -575,6 +688,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
575
688
|
script.src = url;
|
|
576
689
|
return script;
|
|
577
690
|
}
|
|
691
|
+
|
|
578
692
|
let lastWindowError$1, lastWindowErrorUrl;
|
|
579
693
|
function loadModuleDef(url) {
|
|
580
694
|
return new Promise(function (resolve, reject) {
|
|
@@ -588,8 +702,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
588
702
|
document.head.removeChild(script);
|
|
589
703
|
if (lastWindowErrorUrl === url) {
|
|
590
704
|
reject(lastWindowError$1);
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
705
|
+
} else {
|
|
593
706
|
resolve();
|
|
594
707
|
}
|
|
595
708
|
});
|
|
@@ -598,6 +711,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
598
711
|
}
|
|
599
712
|
});
|
|
600
713
|
}
|
|
714
|
+
|
|
601
715
|
if (hasDocument) {
|
|
602
716
|
// When a script is executed, runtime errors are on the global/window scope which are NOT caught by the script's onerror handler.
|
|
603
717
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
@@ -621,44 +735,53 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
621
735
|
|
|
622
736
|
/* spec based import map resolver */
|
|
623
737
|
class ImportMetadataResolver {
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
738
|
+
// Default to empty mappings
|
|
739
|
+
__init() {this.importURICache = new Map();}
|
|
740
|
+
__init2() {this.pendingURICache = new Map();}
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
__init3() {this.loadMappingHooks = [];}
|
|
744
|
+
|
|
745
|
+
constructor(config, invalidationCallback) {ImportMetadataResolver.prototype.__init.call(this);ImportMetadataResolver.prototype.__init2.call(this);ImportMetadataResolver.prototype.__init3.call(this);
|
|
629
746
|
this.config = config;
|
|
630
747
|
this.invalidationCallback = invalidationCallback;
|
|
631
748
|
}
|
|
749
|
+
|
|
632
750
|
addLoadMappingHook(hook) {
|
|
633
751
|
this.loadMappingHooks.push(hook);
|
|
634
752
|
}
|
|
753
|
+
|
|
635
754
|
getMappingEndpoint() {
|
|
636
755
|
return this.config.endpoints && this.config.endpoints.uris
|
|
637
756
|
? this.config.endpoints.uris.mapping
|
|
638
757
|
: undefined;
|
|
639
758
|
}
|
|
759
|
+
|
|
640
760
|
getModifiersAsUrlParams() {
|
|
641
761
|
const modifiers = this.config.endpoints ? this.config.endpoints.modifiers : undefined;
|
|
762
|
+
|
|
642
763
|
if (!modifiers) {
|
|
643
764
|
// No modifiers return an empty string to append to the URL
|
|
644
765
|
return '';
|
|
645
|
-
}
|
|
646
|
-
else {
|
|
766
|
+
} else {
|
|
647
767
|
const qs = Object.keys(modifiers)
|
|
648
768
|
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(modifiers[key])}`)
|
|
649
769
|
.join('&');
|
|
650
770
|
return `?${qs}`;
|
|
651
771
|
}
|
|
652
772
|
}
|
|
773
|
+
|
|
653
774
|
buildMappingUrl(specifier) {
|
|
654
775
|
const mappingEndpoint = this.getMappingEndpoint();
|
|
655
776
|
const specifiers = encodeURIComponent(specifier);
|
|
656
777
|
const modifiers = this.getModifiersAsUrlParams();
|
|
657
778
|
return `${mappingEndpoint}${specifiers}${modifiers}`;
|
|
658
779
|
}
|
|
780
|
+
|
|
659
781
|
getBaseUrl() {
|
|
660
782
|
return this.config.baseUrl;
|
|
661
783
|
}
|
|
784
|
+
|
|
662
785
|
registerImportMappings(newImportMetadata, rootSpecifiers) {
|
|
663
786
|
if (!rootSpecifiers || rootSpecifiers.length === 0) {
|
|
664
787
|
const imports = newImportMetadata ? JSON.stringify(newImportMetadata) : 'undefined';
|
|
@@ -680,8 +803,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
680
803
|
const existing = this.importURICache.get(specifier);
|
|
681
804
|
if (!existing) {
|
|
682
805
|
this.saveImportURIRecord(specifier, uri, indexValue, rootSpecifiers.includes(specifier));
|
|
683
|
-
}
|
|
684
|
-
else {
|
|
806
|
+
} else {
|
|
685
807
|
const identity = indexValue || uri;
|
|
686
808
|
const existingIdentity = existing.identity || existing.uri;
|
|
687
809
|
if (existingIdentity !== identity) {
|
|
@@ -695,22 +817,24 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
695
817
|
});
|
|
696
818
|
}
|
|
697
819
|
}
|
|
820
|
+
|
|
698
821
|
// Get URL from the local cache or return undefiend
|
|
699
|
-
|
|
822
|
+
getURI(specifier) {
|
|
700
823
|
return this.importURICache.has(specifier)
|
|
701
824
|
? resolveUrl(this.importURICache.get(specifier).uri, this.getBaseUrl())
|
|
702
825
|
: undefined;
|
|
703
826
|
}
|
|
827
|
+
|
|
704
828
|
resolveLocal(specifier) {
|
|
705
829
|
const uri = this.getURI(specifier);
|
|
706
830
|
if (uri) {
|
|
707
831
|
return uri;
|
|
708
|
-
}
|
|
709
|
-
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
832
|
+
} else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
710
833
|
return specifier;
|
|
711
834
|
}
|
|
712
835
|
return undefined;
|
|
713
836
|
}
|
|
837
|
+
|
|
714
838
|
/**
|
|
715
839
|
* Resolves a the URI for a specified module. It will return the value in this order:
|
|
716
840
|
*
|
|
@@ -725,15 +849,14 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
725
849
|
let uri = this.getURI(specifier);
|
|
726
850
|
if (uri) {
|
|
727
851
|
return uri;
|
|
728
|
-
}
|
|
729
|
-
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
852
|
+
} else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
730
853
|
return specifier;
|
|
731
|
-
}
|
|
732
|
-
else {
|
|
854
|
+
} else {
|
|
733
855
|
const pending = this.pendingURICache.get(specifier);
|
|
734
856
|
if (pending) {
|
|
735
857
|
return pending;
|
|
736
858
|
}
|
|
859
|
+
|
|
737
860
|
this.config.profiler.logOperationStart({ id: MAPPINGS_FETCH, specifier });
|
|
738
861
|
const fetchMappingService = this.hasMappingHooks()
|
|
739
862
|
? this.evaluateMappingHooks
|
|
@@ -741,33 +864,37 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
741
864
|
const promise = fetchMappingService
|
|
742
865
|
.bind(this)(specifier)
|
|
743
866
|
.then((importMetadata) => {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
867
|
+
if (!importMetadata || !importMetadata.imports) {
|
|
868
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
869
|
+
}
|
|
870
|
+
this.registerImportMappings(importMetadata, [specifier]);
|
|
871
|
+
uri = this.getURI(specifier);
|
|
872
|
+
if (!uri) {
|
|
873
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
this.config.profiler.logOperationEnd({ id: MAPPINGS_FETCH, specifier });
|
|
877
|
+
return uri;
|
|
878
|
+
})
|
|
755
879
|
.finally(() => {
|
|
756
|
-
|
|
757
|
-
|
|
880
|
+
this.pendingURICache.delete(specifier);
|
|
881
|
+
});
|
|
882
|
+
|
|
758
883
|
this.pendingURICache.set(specifier, promise);
|
|
759
884
|
return promise;
|
|
760
885
|
}
|
|
761
886
|
}
|
|
762
|
-
|
|
887
|
+
|
|
888
|
+
hasMappingHooks() {
|
|
763
889
|
return this.loadMappingHooks.length > 0;
|
|
764
890
|
}
|
|
891
|
+
|
|
765
892
|
/**
|
|
766
893
|
* Evaluates mapping hooks. Returns first match. If all hooks return null call the mapping service.
|
|
767
894
|
* @param specifier Request module identifier
|
|
768
895
|
* @returns Import Metadata from the module root
|
|
769
896
|
*/
|
|
770
|
-
|
|
897
|
+
async evaluateMappingHooks(specifier) {
|
|
771
898
|
// Check with any registered loadMappingHooks
|
|
772
899
|
const loadMappingHooks = this.loadMappingHooks;
|
|
773
900
|
if (loadMappingHooks.length) {
|
|
@@ -782,13 +909,16 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
782
909
|
}
|
|
783
910
|
}
|
|
784
911
|
}
|
|
912
|
+
|
|
785
913
|
// If we still do not have a match call the mapping service
|
|
786
914
|
return this.fetchNewMappings(specifier);
|
|
787
915
|
}
|
|
788
|
-
|
|
916
|
+
|
|
917
|
+
async fetchNewMappings(specifier) {
|
|
789
918
|
if (typeof globalThis.fetch !== 'function') {
|
|
790
919
|
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
791
920
|
}
|
|
921
|
+
|
|
792
922
|
// TODO For module invalidation with bundles it is recommended we have to send back all loaded root specified
|
|
793
923
|
// to ensure we detect all conflicts.
|
|
794
924
|
const uri = resolveUrl(this.buildMappingUrl(specifier), this.getBaseUrl());
|
|
@@ -800,21 +930,21 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
800
930
|
return res
|
|
801
931
|
.json()
|
|
802
932
|
.then((ret) => {
|
|
803
|
-
|
|
804
|
-
|
|
933
|
+
return ret ;
|
|
934
|
+
})
|
|
805
935
|
.catch((err) => {
|
|
806
|
-
|
|
807
|
-
|
|
936
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
937
|
+
});
|
|
808
938
|
});
|
|
809
939
|
}
|
|
810
|
-
|
|
940
|
+
|
|
941
|
+
saveImportURIRecord(specifier, uri, identity, isRoot) {
|
|
811
942
|
if (!identity || uri === identity) {
|
|
812
943
|
this.importURICache.set(specifier, {
|
|
813
944
|
uri,
|
|
814
945
|
isRoot: isRoot,
|
|
815
946
|
});
|
|
816
|
-
}
|
|
817
|
-
else {
|
|
947
|
+
} else {
|
|
818
948
|
this.importURICache.set(specifier, {
|
|
819
949
|
uri,
|
|
820
950
|
identity,
|
|
@@ -827,11 +957,13 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
827
957
|
function reportError(error) {
|
|
828
958
|
// TODO eventually this should be configurable instrumentation to send this somewhere
|
|
829
959
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
830
|
-
if (hasConsole)
|
|
831
|
-
console.error(error);
|
|
960
|
+
if (hasConsole) console.error(error);
|
|
832
961
|
}
|
|
833
962
|
|
|
834
|
-
function evaluateHandleStaleModuleHooks(
|
|
963
|
+
function evaluateHandleStaleModuleHooks(
|
|
964
|
+
handleStaleModuleHooks,
|
|
965
|
+
hookArgs,
|
|
966
|
+
) {
|
|
835
967
|
const { name, oldUrl, newUrl } = hookArgs;
|
|
836
968
|
// keep evaluating hooks if return value is null
|
|
837
969
|
for (let i = 0; i < handleStaleModuleHooks.length; i++) {
|
|
@@ -841,39 +973,82 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
841
973
|
if (hookResult !== null) {
|
|
842
974
|
break;
|
|
843
975
|
}
|
|
844
|
-
}
|
|
845
|
-
catch (e) {
|
|
976
|
+
} catch (e) {
|
|
846
977
|
reportError(new LoaderError(STALE_HOOK_ERROR));
|
|
847
978
|
}
|
|
848
979
|
}
|
|
849
980
|
}
|
|
850
981
|
|
|
851
|
-
const MODULE_LOAD_TIMEOUT_TIMER =
|
|
982
|
+
const MODULE_LOAD_TIMEOUT_TIMER = 60 * 1000; // 1m
|
|
983
|
+
|
|
984
|
+
/*!
|
|
985
|
+
* Copyright (C) 2023 salesforce.com, inc.
|
|
986
|
+
*/
|
|
987
|
+
// @ts-ignore: Prevent cannot find name 'trustedTypes' error.
|
|
988
|
+
const SUPPORTS_TRUSTED_TYPES = typeof trustedTypes !== 'undefined';
|
|
989
|
+
function createTrustedTypesPolicy(name, options) {
|
|
990
|
+
// @ts-ignore: Prevent cannot find name 'trustedTypes' error.
|
|
991
|
+
return trustedTypes.createPolicy(name, options);
|
|
992
|
+
}
|
|
993
|
+
function createFallbackPolicy(_name, options) {
|
|
994
|
+
return options;
|
|
995
|
+
}
|
|
996
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
|
|
997
|
+
const createPolicy = SUPPORTS_TRUSTED_TYPES ? createTrustedTypesPolicy : createFallbackPolicy;
|
|
998
|
+
const policyOptions = {
|
|
999
|
+
createHTML(value) {
|
|
1000
|
+
return value;
|
|
1001
|
+
},
|
|
1002
|
+
createScript(value) {
|
|
1003
|
+
return value;
|
|
1004
|
+
},
|
|
1005
|
+
createScriptURL(value) {
|
|
1006
|
+
return value;
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
const trusted = createPolicy('trusted', policyOptions);
|
|
1010
|
+
/*! version: 0.19.4 */
|
|
852
1011
|
|
|
853
1012
|
/* global console,process */
|
|
1013
|
+
|
|
1014
|
+
|
|
854
1015
|
let lastWindowError;
|
|
855
1016
|
if (hasDocument) {
|
|
856
1017
|
globalThis.addEventListener('error', (evt) => {
|
|
857
1018
|
lastWindowError = evt.error;
|
|
858
1019
|
});
|
|
859
1020
|
}
|
|
1021
|
+
|
|
860
1022
|
if (process.env.NODE_ENV !== 'production') {
|
|
861
1023
|
if (!hasSetTimeout && hasConsole) {
|
|
862
1024
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
863
1025
|
console.warn('setTimeout API is not available, watchdog timer on load hook will not be set');
|
|
864
1026
|
}
|
|
865
1027
|
}
|
|
1028
|
+
|
|
866
1029
|
function isCustomResponse(response) {
|
|
867
|
-
return (
|
|
868
|
-
|
|
1030
|
+
return (
|
|
1031
|
+
Object.prototype.hasOwnProperty.call(response, 'data') &&
|
|
1032
|
+
!Object.prototype.hasOwnProperty.call(response, 'blob')
|
|
1033
|
+
);
|
|
869
1034
|
}
|
|
870
|
-
function isFetchResponse(
|
|
1035
|
+
function isFetchResponse(
|
|
1036
|
+
response,
|
|
1037
|
+
) {
|
|
871
1038
|
// if it quacks like a duck...
|
|
872
|
-
return typeof response.blob === 'function';
|
|
1039
|
+
return typeof (response ).blob === 'function';
|
|
873
1040
|
}
|
|
874
|
-
|
|
875
|
-
|
|
1041
|
+
|
|
1042
|
+
function isResponseAPromise(
|
|
1043
|
+
response
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
,
|
|
1048
|
+
) {
|
|
1049
|
+
return !!(response && (response ).then);
|
|
876
1050
|
}
|
|
1051
|
+
|
|
877
1052
|
async function evaluateLoadHookResponse(response, id) {
|
|
878
1053
|
return Promise.resolve().then(async () => {
|
|
879
1054
|
if (!response.status) {
|
|
@@ -882,36 +1057,41 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
882
1057
|
if (response.status !== 200) {
|
|
883
1058
|
throw new LoaderError(HTTP_FAIL_LOAD, [id, `${response.status}`]);
|
|
884
1059
|
}
|
|
1060
|
+
|
|
885
1061
|
const isResponse = isFetchResponse(response);
|
|
886
1062
|
let code;
|
|
887
1063
|
if (isCustomResponse(response)) {
|
|
888
1064
|
code = response.data;
|
|
889
|
-
}
|
|
890
|
-
else if (isResponse) {
|
|
1065
|
+
} else if (isResponse) {
|
|
891
1066
|
// handle fetch response
|
|
892
|
-
code = await response.text();
|
|
893
|
-
}
|
|
894
|
-
else {
|
|
1067
|
+
code = await (response ).text();
|
|
1068
|
+
} else {
|
|
895
1069
|
throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
|
|
896
1070
|
}
|
|
1071
|
+
|
|
897
1072
|
if (!code) {
|
|
898
1073
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
899
1074
|
}
|
|
1075
|
+
|
|
900
1076
|
code = `${code}\n//# sourceURL=${id}`; // append sourceURL for debugging
|
|
901
1077
|
try {
|
|
902
1078
|
// TODO eval source maps for debugging
|
|
903
|
-
eval(code);
|
|
904
|
-
}
|
|
905
|
-
catch (e) {
|
|
1079
|
+
eval(trusted.createScript(code));
|
|
1080
|
+
} catch (e) {
|
|
906
1081
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
907
1082
|
}
|
|
1083
|
+
|
|
908
1084
|
if (lastWindowError) {
|
|
909
1085
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
910
1086
|
}
|
|
911
1087
|
return true;
|
|
912
1088
|
});
|
|
913
1089
|
}
|
|
914
|
-
|
|
1090
|
+
|
|
1091
|
+
async function evaluateLoadHook(
|
|
1092
|
+
id,
|
|
1093
|
+
hookPromise,
|
|
1094
|
+
) {
|
|
915
1095
|
if (!hasSetTimeout) {
|
|
916
1096
|
return hookPromise;
|
|
917
1097
|
}
|
|
@@ -923,45 +1103,91 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
923
1103
|
}, MODULE_LOAD_TIMEOUT_TIMER);
|
|
924
1104
|
hookPromise
|
|
925
1105
|
.then((response) => {
|
|
926
|
-
|
|
927
|
-
|
|
1106
|
+
resolve(response);
|
|
1107
|
+
})
|
|
928
1108
|
.catch(() => {
|
|
929
|
-
|
|
930
|
-
|
|
1109
|
+
reject(new LoaderError(FAIL_HOOK_LOAD, [id]));
|
|
1110
|
+
})
|
|
931
1111
|
.finally(() => {
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1112
|
+
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
1113
|
+
clearTimeout(timer);
|
|
1114
|
+
});
|
|
935
1115
|
});
|
|
936
1116
|
}
|
|
937
1117
|
|
|
938
1118
|
/* global console,process */
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
939
1164
|
class ModuleRegistry {
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
// The evaluated module registry where the module identifier (name or URL?) is the key
|
|
944
|
-
this.moduleRegistry = new Map();
|
|
945
|
-
// Aliases of modules in the registry
|
|
946
|
-
this.aliases = new Map();
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
constructor(config) {ModuleRegistry.prototype.__init.call(this);ModuleRegistry.prototype.__init2.call(this);ModuleRegistry.prototype.__init3.call(this);
|
|
947
1168
|
this.profiler = config.profiler;
|
|
948
|
-
this.resolver = new ImportMetadataResolver(
|
|
1169
|
+
this.resolver = new ImportMetadataResolver(
|
|
1170
|
+
config,
|
|
1171
|
+
this.importMetadataInvalidationCallback.bind(this),
|
|
1172
|
+
);
|
|
949
1173
|
}
|
|
1174
|
+
|
|
950
1175
|
async load(id, importer) {
|
|
951
1176
|
const resolvedId = await this.resolve(id, importer);
|
|
952
1177
|
const moduleRecord = this.getModuleRecord(resolvedId, id);
|
|
953
1178
|
if (moduleRecord.evaluated) {
|
|
954
1179
|
return moduleRecord.module;
|
|
955
|
-
}
|
|
956
|
-
else {
|
|
1180
|
+
} else {
|
|
957
1181
|
if (!moduleRecord.evaluationPromise) {
|
|
958
1182
|
moduleRecord.evaluationPromise = this.topLevelEvaluation(moduleRecord);
|
|
959
1183
|
}
|
|
960
1184
|
return moduleRecord.evaluationPromise;
|
|
961
1185
|
}
|
|
962
1186
|
}
|
|
1187
|
+
|
|
963
1188
|
async resolve(id, importer) {
|
|
964
1189
|
const parentUrl = this.resolver.getBaseUrl(); // only support baseUrl for now
|
|
1190
|
+
|
|
965
1191
|
let resolved;
|
|
966
1192
|
let aliasedId = id;
|
|
967
1193
|
const resolveHooks = this.resolveHook;
|
|
@@ -974,6 +1200,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
974
1200
|
// eslint-disable-next-line no-await-in-loop
|
|
975
1201
|
result = isResponseAPromise(response) ? await response : response;
|
|
976
1202
|
}
|
|
1203
|
+
|
|
977
1204
|
// if result is not null, attempt resolution
|
|
978
1205
|
if (result !== null) {
|
|
979
1206
|
if (typeof result === 'string') {
|
|
@@ -984,6 +1211,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
984
1211
|
aliasedId = result; // the next hook will receive the new id
|
|
985
1212
|
continue;
|
|
986
1213
|
}
|
|
1214
|
+
|
|
987
1215
|
resolved =
|
|
988
1216
|
result && result.url && (resolveIfNotPlainOrUrl(result.url, parentUrl) || result.url);
|
|
989
1217
|
if (!resolved) {
|
|
@@ -993,27 +1221,32 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
993
1221
|
break;
|
|
994
1222
|
}
|
|
995
1223
|
}
|
|
1224
|
+
|
|
996
1225
|
if (aliasedId !== id) {
|
|
997
1226
|
// resolved module id is the aliased module if it has already been defined
|
|
998
1227
|
if (!resolved && this.namedDefineRegistry.has(aliasedId)) {
|
|
999
1228
|
return aliasedId;
|
|
1000
|
-
}
|
|
1001
|
-
else {
|
|
1229
|
+
} else {
|
|
1002
1230
|
id = aliasedId;
|
|
1003
1231
|
}
|
|
1004
1232
|
}
|
|
1005
1233
|
}
|
|
1234
|
+
|
|
1006
1235
|
if (!resolved) {
|
|
1007
1236
|
const resolvedOrPlain = resolveIfNotPlainOrUrl(id, parentUrl) || id;
|
|
1237
|
+
|
|
1008
1238
|
// if module registry already has named module the resolved id is the plain id
|
|
1009
1239
|
if (this.moduleRegistry.has(resolvedOrPlain)) {
|
|
1010
1240
|
return resolvedOrPlain;
|
|
1011
1241
|
}
|
|
1242
|
+
|
|
1012
1243
|
const resolvedUrl = this.resolver.resolveLocal(resolvedOrPlain);
|
|
1013
1244
|
if (resolvedUrl) {
|
|
1014
1245
|
// return the plain id if it is already defined && the resolvedUrl is NOT already in the module registry
|
|
1015
|
-
if (
|
|
1016
|
-
this.namedDefineRegistry.
|
|
1246
|
+
if (
|
|
1247
|
+
this.namedDefineRegistry.has(resolvedOrPlain) &&
|
|
1248
|
+
this.namedDefineRegistry.get(resolvedOrPlain).defined
|
|
1249
|
+
) {
|
|
1017
1250
|
const record = this.moduleRegistry.get(resolvedUrl);
|
|
1018
1251
|
if (!record || !this.aliases.has(resolvedOrPlain)) {
|
|
1019
1252
|
return resolvedOrPlain;
|
|
@@ -1021,13 +1254,13 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1021
1254
|
}
|
|
1022
1255
|
return resolvedUrl;
|
|
1023
1256
|
}
|
|
1257
|
+
|
|
1024
1258
|
if (this.namedDefineRegistry.has(resolvedOrPlain)) {
|
|
1025
1259
|
return resolvedOrPlain;
|
|
1026
1260
|
}
|
|
1027
1261
|
try {
|
|
1028
1262
|
resolved = await this.resolver.resolve(resolvedOrPlain);
|
|
1029
|
-
}
|
|
1030
|
-
catch (e) {
|
|
1263
|
+
} catch (e) {
|
|
1031
1264
|
// defer to error handling below for unresolved
|
|
1032
1265
|
}
|
|
1033
1266
|
}
|
|
@@ -1035,6 +1268,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1035
1268
|
if (this.namedDefineRegistry.has(id)) {
|
|
1036
1269
|
return id;
|
|
1037
1270
|
}
|
|
1271
|
+
|
|
1038
1272
|
throw new LoaderError(UNRESOLVED, [id]);
|
|
1039
1273
|
}
|
|
1040
1274
|
if (importer && isUrl(resolved)) {
|
|
@@ -1042,9 +1276,11 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1042
1276
|
}
|
|
1043
1277
|
return resolved;
|
|
1044
1278
|
}
|
|
1279
|
+
|
|
1045
1280
|
has(id) {
|
|
1046
1281
|
return this.moduleRegistry.has(id);
|
|
1047
1282
|
}
|
|
1283
|
+
|
|
1048
1284
|
define(name, dependencies, exporter) {
|
|
1049
1285
|
const mod = this.namedDefineRegistry.get(name);
|
|
1050
1286
|
// Don't allow redefining a module.
|
|
@@ -1056,6 +1292,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1056
1292
|
this.lastDefine = mod;
|
|
1057
1293
|
return;
|
|
1058
1294
|
}
|
|
1295
|
+
|
|
1059
1296
|
const moduleDef = {
|
|
1060
1297
|
name,
|
|
1061
1298
|
dependencies,
|
|
@@ -1066,10 +1303,12 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1066
1303
|
// if module is "external", resolve the external promise to notify any dependees
|
|
1067
1304
|
mod.external.resolveExternal(moduleDef);
|
|
1068
1305
|
}
|
|
1306
|
+
|
|
1069
1307
|
this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
|
|
1070
1308
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
1071
1309
|
this.lastDefine = moduleDef;
|
|
1072
1310
|
}
|
|
1311
|
+
|
|
1073
1312
|
/**
|
|
1074
1313
|
* Marks modules as "externally" loaded/provided, so that the loader does not attempt to fetch them.
|
|
1075
1314
|
*
|
|
@@ -1082,6 +1321,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1082
1321
|
let timer;
|
|
1083
1322
|
const moduleDefPromise = new Promise((resolve, reject) => {
|
|
1084
1323
|
resolveExternal = resolve;
|
|
1324
|
+
|
|
1085
1325
|
// watch the external for timeout
|
|
1086
1326
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
1087
1327
|
timer = setTimeout(() => {
|
|
@@ -1099,24 +1339,39 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1099
1339
|
moduleDefPromise,
|
|
1100
1340
|
},
|
|
1101
1341
|
};
|
|
1102
|
-
this.namedDefineRegistry.set(id, moduleDef);
|
|
1103
|
-
}
|
|
1104
|
-
else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1342
|
+
this.namedDefineRegistry.set(id, moduleDef );
|
|
1343
|
+
} else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1105
1344
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
1106
1345
|
console.warn(MODULE_ALREADY_LOADED.message, id);
|
|
1107
1346
|
}
|
|
1108
1347
|
});
|
|
1109
1348
|
}
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
// A registry for named AMD defines containing the *metadata* of AMD module
|
|
1353
|
+
__init() {this.namedDefineRegistry = new Map();}
|
|
1354
|
+
|
|
1355
|
+
// The evaluated module registry where the module identifier (name or URL?) is the key
|
|
1356
|
+
__init2() {this.moduleRegistry = new Map();}
|
|
1357
|
+
|
|
1358
|
+
// Aliases of modules in the registry
|
|
1359
|
+
__init3() {this.aliases = new Map();}
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1110
1363
|
getImportMetadataResolver() {
|
|
1111
1364
|
return this.resolver;
|
|
1112
1365
|
}
|
|
1366
|
+
|
|
1113
1367
|
// Returns an existing module record by the resolvedId or aliased id
|
|
1114
|
-
|
|
1368
|
+
getExistingModuleRecord(resolvedId, aliasId) {
|
|
1115
1369
|
const moduleRecord = this.moduleRegistry.get(resolvedId);
|
|
1116
1370
|
if (moduleRecord) {
|
|
1117
1371
|
this.storeModuleAlias(aliasId, resolvedId);
|
|
1118
1372
|
return moduleRecord;
|
|
1119
1373
|
}
|
|
1374
|
+
|
|
1120
1375
|
// Check if this is a known alias
|
|
1121
1376
|
if (resolvedId !== aliasId) {
|
|
1122
1377
|
const alias = this.aliases.get(aliasId);
|
|
@@ -1129,13 +1384,15 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1129
1384
|
}
|
|
1130
1385
|
return moduleRecord;
|
|
1131
1386
|
}
|
|
1132
|
-
|
|
1387
|
+
|
|
1388
|
+
getModuleRecord(resolvedId, id) {
|
|
1133
1389
|
// Look for an existing record
|
|
1134
1390
|
const existingRecord = this.getExistingModuleRecord(resolvedId, id);
|
|
1135
1391
|
if (existingRecord) {
|
|
1136
1392
|
// return existing
|
|
1137
1393
|
return existingRecord;
|
|
1138
1394
|
}
|
|
1395
|
+
|
|
1139
1396
|
// Create a new Module Record
|
|
1140
1397
|
const instantiation = this.getModuleDef(resolvedId, id);
|
|
1141
1398
|
const dependencyRecords = instantiation.then((moduleDef) => {
|
|
@@ -1143,15 +1400,17 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1143
1400
|
// get dep and filter out exports
|
|
1144
1401
|
const filtered = dependencies
|
|
1145
1402
|
.map((dep) => {
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
.filter((depRecord) => depRecord !== undefined);
|
|
1403
|
+
if (dep === 'exports') {
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
invariant(dep !== 'require', NO_AMD_REQUIRE);
|
|
1407
|
+
return this.getModuleDependencyRecord.call(this, dep);
|
|
1408
|
+
})
|
|
1409
|
+
.filter((depRecord) => depRecord !== undefined) ;
|
|
1410
|
+
|
|
1153
1411
|
return Promise.all(filtered);
|
|
1154
1412
|
});
|
|
1413
|
+
|
|
1155
1414
|
const newModuleRecord = {
|
|
1156
1415
|
id: resolvedId,
|
|
1157
1416
|
module: Object.create(null),
|
|
@@ -1160,16 +1419,17 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1160
1419
|
evaluated: false,
|
|
1161
1420
|
evaluationPromise: null,
|
|
1162
1421
|
};
|
|
1422
|
+
|
|
1163
1423
|
this.moduleRegistry.set(resolvedId, newModuleRecord);
|
|
1164
1424
|
this.storeModuleAlias(id, resolvedId);
|
|
1165
1425
|
return newModuleRecord;
|
|
1166
1426
|
}
|
|
1167
|
-
|
|
1427
|
+
|
|
1428
|
+
storeModuleAlias(aliasId, resolvedId) {
|
|
1168
1429
|
if (aliasId !== resolvedId) {
|
|
1169
1430
|
if (!this.aliases.has(aliasId)) {
|
|
1170
1431
|
this.aliases.set(aliasId, resolvedId);
|
|
1171
|
-
}
|
|
1172
|
-
else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1432
|
+
} else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1173
1433
|
// Warn the user if they were not aliasing to the resolvedId
|
|
1174
1434
|
const currentResolvedId = this.aliases.get(aliasId);
|
|
1175
1435
|
if (currentResolvedId !== resolvedId) {
|
|
@@ -1179,17 +1439,23 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1179
1439
|
}
|
|
1180
1440
|
}
|
|
1181
1441
|
}
|
|
1182
|
-
|
|
1442
|
+
|
|
1443
|
+
async getModuleDependencyRecord(dependency) {
|
|
1183
1444
|
const resolvedDepId = await this.resolve(dependency);
|
|
1184
1445
|
return this.getModuleRecord(resolvedDepId, dependency);
|
|
1185
1446
|
}
|
|
1447
|
+
|
|
1186
1448
|
// execute the "top-level code" (the code outside of functions) of a module
|
|
1187
|
-
|
|
1449
|
+
async topLevelEvaluation(moduleRecord) {
|
|
1188
1450
|
await this.instantiateAll(moduleRecord, {});
|
|
1189
1451
|
return this.evaluateModule(moduleRecord, {});
|
|
1190
1452
|
}
|
|
1453
|
+
|
|
1191
1454
|
// Returns a promise when a module and all of it's dependencies have finished instantiation
|
|
1192
|
-
|
|
1455
|
+
async instantiateAll(
|
|
1456
|
+
moduleRecord,
|
|
1457
|
+
instantiatedMap,
|
|
1458
|
+
) {
|
|
1193
1459
|
if (!instantiatedMap[moduleRecord.id]) {
|
|
1194
1460
|
instantiatedMap[moduleRecord.id] = true;
|
|
1195
1461
|
const dependencyModuleRecords = await moduleRecord.dependencyRecords;
|
|
@@ -1202,46 +1468,61 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1202
1468
|
}
|
|
1203
1469
|
}
|
|
1204
1470
|
}
|
|
1205
|
-
|
|
1471
|
+
|
|
1472
|
+
async evaluateModule(
|
|
1473
|
+
moduleRecord,
|
|
1474
|
+
evaluationMap,
|
|
1475
|
+
) {
|
|
1206
1476
|
const dependencyModuleRecords = await moduleRecord.dependencyRecords;
|
|
1207
1477
|
if (dependencyModuleRecords.length > 0) {
|
|
1208
1478
|
evaluationMap[moduleRecord.id] = true;
|
|
1209
1479
|
// evaluate dependencies first
|
|
1210
1480
|
await this.evaluateModuleDependencies(dependencyModuleRecords, evaluationMap);
|
|
1211
1481
|
}
|
|
1482
|
+
|
|
1212
1483
|
const { exporter, dependencies } = await moduleRecord.instantiation;
|
|
1213
1484
|
// The exports object automatically gets filled in by the exporter evaluation
|
|
1214
1485
|
const exports = {};
|
|
1215
|
-
const depsMapped = await Promise.all(
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1486
|
+
const depsMapped = await Promise.all(
|
|
1487
|
+
dependencies.map(async (dep) => {
|
|
1488
|
+
if (dep === 'exports') {
|
|
1489
|
+
return exports;
|
|
1490
|
+
}
|
|
1491
|
+
const resolvedDepId = await this.resolve(dep);
|
|
1492
|
+
|
|
1493
|
+
const moduleRecord = this.moduleRegistry.get(resolvedDepId) ;
|
|
1494
|
+
if (!moduleRecord) {
|
|
1495
|
+
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
const module = moduleRecord.module;
|
|
1499
|
+
|
|
1500
|
+
/**
|
|
1501
|
+
* Circular dependencies are handled properly when named exports are used,
|
|
1502
|
+
* however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
|
|
1503
|
+
*
|
|
1504
|
+
* The workaround below applies for circular dependencies (!moduleRecord.evaluated)
|
|
1505
|
+
*/
|
|
1506
|
+
if (!moduleRecord.evaluated) {
|
|
1507
|
+
return this.getCircularDependencyWrapper(module);
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
if (module) {
|
|
1511
|
+
return module.__defaultInterop ? module.default : module;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1222
1514
|
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
1223
|
-
}
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
* Circular dependencies are handled properly when named exports are used,
|
|
1227
|
-
* however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
|
|
1228
|
-
*
|
|
1229
|
-
* The workaround below applies for circular dependencies (!moduleRecord.evaluated)
|
|
1230
|
-
*/
|
|
1231
|
-
if (!moduleRecord.evaluated) {
|
|
1232
|
-
return this.getCircularDependencyWrapper(module);
|
|
1233
|
-
}
|
|
1234
|
-
if (module) {
|
|
1235
|
-
return module.__defaultInterop ? module.default : module;
|
|
1236
|
-
}
|
|
1237
|
-
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
1238
|
-
}));
|
|
1515
|
+
}),
|
|
1516
|
+
);
|
|
1517
|
+
|
|
1239
1518
|
// W-10029836 - In the case where we could be instantiating multiple graphs at the same time lets make sure the module have not already been evaluated
|
|
1240
1519
|
if (moduleRecord.evaluated) {
|
|
1241
1520
|
return moduleRecord.module;
|
|
1242
1521
|
}
|
|
1522
|
+
|
|
1243
1523
|
// evaluates the module function
|
|
1244
1524
|
let moduleDefault = exporter(...depsMapped);
|
|
1525
|
+
|
|
1245
1526
|
// value is returned from exporter, then we are not using named exports
|
|
1246
1527
|
if (moduleDefault !== undefined) {
|
|
1247
1528
|
moduleDefault = { default: moduleDefault };
|
|
@@ -1257,7 +1538,9 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1257
1538
|
Object.defineProperty(exports, '__useDefault', { value: true });
|
|
1258
1539
|
}
|
|
1259
1540
|
}
|
|
1541
|
+
|
|
1260
1542
|
const moduleExports = moduleDefault || exports;
|
|
1543
|
+
|
|
1261
1544
|
// update the module record
|
|
1262
1545
|
// copy over enumerable public methods to module
|
|
1263
1546
|
for (const key in moduleExports) {
|
|
@@ -1271,6 +1554,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1271
1554
|
},
|
|
1272
1555
|
});
|
|
1273
1556
|
}
|
|
1557
|
+
|
|
1274
1558
|
// copy non-enumerable to module
|
|
1275
1559
|
if (moduleExports.__useDefault) {
|
|
1276
1560
|
Object.defineProperty(moduleRecord.module, '__useDefault', { value: true });
|
|
@@ -1281,27 +1565,36 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1281
1565
|
if (moduleExports.__esModule) {
|
|
1282
1566
|
Object.defineProperty(moduleRecord.module, '__esModule', { value: true });
|
|
1283
1567
|
}
|
|
1568
|
+
|
|
1284
1569
|
moduleRecord.evaluated = true;
|
|
1285
1570
|
Object.freeze(moduleRecord.module);
|
|
1286
1571
|
return moduleRecord.module;
|
|
1287
1572
|
}
|
|
1573
|
+
|
|
1288
1574
|
// Determines if named exports module has only default export
|
|
1289
|
-
|
|
1290
|
-
return (
|
|
1575
|
+
isNamedExportDefaultOnly(exports) {
|
|
1576
|
+
return (
|
|
1577
|
+
exports !== undefined &&
|
|
1291
1578
|
Object.getOwnPropertyNames(exports).length === 2 &&
|
|
1292
1579
|
Object.prototype.hasOwnProperty.call(exports, 'default') &&
|
|
1293
|
-
Object.prototype.hasOwnProperty.call(exports, '__esModule')
|
|
1580
|
+
Object.prototype.hasOwnProperty.call(exports, '__esModule')
|
|
1581
|
+
);
|
|
1294
1582
|
}
|
|
1583
|
+
|
|
1295
1584
|
// Wrap the dependency in a function that can be called and detected by __circular__ property.
|
|
1296
1585
|
// The LWC engine checks for __circular__ to detect circular dependencies.
|
|
1297
|
-
|
|
1586
|
+
getCircularDependencyWrapper(module) {
|
|
1298
1587
|
const tmp = () => {
|
|
1299
1588
|
return module.__useDefault || module.__defaultInterop ? module.default : module;
|
|
1300
1589
|
};
|
|
1301
1590
|
tmp.__circular__ = true;
|
|
1302
1591
|
return tmp;
|
|
1303
1592
|
}
|
|
1304
|
-
|
|
1593
|
+
|
|
1594
|
+
async evaluateModuleDependencies(
|
|
1595
|
+
dependencyModuleRecords,
|
|
1596
|
+
evaluationMap,
|
|
1597
|
+
) {
|
|
1305
1598
|
for (let i = 0; i < dependencyModuleRecords.length; i++) {
|
|
1306
1599
|
const depRecord = dependencyModuleRecords[i];
|
|
1307
1600
|
if (!depRecord.evaluated && !evaluationMap[depRecord.id]) {
|
|
@@ -1311,15 +1604,17 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1311
1604
|
}
|
|
1312
1605
|
}
|
|
1313
1606
|
}
|
|
1314
|
-
|
|
1607
|
+
|
|
1608
|
+
async getModuleDef(resolvedId, originalId) {
|
|
1315
1609
|
// reset lastDefine
|
|
1316
1610
|
this.lastDefine = undefined;
|
|
1611
|
+
|
|
1317
1612
|
// the module name can be the resolved ID or the original ID if neither are URL's.
|
|
1318
1613
|
const moduleName = !isUrl(resolvedId)
|
|
1319
1614
|
? resolvedId
|
|
1320
1615
|
: originalId !== resolvedId
|
|
1321
|
-
|
|
1322
|
-
|
|
1616
|
+
? originalId
|
|
1617
|
+
: undefined;
|
|
1323
1618
|
let moduleDef = moduleName && this.namedDefineRegistry.get(moduleName);
|
|
1324
1619
|
if (moduleDef && moduleDef.external) {
|
|
1325
1620
|
return moduleDef.external.moduleDefPromise;
|
|
@@ -1332,67 +1627,75 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1332
1627
|
this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
|
|
1333
1628
|
return Promise.resolve()
|
|
1334
1629
|
.then(async () => {
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1630
|
+
const loadHooks = this.loadHook;
|
|
1631
|
+
if (loadHooks) {
|
|
1632
|
+
for (let i = 0; i < loadHooks.length; i++) {
|
|
1633
|
+
const loadHook = loadHooks[i];
|
|
1634
|
+
const response = loadHook(resolvedId, parentUrl);
|
|
1635
|
+
const result = (
|
|
1636
|
+
isResponseAPromise(response)
|
|
1637
|
+
? // eslint-disable-next-line no-await-in-loop
|
|
1638
|
+
await evaluateLoadHook(resolvedId, response)
|
|
1639
|
+
: response
|
|
1640
|
+
) ;
|
|
1641
|
+
if (result === undefined) {
|
|
1642
|
+
throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
|
|
1643
|
+
}
|
|
1644
|
+
if (result && result !== null) {
|
|
1645
|
+
return evaluateLoadHookResponse(result, resolvedId);
|
|
1646
|
+
}
|
|
1349
1647
|
}
|
|
1350
1648
|
}
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
})
|
|
1649
|
+
return false;
|
|
1650
|
+
})
|
|
1354
1651
|
.then((result) => {
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1652
|
+
if (result !== true && hasDocument) {
|
|
1653
|
+
return loadModuleDef(resolvedId);
|
|
1654
|
+
}
|
|
1655
|
+
})
|
|
1359
1656
|
.then(() => {
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
moduleDef
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1657
|
+
// Attempt to retrieve the module definition by name first
|
|
1658
|
+
moduleDef = moduleName && this.namedDefineRegistry.get(moduleName);
|
|
1659
|
+
|
|
1660
|
+
// Fallback to the last loader.define call
|
|
1661
|
+
if (!moduleDef) {
|
|
1662
|
+
moduleDef = this.lastDefine;
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
// This should not happen
|
|
1666
|
+
if (!moduleDef) {
|
|
1667
|
+
throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
|
|
1671
|
+
return moduleDef;
|
|
1672
|
+
})
|
|
1373
1673
|
.catch((e) => {
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1674
|
+
this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
|
|
1675
|
+
throw e;
|
|
1676
|
+
});
|
|
1377
1677
|
}
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1378
1682
|
addLoaderPlugin(hooks) {
|
|
1379
1683
|
if (typeof hooks !== 'object') {
|
|
1380
1684
|
throw new LoaderError(INVALID_HOOK);
|
|
1381
1685
|
}
|
|
1382
1686
|
const { loadModule: loadHook, resolveModule: resolveHook, loadMapping } = hooks;
|
|
1687
|
+
|
|
1383
1688
|
if (resolveHook) {
|
|
1384
1689
|
if (this.resolveHook) {
|
|
1385
1690
|
this.resolveHook.push(resolveHook);
|
|
1386
|
-
}
|
|
1387
|
-
else {
|
|
1691
|
+
} else {
|
|
1388
1692
|
this.resolveHook = [resolveHook];
|
|
1389
1693
|
}
|
|
1390
1694
|
}
|
|
1391
1695
|
if (loadHook) {
|
|
1392
1696
|
if (this.loadHook) {
|
|
1393
1697
|
this.loadHook.push(loadHook);
|
|
1394
|
-
}
|
|
1395
|
-
else {
|
|
1698
|
+
} else {
|
|
1396
1699
|
this.loadHook = [loadHook];
|
|
1397
1700
|
}
|
|
1398
1701
|
}
|
|
@@ -1400,7 +1703,8 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1400
1703
|
this.resolver.addLoadMappingHook(loadMapping);
|
|
1401
1704
|
}
|
|
1402
1705
|
}
|
|
1403
|
-
|
|
1706
|
+
|
|
1707
|
+
importMetadataInvalidationCallback({ name, oldUrl, newUrl }) {
|
|
1404
1708
|
const handleStaleModuleHooks = this.handleStaleModuleHook;
|
|
1405
1709
|
if (handleStaleModuleHooks) {
|
|
1406
1710
|
evaluateHandleStaleModuleHooks(handleStaleModuleHooks, {
|
|
@@ -1408,19 +1712,19 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1408
1712
|
oldUrl,
|
|
1409
1713
|
newUrl,
|
|
1410
1714
|
});
|
|
1411
|
-
}
|
|
1412
|
-
else {
|
|
1715
|
+
} else {
|
|
1413
1716
|
if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1414
1717
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
1415
1718
|
console.warn(`stale module detected ${name}, current URL:${oldUrl}, new URL:${newUrl}`);
|
|
1416
1719
|
}
|
|
1417
1720
|
}
|
|
1418
1721
|
}
|
|
1722
|
+
|
|
1723
|
+
|
|
1419
1724
|
registerHandleStaleModuleHook(handleStaleModule) {
|
|
1420
1725
|
if (this.handleStaleModuleHook) {
|
|
1421
1726
|
this.handleStaleModuleHook.push(handleStaleModule);
|
|
1422
|
-
}
|
|
1423
|
-
else {
|
|
1727
|
+
} else {
|
|
1424
1728
|
this.handleStaleModuleHook = [handleStaleModule];
|
|
1425
1729
|
}
|
|
1426
1730
|
}
|
|
@@ -1430,15 +1734,21 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1430
1734
|
* The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
|
|
1431
1735
|
*/
|
|
1432
1736
|
class Loader {
|
|
1737
|
+
|
|
1738
|
+
|
|
1739
|
+
|
|
1433
1740
|
constructor(config) {
|
|
1434
1741
|
let baseUrl = config.baseUrl;
|
|
1435
1742
|
const mappingEndpoint = config.endpoints ? config.endpoints.uris.mapping : undefined;
|
|
1436
1743
|
let profiler = config.profiler;
|
|
1744
|
+
|
|
1437
1745
|
if (!mappingEndpoint) {
|
|
1438
1746
|
throw new LoaderError(NO_MAPPING_URL);
|
|
1439
1747
|
}
|
|
1748
|
+
|
|
1440
1749
|
// add a trailing slash, if it does not exist
|
|
1441
1750
|
config.endpoints.uris.mapping = mappingEndpoint.replace(/\/?$/, '/');
|
|
1751
|
+
|
|
1442
1752
|
if (baseUrl) {
|
|
1443
1753
|
// add a trailing slash, if it does not exist
|
|
1444
1754
|
baseUrl = baseUrl.replace(/\/?$/, '/');
|
|
@@ -1449,6 +1759,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1449
1759
|
if (!baseUrl) {
|
|
1450
1760
|
throw new LoaderError(NO_BASE_URL);
|
|
1451
1761
|
}
|
|
1762
|
+
|
|
1452
1763
|
if (!profiler) {
|
|
1453
1764
|
// default noop profiler
|
|
1454
1765
|
profiler = {
|
|
@@ -1460,7 +1771,9 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1460
1771
|
},
|
|
1461
1772
|
};
|
|
1462
1773
|
}
|
|
1774
|
+
|
|
1463
1775
|
this.registry = new ModuleRegistry(Object.freeze({ endpoints: config.endpoints, baseUrl, profiler }));
|
|
1776
|
+
|
|
1464
1777
|
// TODO: W-10539691 - temp workaround for LWR-Java -- remove once appId is implemented there
|
|
1465
1778
|
if (config.appMetadata && !config.appMetadata.appId) {
|
|
1466
1779
|
// Parse the appId from the bootstrapModule
|
|
@@ -1469,6 +1782,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1469
1782
|
const appId = match && match[1];
|
|
1470
1783
|
config.appMetadata.appId = appId;
|
|
1471
1784
|
}
|
|
1785
|
+
|
|
1472
1786
|
// TODO: https://github.com/salesforce-experience-platform-emu/lwr/issues/1087
|
|
1473
1787
|
this.services = Object.freeze({
|
|
1474
1788
|
addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
|
|
@@ -1476,6 +1790,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1476
1790
|
appMetadata: config.appMetadata,
|
|
1477
1791
|
});
|
|
1478
1792
|
}
|
|
1793
|
+
|
|
1479
1794
|
/**
|
|
1480
1795
|
* Defines/registers a single named AMD module definition.
|
|
1481
1796
|
*
|
|
@@ -1488,14 +1803,18 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1488
1803
|
invariant(typeof name === 'string', MISSING_NAME);
|
|
1489
1804
|
let ctor = execute;
|
|
1490
1805
|
let deps = dependencies;
|
|
1806
|
+
|
|
1491
1807
|
// Convert no dependencies form `define('name', function(){}, {});` to: `define('name', [], function(){}, {})`
|
|
1492
1808
|
if (typeof deps === 'function') {
|
|
1493
1809
|
ctor = dependencies;
|
|
1494
1810
|
deps = [];
|
|
1495
1811
|
}
|
|
1812
|
+
|
|
1496
1813
|
invariant(Array.isArray(deps), INVALID_DEPS);
|
|
1497
|
-
|
|
1814
|
+
|
|
1815
|
+
this.registry.define(name, deps, ctor );
|
|
1498
1816
|
}
|
|
1817
|
+
|
|
1499
1818
|
/**
|
|
1500
1819
|
* Retrieves/loads a module, returning it from the registry if it exists and fetching it if it doesn't.
|
|
1501
1820
|
*
|
|
@@ -1507,6 +1826,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1507
1826
|
async load(id, importer) {
|
|
1508
1827
|
return this.registry.load(id, importer);
|
|
1509
1828
|
}
|
|
1829
|
+
|
|
1510
1830
|
/**
|
|
1511
1831
|
* Checks if a Module exists in the registry. Note, returns false even if the ModuleDefinition exists but the Module has not been instantiated yet (executed).
|
|
1512
1832
|
*
|
|
@@ -1516,6 +1836,7 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1516
1836
|
has(id) {
|
|
1517
1837
|
return this.registry.has(id);
|
|
1518
1838
|
}
|
|
1839
|
+
|
|
1519
1840
|
/**
|
|
1520
1841
|
* Resolves the module identifier or URL. Returns the module identifier if the moduleDefinition exists, or the full resolved URL if a URL is given.
|
|
1521
1842
|
*
|
|
@@ -1527,9 +1848,11 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1527
1848
|
async resolve(id, importer) {
|
|
1528
1849
|
return this.registry.resolve(id, importer);
|
|
1529
1850
|
}
|
|
1851
|
+
|
|
1530
1852
|
async registerImportMappings(mappings, rootSpecifiers) {
|
|
1531
1853
|
this.registry.getImportMetadataResolver().registerImportMappings(mappings, rootSpecifiers);
|
|
1532
1854
|
}
|
|
1855
|
+
|
|
1533
1856
|
/**
|
|
1534
1857
|
* Marks modules as "externally" loaded/provided (e.g. preloaded), so that the loader does not attempt to load them.
|
|
1535
1858
|
*
|
|
@@ -1544,4 +1867,5 @@ LWR.define('lwr/loader/v/0_10_0-alpha_2', ['exports'], function (exports) { 'use
|
|
|
1544
1867
|
|
|
1545
1868
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1546
1869
|
|
|
1547
|
-
});
|
|
1870
|
+
}));
|
|
1871
|
+
//# sourceMappingURL=lwr-loader-shim.bundle.js.map
|