@lwrjs/everywhere 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/__generated_site_amd_modules__/1/application/amd/l/en-US/ai/amd-bootstrap/configuration/ci/-/-/s/981eca8f72a57fecd21a35a5dfee2751/config.js +10 -0
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/@lwrjs/app-service/amd-bootstrap/module/amd/v/0_10_0-alpha_20/s/c40e6caf07454ce13961de8ca76ab7f0/@lwrjs_app-service_amd-bootstrap_module_amd.js +18 -0
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwc/v/{2_38_1/s/8cf6b94d9c0b398c70c97fdca2759caa → 2_50_0/s/b40440e2e29d782101f05d6085a324e9}/lwc.js +560 -584
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/everywhereAmd/v/{0_10_0-alpha_2/s/c538071acf5bde58e816967a14c465c3 → 0_10_0-alpha_20/s/cb931ebef2b89dcf8ab51456e3a68864}/lwr_everywhereAmd.js +4 -4
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/init/v/0_10_0-alpha_20/s/5e6db37af51f0d23be4893abe5eeb913/lwr_init.js +163 -0
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/loader/v/{0_10_0-alpha_2/s/b9b0d37c87017e0845caa60b6f4b88f9 → 0_10_0-alpha_20/s/e768d044ed1b7b7babca2f39e1cbca58}/lwr_loader.js +105 -14
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/metrics/v/{0_10_0-alpha_2/s/3426e42c3e22ce07ab2adc62c4c523a0 → 0_10_0-alpha_20/s/7a802ee7a2430244c1f66bce2c2eafef}/lwr_metrics.js +14 -2
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/profiler/v/0_10_0-alpha_20/s/a152b8d35f12ca1b5147c5cd1ee155fb/lwr_profiler.js +102 -0
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/vault/v/{0_10_0-alpha_2 → 0_10_0-alpha_20}/s/c92abd8c1fec2d7eff62e4b097abbe14/lwr_vault.js +2 -2
- package/build/__generated_site_amd_modules__/1/resource/amd/lwr-error-shim.js/v/{0_10_0-alpha_2 → 0_10_0-alpha_20}/lwr-error-shim.js +2 -1
- package/build/__generated_site_amd_modules__/1/resource/amd/lwr-loader-shim.bundle.js/v/{0_10_0-alpha_2 → 0_10_0-alpha_20}/lwr-loader-shim.bundle.js +583 -259
- package/build/assets/amd/lwr-everywhere-debug.js +536 -278
- package/build/assets/amd/lwr-everywhere-min.js +6 -1
- package/build/assets/amd/lwr-everywhere.js +536 -278
- package/build/assets/core/lwr-everywhere-debug.js +534 -276
- package/build/assets/core/lwr-everywhere-min.js +6 -1
- package/build/assets/core/lwr-everywhere.js +534 -276
- package/build/assets/esm/lwr-everywhere-debug.js +10 -3
- package/build/assets/esm/lwr-everywhere-min.js +1 -1
- package/build/assets/esm/lwr-everywhere.js +10 -3
- package/build/generate.js +5 -10
- package/build/index.js +1 -1
- package/build/modules/lwr/everywhereAmd/everywhereAmd.d.ts +1 -1
- package/build/modules/lwr/host/host.html +1 -1
- package/build/modules/lwr/host/host.js +2 -2
- package/build/modules/lwr/setupLDS/setupLDS.js +8 -5
- package/build/modules/lwr/vault/vault.d.ts +1 -1
- package/build/utils.d.ts +1 -1
- package/package.json +15 -13
- package/src/generate.ts +7 -11
- package/src/index.ts +4 -1
- package/build/__generated_site_amd_modules__/1/application/amd/l/en-US/ai/amd-bootstrap/configuration/ci/-/-/s/e8b5228f499eea69a8f669aada1f8042/config.js +0 -10
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/@lwrjs/app-service/amd-bootstrap/module/amd/v/0_10_0-alpha_2/s/c40e6caf07454ce13961de8ca76ab7f0/@lwrjs_app-service_amd-bootstrap_module_amd.js +0 -18
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/init/v/0_10_0-alpha_2/s/206e6e0af6f019fdfd86d87d56331bc2/lwr_init.js +0 -97
- package/build/__generated_site_amd_modules__/1/module/amd/1/l/en-US/mi/lwr/profiler/v/0_10_0-alpha_2/s/e25f3b0b8b5c096acebf847d0a83ea62/lwr_profiler.js +0 -68
- package/build/modules/@salesforce/lds-default-luvio/lds-default-luvio.d.ts +0 -24
- package/build/modules/@salesforce/lds-default-luvio/lds-default-luvio.js +0 -60
- package/build/modules/lwr/setupNodeLDS/network.d.ts +0 -3
- package/build/modules/lwr/setupNodeLDS/network.js +0 -88
- package/build/modules/lwr/setupNodeLDS/setupNodeLDS.d.ts +0 -2
- package/build/modules/lwr/setupNodeLDS/setupNodeLDS.js +0 -14
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
function getConfiguration(options) {
|
|
10
10
|
const { format, server, apiVersion, apiPrefix, locale, bundle, debug } = options;
|
|
11
11
|
const thisUrl = new URL(import.meta.url);
|
|
12
|
+
|
|
12
13
|
// URLs
|
|
13
14
|
const ORIGIN = server || thisUrl.origin || '';
|
|
14
15
|
const PREFIX = `${apiPrefix}/${apiVersion}`;
|
|
@@ -17,9 +18,11 @@ function getConfiguration(options) {
|
|
|
17
18
|
const ENDPOINT = bundle
|
|
18
19
|
? `${ORIGIN}${PREFIX}/bundle/${format}/${POSTFIX}${BUNDLE_ID}-0/module/mi/`
|
|
19
20
|
: `${ORIGIN}${PREFIX}/module/${format}/${POSTFIX}/mi/`;
|
|
21
|
+
|
|
20
22
|
// Component specifiers and URIs
|
|
21
23
|
const BOOT_MODULE = `lwr/everywhere${format === 'esm' ? 'Esm' : 'Amd'}/v/0_0_1`;
|
|
22
24
|
const BOOT_URI = `${ENDPOINT}${encodeURIComponent(BOOT_MODULE)}/latest${debug ? '?debug' : ''}`;
|
|
25
|
+
|
|
23
26
|
// Client Bootstrap Config
|
|
24
27
|
const config = {
|
|
25
28
|
appId: 'lwre',
|
|
@@ -39,6 +42,7 @@ function getConfiguration(options) {
|
|
|
39
42
|
if (debug && config.endpoints) {
|
|
40
43
|
config.endpoints.modifiers = { debug: 'true' };
|
|
41
44
|
}
|
|
45
|
+
|
|
42
46
|
return config;
|
|
43
47
|
}
|
|
44
48
|
|
|
@@ -48,7 +52,7 @@ function getConfiguration(options) {
|
|
|
48
52
|
* SPDX-License-Identifier: MIT
|
|
49
53
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
50
54
|
*/
|
|
51
|
-
/* LWR Module Loader v0.10.0-alpha.
|
|
55
|
+
/* LWR Module Loader v0.10.0-alpha.20 */
|
|
52
56
|
const templateRegex = /\{([0-9]+)\}/g;
|
|
53
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
58
|
function templateString(template, args) {
|
|
@@ -62,17 +66,26 @@ function generateErrorMessage(errorInfo, args) {
|
|
|
62
66
|
const message = Array.isArray(args) ? templateString(errorInfo.message, args) : errorInfo.message;
|
|
63
67
|
return `LWR${errorInfo.code}: ${message}`;
|
|
64
68
|
}
|
|
69
|
+
|
|
65
70
|
class LoaderError extends Error {
|
|
66
71
|
constructor(errorInfo, errorArgs) {
|
|
67
72
|
super();
|
|
68
73
|
this.message = generateErrorMessage(errorInfo, errorArgs);
|
|
69
74
|
}
|
|
70
75
|
}
|
|
76
|
+
|
|
71
77
|
function invariant(condition, errorInfo) {
|
|
72
78
|
if (!condition) {
|
|
73
79
|
throw new LoaderError(errorInfo);
|
|
74
80
|
}
|
|
75
81
|
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
76
89
|
const MISSING_NAME = Object.freeze({
|
|
77
90
|
code: 3000,
|
|
78
91
|
message: 'A module name is required.',
|
|
@@ -163,6 +176,7 @@ const BAD_IMPORT_METADATA = Object.freeze({
|
|
|
163
176
|
level: 0,
|
|
164
177
|
message: 'Invalid import metadata: {0} {1}',
|
|
165
178
|
});
|
|
179
|
+
|
|
166
180
|
/* importMap errors */
|
|
167
181
|
Object.freeze({
|
|
168
182
|
code: 3011,
|
|
@@ -172,7 +186,9 @@ Object.freeze({
|
|
|
172
186
|
|
|
173
187
|
/* eslint-disable lwr/no-unguarded-apis */
|
|
174
188
|
const hasDocument = typeof document !== 'undefined';
|
|
189
|
+
|
|
175
190
|
const hasSetTimeout = typeof setTimeout === 'function';
|
|
191
|
+
|
|
176
192
|
const hasConsole = typeof console !== 'undefined';
|
|
177
193
|
/* eslint-enable lwr/no-unguarded-apis */
|
|
178
194
|
|
|
@@ -180,7 +196,7 @@ function getBaseUrl() {
|
|
|
180
196
|
let baseUrl = undefined;
|
|
181
197
|
if (hasDocument) {
|
|
182
198
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
183
|
-
const baseEl = document.querySelector('base[href]');
|
|
199
|
+
const baseEl = document.querySelector('base[href]') ;
|
|
184
200
|
baseUrl = baseEl && baseEl.href;
|
|
185
201
|
}
|
|
186
202
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
@@ -192,8 +208,10 @@ function getBaseUrl() {
|
|
|
192
208
|
baseUrl = baseUrl.slice(0, lastSepIndex + 1);
|
|
193
209
|
}
|
|
194
210
|
}
|
|
211
|
+
|
|
195
212
|
return baseUrl;
|
|
196
213
|
}
|
|
214
|
+
|
|
197
215
|
/**
|
|
198
216
|
* Check if a string is a URL based on Common Internet Scheme Syntax
|
|
199
217
|
* https://www.ietf.org/rfc/rfc1738.txt
|
|
@@ -220,24 +238,26 @@ function getBaseUrl() {
|
|
|
220
238
|
function isUrl(url) {
|
|
221
239
|
return url.indexOf('://') !== -1;
|
|
222
240
|
}
|
|
241
|
+
|
|
223
242
|
// Borrowed and adapted from https://github.com/systemjs/systemjs/blob/master/src/common.js
|
|
224
243
|
// Resolves the first path segment relative to the second/parent URL
|
|
225
244
|
// eg: resolveIfNotPlainOrUrl('../test', 'http://www.site.com/one/two') => 'http://www.site.com/test'
|
|
226
245
|
// eg: resolveIfNotPlainOrUrl('./x/y/z', 'https://my.com/segment')).toBe('https://my.com/x/y/z')
|
|
227
246
|
function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
228
247
|
const backslashRegEx = /\\/g;
|
|
229
|
-
if (relUrl.indexOf('\\') !== -1)
|
|
230
|
-
relUrl = relUrl.replace(backslashRegEx, '/');
|
|
248
|
+
if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
|
|
231
249
|
// protocol-relative
|
|
232
250
|
if (relUrl[0] === '/' && relUrl[1] === '/') {
|
|
233
251
|
return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
|
|
234
252
|
}
|
|
235
253
|
// relative-url
|
|
236
|
-
else if (
|
|
237
|
-
(relUrl[
|
|
238
|
-
(relUrl[1] === '
|
|
239
|
-
|
|
240
|
-
|
|
254
|
+
else if (
|
|
255
|
+
(relUrl[0] === '.' &&
|
|
256
|
+
(relUrl[1] === '/' ||
|
|
257
|
+
(relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
|
|
258
|
+
(relUrl.length === 1 && (relUrl += '/')))) ||
|
|
259
|
+
relUrl[0] === '/'
|
|
260
|
+
) {
|
|
241
261
|
const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
|
|
242
262
|
let pathname;
|
|
243
263
|
if (parentUrl[parentProtocol.length + 1] === '/') {
|
|
@@ -245,21 +265,23 @@ function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
|
245
265
|
if (parentProtocol !== 'file:') {
|
|
246
266
|
pathname = parentUrl.slice(parentProtocol.length + 2);
|
|
247
267
|
pathname = pathname.slice(pathname.indexOf('/') + 1);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
268
|
+
} else {
|
|
250
269
|
pathname = parentUrl.slice(8);
|
|
251
270
|
}
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
271
|
+
} else {
|
|
254
272
|
// resolving to :/ so pathname is the /... part
|
|
255
|
-
pathname = parentUrl.slice(
|
|
273
|
+
pathname = parentUrl.slice(
|
|
274
|
+
parentProtocol.length + (parentUrl[parentProtocol.length] === '/' ? 1 : 0),
|
|
275
|
+
);
|
|
256
276
|
}
|
|
257
|
-
|
|
258
|
-
|
|
277
|
+
|
|
278
|
+
if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
|
|
279
|
+
|
|
259
280
|
// join together and split for removal of .. and . segments
|
|
260
281
|
// looping the string instead of anything fancy for perf reasons
|
|
261
282
|
// '../../../../../z' resolved to 'x/y' is just 'z'
|
|
262
283
|
const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
|
|
284
|
+
|
|
263
285
|
const output = [];
|
|
264
286
|
let segmentIndex = -1;
|
|
265
287
|
for (let i = 0; i < segmented.length; i++) {
|
|
@@ -270,6 +292,7 @@ function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
|
270
292
|
segmentIndex = -1;
|
|
271
293
|
}
|
|
272
294
|
}
|
|
295
|
+
|
|
273
296
|
// new segment - check if it is relative
|
|
274
297
|
else if (segmented[i] === '.') {
|
|
275
298
|
// ../ segment
|
|
@@ -280,8 +303,7 @@ function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
|
280
303
|
// ./ segment
|
|
281
304
|
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
|
|
282
305
|
i += 1;
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
306
|
+
} else {
|
|
285
307
|
// the start of a new segment as below
|
|
286
308
|
segmentIndex = i;
|
|
287
309
|
}
|
|
@@ -292,13 +314,14 @@ function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
|
292
314
|
}
|
|
293
315
|
}
|
|
294
316
|
// finish reading out the last segment
|
|
295
|
-
if (segmentIndex !== -1)
|
|
296
|
-
output.push(segmented.slice(segmentIndex));
|
|
317
|
+
if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
|
|
297
318
|
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
|
|
298
319
|
}
|
|
299
320
|
}
|
|
321
|
+
|
|
300
322
|
function resolveUrl(relUrl, parentUrl) {
|
|
301
|
-
const resolvedUrl =
|
|
323
|
+
const resolvedUrl =
|
|
324
|
+
resolveIfNotPlainOrUrl(relUrl, parentUrl) ||
|
|
302
325
|
(isUrl(relUrl) ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
|
|
303
326
|
return resolvedUrl;
|
|
304
327
|
}
|
|
@@ -311,6 +334,7 @@ function createScript(url) {
|
|
|
311
334
|
script.src = url;
|
|
312
335
|
return script;
|
|
313
336
|
}
|
|
337
|
+
|
|
314
338
|
let lastWindowError$1, lastWindowErrorUrl;
|
|
315
339
|
function loadModuleDef(url) {
|
|
316
340
|
return new Promise(function (resolve, reject) {
|
|
@@ -324,8 +348,7 @@ function loadModuleDef(url) {
|
|
|
324
348
|
document.head.removeChild(script);
|
|
325
349
|
if (lastWindowErrorUrl === url) {
|
|
326
350
|
reject(lastWindowError$1);
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
351
|
+
} else {
|
|
329
352
|
resolve();
|
|
330
353
|
}
|
|
331
354
|
});
|
|
@@ -334,6 +357,7 @@ function loadModuleDef(url) {
|
|
|
334
357
|
}
|
|
335
358
|
});
|
|
336
359
|
}
|
|
360
|
+
|
|
337
361
|
if (hasDocument) {
|
|
338
362
|
// When a script is executed, runtime errors are on the global/window scope which are NOT caught by the script's onerror handler.
|
|
339
363
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
@@ -357,44 +381,53 @@ const MAPPINGS_ERROR = `${LOADER_PREFIX}mappings.error`;
|
|
|
357
381
|
|
|
358
382
|
/* spec based import map resolver */
|
|
359
383
|
class ImportMetadataResolver {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
384
|
+
// Default to empty mappings
|
|
385
|
+
__init() {this.importURICache = new Map();}
|
|
386
|
+
__init2() {this.pendingURICache = new Map();}
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
__init3() {this.loadMappingHooks = [];}
|
|
390
|
+
|
|
391
|
+
constructor(config, invalidationCallback) {ImportMetadataResolver.prototype.__init.call(this);ImportMetadataResolver.prototype.__init2.call(this);ImportMetadataResolver.prototype.__init3.call(this);
|
|
365
392
|
this.config = config;
|
|
366
393
|
this.invalidationCallback = invalidationCallback;
|
|
367
394
|
}
|
|
395
|
+
|
|
368
396
|
addLoadMappingHook(hook) {
|
|
369
397
|
this.loadMappingHooks.push(hook);
|
|
370
398
|
}
|
|
399
|
+
|
|
371
400
|
getMappingEndpoint() {
|
|
372
401
|
return this.config.endpoints && this.config.endpoints.uris
|
|
373
402
|
? this.config.endpoints.uris.mapping
|
|
374
403
|
: undefined;
|
|
375
404
|
}
|
|
405
|
+
|
|
376
406
|
getModifiersAsUrlParams() {
|
|
377
407
|
const modifiers = this.config.endpoints ? this.config.endpoints.modifiers : undefined;
|
|
408
|
+
|
|
378
409
|
if (!modifiers) {
|
|
379
410
|
// No modifiers return an empty string to append to the URL
|
|
380
411
|
return '';
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
412
|
+
} else {
|
|
383
413
|
const qs = Object.keys(modifiers)
|
|
384
414
|
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(modifiers[key])}`)
|
|
385
415
|
.join('&');
|
|
386
416
|
return `?${qs}`;
|
|
387
417
|
}
|
|
388
418
|
}
|
|
419
|
+
|
|
389
420
|
buildMappingUrl(specifier) {
|
|
390
421
|
const mappingEndpoint = this.getMappingEndpoint();
|
|
391
422
|
const specifiers = encodeURIComponent(specifier);
|
|
392
423
|
const modifiers = this.getModifiersAsUrlParams();
|
|
393
424
|
return `${mappingEndpoint}${specifiers}${modifiers}`;
|
|
394
425
|
}
|
|
426
|
+
|
|
395
427
|
getBaseUrl() {
|
|
396
428
|
return this.config.baseUrl;
|
|
397
429
|
}
|
|
430
|
+
|
|
398
431
|
registerImportMappings(newImportMetadata, rootSpecifiers) {
|
|
399
432
|
if (!rootSpecifiers || rootSpecifiers.length === 0) {
|
|
400
433
|
const imports = newImportMetadata ? JSON.stringify(newImportMetadata) : 'undefined';
|
|
@@ -416,8 +449,7 @@ class ImportMetadataResolver {
|
|
|
416
449
|
const existing = this.importURICache.get(specifier);
|
|
417
450
|
if (!existing) {
|
|
418
451
|
this.saveImportURIRecord(specifier, uri, indexValue, rootSpecifiers.includes(specifier));
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
452
|
+
} else {
|
|
421
453
|
const identity = indexValue || uri;
|
|
422
454
|
const existingIdentity = existing.identity || existing.uri;
|
|
423
455
|
if (existingIdentity !== identity) {
|
|
@@ -431,22 +463,24 @@ class ImportMetadataResolver {
|
|
|
431
463
|
});
|
|
432
464
|
}
|
|
433
465
|
}
|
|
466
|
+
|
|
434
467
|
// Get URL from the local cache or return undefiend
|
|
435
|
-
|
|
468
|
+
getURI(specifier) {
|
|
436
469
|
return this.importURICache.has(specifier)
|
|
437
470
|
? resolveUrl(this.importURICache.get(specifier).uri, this.getBaseUrl())
|
|
438
471
|
: undefined;
|
|
439
472
|
}
|
|
473
|
+
|
|
440
474
|
resolveLocal(specifier) {
|
|
441
475
|
const uri = this.getURI(specifier);
|
|
442
476
|
if (uri) {
|
|
443
477
|
return uri;
|
|
444
|
-
}
|
|
445
|
-
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
478
|
+
} else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
446
479
|
return specifier;
|
|
447
480
|
}
|
|
448
481
|
return undefined;
|
|
449
482
|
}
|
|
483
|
+
|
|
450
484
|
/**
|
|
451
485
|
* Resolves a the URI for a specified module. It will return the value in this order:
|
|
452
486
|
*
|
|
@@ -461,15 +495,14 @@ class ImportMetadataResolver {
|
|
|
461
495
|
let uri = this.getURI(specifier);
|
|
462
496
|
if (uri) {
|
|
463
497
|
return uri;
|
|
464
|
-
}
|
|
465
|
-
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
498
|
+
} else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
466
499
|
return specifier;
|
|
467
|
-
}
|
|
468
|
-
else {
|
|
500
|
+
} else {
|
|
469
501
|
const pending = this.pendingURICache.get(specifier);
|
|
470
502
|
if (pending) {
|
|
471
503
|
return pending;
|
|
472
504
|
}
|
|
505
|
+
|
|
473
506
|
this.config.profiler.logOperationStart({ id: MAPPINGS_FETCH, specifier });
|
|
474
507
|
const fetchMappingService = this.hasMappingHooks()
|
|
475
508
|
? this.evaluateMappingHooks
|
|
@@ -477,33 +510,37 @@ class ImportMetadataResolver {
|
|
|
477
510
|
const promise = fetchMappingService
|
|
478
511
|
.bind(this)(specifier)
|
|
479
512
|
.then((importMetadata) => {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
513
|
+
if (!importMetadata || !importMetadata.imports) {
|
|
514
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
515
|
+
}
|
|
516
|
+
this.registerImportMappings(importMetadata, [specifier]);
|
|
517
|
+
uri = this.getURI(specifier);
|
|
518
|
+
if (!uri) {
|
|
519
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
this.config.profiler.logOperationEnd({ id: MAPPINGS_FETCH, specifier });
|
|
523
|
+
return uri;
|
|
524
|
+
})
|
|
491
525
|
.finally(() => {
|
|
492
|
-
|
|
493
|
-
|
|
526
|
+
this.pendingURICache.delete(specifier);
|
|
527
|
+
});
|
|
528
|
+
|
|
494
529
|
this.pendingURICache.set(specifier, promise);
|
|
495
530
|
return promise;
|
|
496
531
|
}
|
|
497
532
|
}
|
|
498
|
-
|
|
533
|
+
|
|
534
|
+
hasMappingHooks() {
|
|
499
535
|
return this.loadMappingHooks.length > 0;
|
|
500
536
|
}
|
|
537
|
+
|
|
501
538
|
/**
|
|
502
539
|
* Evaluates mapping hooks. Returns first match. If all hooks return null call the mapping service.
|
|
503
540
|
* @param specifier Request module identifier
|
|
504
541
|
* @returns Import Metadata from the module root
|
|
505
542
|
*/
|
|
506
|
-
|
|
543
|
+
async evaluateMappingHooks(specifier) {
|
|
507
544
|
// Check with any registered loadMappingHooks
|
|
508
545
|
const loadMappingHooks = this.loadMappingHooks;
|
|
509
546
|
if (loadMappingHooks.length) {
|
|
@@ -518,13 +555,16 @@ class ImportMetadataResolver {
|
|
|
518
555
|
}
|
|
519
556
|
}
|
|
520
557
|
}
|
|
558
|
+
|
|
521
559
|
// If we still do not have a match call the mapping service
|
|
522
560
|
return this.fetchNewMappings(specifier);
|
|
523
561
|
}
|
|
524
|
-
|
|
562
|
+
|
|
563
|
+
async fetchNewMappings(specifier) {
|
|
525
564
|
if (typeof globalThis.fetch !== 'function') {
|
|
526
565
|
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
527
566
|
}
|
|
567
|
+
|
|
528
568
|
// TODO For module invalidation with bundles it is recommended we have to send back all loaded root specified
|
|
529
569
|
// to ensure we detect all conflicts.
|
|
530
570
|
const uri = resolveUrl(this.buildMappingUrl(specifier), this.getBaseUrl());
|
|
@@ -536,21 +576,21 @@ class ImportMetadataResolver {
|
|
|
536
576
|
return res
|
|
537
577
|
.json()
|
|
538
578
|
.then((ret) => {
|
|
539
|
-
|
|
540
|
-
|
|
579
|
+
return ret ;
|
|
580
|
+
})
|
|
541
581
|
.catch((err) => {
|
|
542
|
-
|
|
543
|
-
|
|
582
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
583
|
+
});
|
|
544
584
|
});
|
|
545
585
|
}
|
|
546
|
-
|
|
586
|
+
|
|
587
|
+
saveImportURIRecord(specifier, uri, identity, isRoot) {
|
|
547
588
|
if (!identity || uri === identity) {
|
|
548
589
|
this.importURICache.set(specifier, {
|
|
549
590
|
uri,
|
|
550
591
|
isRoot: isRoot,
|
|
551
592
|
});
|
|
552
|
-
}
|
|
553
|
-
else {
|
|
593
|
+
} else {
|
|
554
594
|
this.importURICache.set(specifier, {
|
|
555
595
|
uri,
|
|
556
596
|
identity,
|
|
@@ -563,11 +603,13 @@ class ImportMetadataResolver {
|
|
|
563
603
|
function reportError(error) {
|
|
564
604
|
// TODO eventually this should be configurable instrumentation to send this somewhere
|
|
565
605
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
566
|
-
if (hasConsole)
|
|
567
|
-
console.error(error);
|
|
606
|
+
if (hasConsole) console.error(error);
|
|
568
607
|
}
|
|
569
608
|
|
|
570
|
-
function evaluateHandleStaleModuleHooks(
|
|
609
|
+
function evaluateHandleStaleModuleHooks(
|
|
610
|
+
handleStaleModuleHooks,
|
|
611
|
+
hookArgs,
|
|
612
|
+
) {
|
|
571
613
|
const { name, oldUrl, newUrl } = hookArgs;
|
|
572
614
|
// keep evaluating hooks if return value is null
|
|
573
615
|
for (let i = 0; i < handleStaleModuleHooks.length; i++) {
|
|
@@ -577,39 +619,82 @@ function evaluateHandleStaleModuleHooks(handleStaleModuleHooks, hookArgs) {
|
|
|
577
619
|
if (hookResult !== null) {
|
|
578
620
|
break;
|
|
579
621
|
}
|
|
580
|
-
}
|
|
581
|
-
catch (e) {
|
|
622
|
+
} catch (e) {
|
|
582
623
|
reportError(new LoaderError(STALE_HOOK_ERROR));
|
|
583
624
|
}
|
|
584
625
|
}
|
|
585
626
|
}
|
|
586
627
|
|
|
587
|
-
const MODULE_LOAD_TIMEOUT_TIMER =
|
|
628
|
+
const MODULE_LOAD_TIMEOUT_TIMER = 60 * 1000; // 1m
|
|
629
|
+
|
|
630
|
+
/*!
|
|
631
|
+
* Copyright (C) 2023 salesforce.com, inc.
|
|
632
|
+
*/
|
|
633
|
+
// @ts-ignore: Prevent cannot find name 'trustedTypes' error.
|
|
634
|
+
const SUPPORTS_TRUSTED_TYPES = typeof trustedTypes !== 'undefined';
|
|
635
|
+
function createTrustedTypesPolicy(name, options) {
|
|
636
|
+
// @ts-ignore: Prevent cannot find name 'trustedTypes' error.
|
|
637
|
+
return trustedTypes.createPolicy(name, options);
|
|
638
|
+
}
|
|
639
|
+
function createFallbackPolicy(_name, options) {
|
|
640
|
+
return options;
|
|
641
|
+
}
|
|
642
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
|
|
643
|
+
const createPolicy = SUPPORTS_TRUSTED_TYPES ? createTrustedTypesPolicy : createFallbackPolicy;
|
|
644
|
+
const policyOptions = {
|
|
645
|
+
createHTML(value) {
|
|
646
|
+
return value;
|
|
647
|
+
},
|
|
648
|
+
createScript(value) {
|
|
649
|
+
return value;
|
|
650
|
+
},
|
|
651
|
+
createScriptURL(value) {
|
|
652
|
+
return value;
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
const trusted = createPolicy('trusted', policyOptions);
|
|
656
|
+
/*! version: 0.19.4 */
|
|
588
657
|
|
|
589
658
|
/* global console,process */
|
|
659
|
+
|
|
660
|
+
|
|
590
661
|
let lastWindowError;
|
|
591
662
|
if (hasDocument) {
|
|
592
663
|
globalThis.addEventListener('error', (evt) => {
|
|
593
664
|
lastWindowError = evt.error;
|
|
594
665
|
});
|
|
595
666
|
}
|
|
667
|
+
|
|
596
668
|
if (process.env.NODE_ENV !== 'production') {
|
|
597
669
|
if (!hasSetTimeout && hasConsole) {
|
|
598
670
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
599
671
|
console.warn('setTimeout API is not available, watchdog timer on load hook will not be set');
|
|
600
672
|
}
|
|
601
673
|
}
|
|
674
|
+
|
|
602
675
|
function isCustomResponse(response) {
|
|
603
|
-
return (
|
|
604
|
-
|
|
676
|
+
return (
|
|
677
|
+
Object.prototype.hasOwnProperty.call(response, 'data') &&
|
|
678
|
+
!Object.prototype.hasOwnProperty.call(response, 'blob')
|
|
679
|
+
);
|
|
605
680
|
}
|
|
606
|
-
function isFetchResponse(
|
|
681
|
+
function isFetchResponse(
|
|
682
|
+
response,
|
|
683
|
+
) {
|
|
607
684
|
// if it quacks like a duck...
|
|
608
|
-
return typeof response.blob === 'function';
|
|
685
|
+
return typeof (response ).blob === 'function';
|
|
609
686
|
}
|
|
610
|
-
|
|
611
|
-
|
|
687
|
+
|
|
688
|
+
function isResponseAPromise(
|
|
689
|
+
response
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
,
|
|
694
|
+
) {
|
|
695
|
+
return !!(response && (response ).then);
|
|
612
696
|
}
|
|
697
|
+
|
|
613
698
|
async function evaluateLoadHookResponse(response, id) {
|
|
614
699
|
return Promise.resolve().then(async () => {
|
|
615
700
|
if (!response.status) {
|
|
@@ -618,36 +703,41 @@ async function evaluateLoadHookResponse(response, id) {
|
|
|
618
703
|
if (response.status !== 200) {
|
|
619
704
|
throw new LoaderError(HTTP_FAIL_LOAD, [id, `${response.status}`]);
|
|
620
705
|
}
|
|
706
|
+
|
|
621
707
|
const isResponse = isFetchResponse(response);
|
|
622
708
|
let code;
|
|
623
709
|
if (isCustomResponse(response)) {
|
|
624
710
|
code = response.data;
|
|
625
|
-
}
|
|
626
|
-
else if (isResponse) {
|
|
711
|
+
} else if (isResponse) {
|
|
627
712
|
// handle fetch response
|
|
628
|
-
code = await response.text();
|
|
629
|
-
}
|
|
630
|
-
else {
|
|
713
|
+
code = await (response ).text();
|
|
714
|
+
} else {
|
|
631
715
|
throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
|
|
632
716
|
}
|
|
717
|
+
|
|
633
718
|
if (!code) {
|
|
634
719
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
635
720
|
}
|
|
721
|
+
|
|
636
722
|
code = `${code}\n//# sourceURL=${id}`; // append sourceURL for debugging
|
|
637
723
|
try {
|
|
638
724
|
// TODO eval source maps for debugging
|
|
639
|
-
eval(code);
|
|
640
|
-
}
|
|
641
|
-
catch (e) {
|
|
725
|
+
eval(trusted.createScript(code));
|
|
726
|
+
} catch (e) {
|
|
642
727
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
643
728
|
}
|
|
729
|
+
|
|
644
730
|
if (lastWindowError) {
|
|
645
731
|
throw new LoaderError(FAIL_LOAD, [id]);
|
|
646
732
|
}
|
|
647
733
|
return true;
|
|
648
734
|
});
|
|
649
735
|
}
|
|
650
|
-
|
|
736
|
+
|
|
737
|
+
async function evaluateLoadHook(
|
|
738
|
+
id,
|
|
739
|
+
hookPromise,
|
|
740
|
+
) {
|
|
651
741
|
if (!hasSetTimeout) {
|
|
652
742
|
return hookPromise;
|
|
653
743
|
}
|
|
@@ -659,45 +749,91 @@ async function evaluateLoadHook(id, hookPromise) {
|
|
|
659
749
|
}, MODULE_LOAD_TIMEOUT_TIMER);
|
|
660
750
|
hookPromise
|
|
661
751
|
.then((response) => {
|
|
662
|
-
|
|
663
|
-
|
|
752
|
+
resolve(response);
|
|
753
|
+
})
|
|
664
754
|
.catch(() => {
|
|
665
|
-
|
|
666
|
-
|
|
755
|
+
reject(new LoaderError(FAIL_HOOK_LOAD, [id]));
|
|
756
|
+
})
|
|
667
757
|
.finally(() => {
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
758
|
+
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
759
|
+
clearTimeout(timer);
|
|
760
|
+
});
|
|
671
761
|
});
|
|
672
762
|
}
|
|
673
763
|
|
|
674
764
|
/* global console,process */
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
675
810
|
class ModuleRegistry {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
// The evaluated module registry where the module identifier (name or URL?) is the key
|
|
680
|
-
this.moduleRegistry = new Map();
|
|
681
|
-
// Aliases of modules in the registry
|
|
682
|
-
this.aliases = new Map();
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
constructor(config) {ModuleRegistry.prototype.__init.call(this);ModuleRegistry.prototype.__init2.call(this);ModuleRegistry.prototype.__init3.call(this);
|
|
683
814
|
this.profiler = config.profiler;
|
|
684
|
-
this.resolver = new ImportMetadataResolver(
|
|
815
|
+
this.resolver = new ImportMetadataResolver(
|
|
816
|
+
config,
|
|
817
|
+
this.importMetadataInvalidationCallback.bind(this),
|
|
818
|
+
);
|
|
685
819
|
}
|
|
820
|
+
|
|
686
821
|
async load(id, importer) {
|
|
687
822
|
const resolvedId = await this.resolve(id, importer);
|
|
688
823
|
const moduleRecord = this.getModuleRecord(resolvedId, id);
|
|
689
824
|
if (moduleRecord.evaluated) {
|
|
690
825
|
return moduleRecord.module;
|
|
691
|
-
}
|
|
692
|
-
else {
|
|
826
|
+
} else {
|
|
693
827
|
if (!moduleRecord.evaluationPromise) {
|
|
694
828
|
moduleRecord.evaluationPromise = this.topLevelEvaluation(moduleRecord);
|
|
695
829
|
}
|
|
696
830
|
return moduleRecord.evaluationPromise;
|
|
697
831
|
}
|
|
698
832
|
}
|
|
833
|
+
|
|
699
834
|
async resolve(id, importer) {
|
|
700
835
|
const parentUrl = this.resolver.getBaseUrl(); // only support baseUrl for now
|
|
836
|
+
|
|
701
837
|
let resolved;
|
|
702
838
|
let aliasedId = id;
|
|
703
839
|
const resolveHooks = this.resolveHook;
|
|
@@ -710,6 +846,7 @@ class ModuleRegistry {
|
|
|
710
846
|
// eslint-disable-next-line no-await-in-loop
|
|
711
847
|
result = isResponseAPromise(response) ? await response : response;
|
|
712
848
|
}
|
|
849
|
+
|
|
713
850
|
// if result is not null, attempt resolution
|
|
714
851
|
if (result !== null) {
|
|
715
852
|
if (typeof result === 'string') {
|
|
@@ -720,6 +857,7 @@ class ModuleRegistry {
|
|
|
720
857
|
aliasedId = result; // the next hook will receive the new id
|
|
721
858
|
continue;
|
|
722
859
|
}
|
|
860
|
+
|
|
723
861
|
resolved =
|
|
724
862
|
result && result.url && (resolveIfNotPlainOrUrl(result.url, parentUrl) || result.url);
|
|
725
863
|
if (!resolved) {
|
|
@@ -729,27 +867,32 @@ class ModuleRegistry {
|
|
|
729
867
|
break;
|
|
730
868
|
}
|
|
731
869
|
}
|
|
870
|
+
|
|
732
871
|
if (aliasedId !== id) {
|
|
733
872
|
// resolved module id is the aliased module if it has already been defined
|
|
734
873
|
if (!resolved && this.namedDefineRegistry.has(aliasedId)) {
|
|
735
874
|
return aliasedId;
|
|
736
|
-
}
|
|
737
|
-
else {
|
|
875
|
+
} else {
|
|
738
876
|
id = aliasedId;
|
|
739
877
|
}
|
|
740
878
|
}
|
|
741
879
|
}
|
|
880
|
+
|
|
742
881
|
if (!resolved) {
|
|
743
882
|
const resolvedOrPlain = resolveIfNotPlainOrUrl(id, parentUrl) || id;
|
|
883
|
+
|
|
744
884
|
// if module registry already has named module the resolved id is the plain id
|
|
745
885
|
if (this.moduleRegistry.has(resolvedOrPlain)) {
|
|
746
886
|
return resolvedOrPlain;
|
|
747
887
|
}
|
|
888
|
+
|
|
748
889
|
const resolvedUrl = this.resolver.resolveLocal(resolvedOrPlain);
|
|
749
890
|
if (resolvedUrl) {
|
|
750
891
|
// return the plain id if it is already defined && the resolvedUrl is NOT already in the module registry
|
|
751
|
-
if (
|
|
752
|
-
this.namedDefineRegistry.
|
|
892
|
+
if (
|
|
893
|
+
this.namedDefineRegistry.has(resolvedOrPlain) &&
|
|
894
|
+
this.namedDefineRegistry.get(resolvedOrPlain).defined
|
|
895
|
+
) {
|
|
753
896
|
const record = this.moduleRegistry.get(resolvedUrl);
|
|
754
897
|
if (!record || !this.aliases.has(resolvedOrPlain)) {
|
|
755
898
|
return resolvedOrPlain;
|
|
@@ -757,13 +900,13 @@ class ModuleRegistry {
|
|
|
757
900
|
}
|
|
758
901
|
return resolvedUrl;
|
|
759
902
|
}
|
|
903
|
+
|
|
760
904
|
if (this.namedDefineRegistry.has(resolvedOrPlain)) {
|
|
761
905
|
return resolvedOrPlain;
|
|
762
906
|
}
|
|
763
907
|
try {
|
|
764
908
|
resolved = await this.resolver.resolve(resolvedOrPlain);
|
|
765
|
-
}
|
|
766
|
-
catch (e) {
|
|
909
|
+
} catch (e) {
|
|
767
910
|
// defer to error handling below for unresolved
|
|
768
911
|
}
|
|
769
912
|
}
|
|
@@ -771,6 +914,7 @@ class ModuleRegistry {
|
|
|
771
914
|
if (this.namedDefineRegistry.has(id)) {
|
|
772
915
|
return id;
|
|
773
916
|
}
|
|
917
|
+
|
|
774
918
|
throw new LoaderError(UNRESOLVED, [id]);
|
|
775
919
|
}
|
|
776
920
|
if (importer && isUrl(resolved)) {
|
|
@@ -778,9 +922,11 @@ class ModuleRegistry {
|
|
|
778
922
|
}
|
|
779
923
|
return resolved;
|
|
780
924
|
}
|
|
925
|
+
|
|
781
926
|
has(id) {
|
|
782
927
|
return this.moduleRegistry.has(id);
|
|
783
928
|
}
|
|
929
|
+
|
|
784
930
|
define(name, dependencies, exporter) {
|
|
785
931
|
const mod = this.namedDefineRegistry.get(name);
|
|
786
932
|
// Don't allow redefining a module.
|
|
@@ -792,6 +938,7 @@ class ModuleRegistry {
|
|
|
792
938
|
this.lastDefine = mod;
|
|
793
939
|
return;
|
|
794
940
|
}
|
|
941
|
+
|
|
795
942
|
const moduleDef = {
|
|
796
943
|
name,
|
|
797
944
|
dependencies,
|
|
@@ -802,10 +949,12 @@ class ModuleRegistry {
|
|
|
802
949
|
// if module is "external", resolve the external promise to notify any dependees
|
|
803
950
|
mod.external.resolveExternal(moduleDef);
|
|
804
951
|
}
|
|
952
|
+
|
|
805
953
|
this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
|
|
806
954
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
807
955
|
this.lastDefine = moduleDef;
|
|
808
956
|
}
|
|
957
|
+
|
|
809
958
|
/**
|
|
810
959
|
* Marks modules as "externally" loaded/provided, so that the loader does not attempt to fetch them.
|
|
811
960
|
*
|
|
@@ -818,6 +967,7 @@ class ModuleRegistry {
|
|
|
818
967
|
let timer;
|
|
819
968
|
const moduleDefPromise = new Promise((resolve, reject) => {
|
|
820
969
|
resolveExternal = resolve;
|
|
970
|
+
|
|
821
971
|
// watch the external for timeout
|
|
822
972
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
823
973
|
timer = setTimeout(() => {
|
|
@@ -835,24 +985,39 @@ class ModuleRegistry {
|
|
|
835
985
|
moduleDefPromise,
|
|
836
986
|
},
|
|
837
987
|
};
|
|
838
|
-
this.namedDefineRegistry.set(id, moduleDef);
|
|
839
|
-
}
|
|
840
|
-
else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
988
|
+
this.namedDefineRegistry.set(id, moduleDef );
|
|
989
|
+
} else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
841
990
|
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
842
991
|
console.warn(MODULE_ALREADY_LOADED.message, id);
|
|
843
992
|
}
|
|
844
993
|
});
|
|
845
994
|
}
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
// A registry for named AMD defines containing the *metadata* of AMD module
|
|
999
|
+
__init() {this.namedDefineRegistry = new Map();}
|
|
1000
|
+
|
|
1001
|
+
// The evaluated module registry where the module identifier (name or URL?) is the key
|
|
1002
|
+
__init2() {this.moduleRegistry = new Map();}
|
|
1003
|
+
|
|
1004
|
+
// Aliases of modules in the registry
|
|
1005
|
+
__init3() {this.aliases = new Map();}
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
846
1009
|
getImportMetadataResolver() {
|
|
847
1010
|
return this.resolver;
|
|
848
1011
|
}
|
|
1012
|
+
|
|
849
1013
|
// Returns an existing module record by the resolvedId or aliased id
|
|
850
|
-
|
|
1014
|
+
getExistingModuleRecord(resolvedId, aliasId) {
|
|
851
1015
|
const moduleRecord = this.moduleRegistry.get(resolvedId);
|
|
852
1016
|
if (moduleRecord) {
|
|
853
1017
|
this.storeModuleAlias(aliasId, resolvedId);
|
|
854
1018
|
return moduleRecord;
|
|
855
1019
|
}
|
|
1020
|
+
|
|
856
1021
|
// Check if this is a known alias
|
|
857
1022
|
if (resolvedId !== aliasId) {
|
|
858
1023
|
const alias = this.aliases.get(aliasId);
|
|
@@ -865,13 +1030,15 @@ class ModuleRegistry {
|
|
|
865
1030
|
}
|
|
866
1031
|
return moduleRecord;
|
|
867
1032
|
}
|
|
868
|
-
|
|
1033
|
+
|
|
1034
|
+
getModuleRecord(resolvedId, id) {
|
|
869
1035
|
// Look for an existing record
|
|
870
1036
|
const existingRecord = this.getExistingModuleRecord(resolvedId, id);
|
|
871
1037
|
if (existingRecord) {
|
|
872
1038
|
// return existing
|
|
873
1039
|
return existingRecord;
|
|
874
1040
|
}
|
|
1041
|
+
|
|
875
1042
|
// Create a new Module Record
|
|
876
1043
|
const instantiation = this.getModuleDef(resolvedId, id);
|
|
877
1044
|
const dependencyRecords = instantiation.then((moduleDef) => {
|
|
@@ -879,15 +1046,17 @@ class ModuleRegistry {
|
|
|
879
1046
|
// get dep and filter out exports
|
|
880
1047
|
const filtered = dependencies
|
|
881
1048
|
.map((dep) => {
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
.filter((depRecord) => depRecord !== undefined);
|
|
1049
|
+
if (dep === 'exports') {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
invariant(dep !== 'require', NO_AMD_REQUIRE);
|
|
1053
|
+
return this.getModuleDependencyRecord.call(this, dep);
|
|
1054
|
+
})
|
|
1055
|
+
.filter((depRecord) => depRecord !== undefined) ;
|
|
1056
|
+
|
|
889
1057
|
return Promise.all(filtered);
|
|
890
1058
|
});
|
|
1059
|
+
|
|
891
1060
|
const newModuleRecord = {
|
|
892
1061
|
id: resolvedId,
|
|
893
1062
|
module: Object.create(null),
|
|
@@ -896,16 +1065,17 @@ class ModuleRegistry {
|
|
|
896
1065
|
evaluated: false,
|
|
897
1066
|
evaluationPromise: null,
|
|
898
1067
|
};
|
|
1068
|
+
|
|
899
1069
|
this.moduleRegistry.set(resolvedId, newModuleRecord);
|
|
900
1070
|
this.storeModuleAlias(id, resolvedId);
|
|
901
1071
|
return newModuleRecord;
|
|
902
1072
|
}
|
|
903
|
-
|
|
1073
|
+
|
|
1074
|
+
storeModuleAlias(aliasId, resolvedId) {
|
|
904
1075
|
if (aliasId !== resolvedId) {
|
|
905
1076
|
if (!this.aliases.has(aliasId)) {
|
|
906
1077
|
this.aliases.set(aliasId, resolvedId);
|
|
907
|
-
}
|
|
908
|
-
else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1078
|
+
} else if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
909
1079
|
// Warn the user if they were not aliasing to the resolvedId
|
|
910
1080
|
const currentResolvedId = this.aliases.get(aliasId);
|
|
911
1081
|
if (currentResolvedId !== resolvedId) {
|
|
@@ -915,17 +1085,23 @@ class ModuleRegistry {
|
|
|
915
1085
|
}
|
|
916
1086
|
}
|
|
917
1087
|
}
|
|
918
|
-
|
|
1088
|
+
|
|
1089
|
+
async getModuleDependencyRecord(dependency) {
|
|
919
1090
|
const resolvedDepId = await this.resolve(dependency);
|
|
920
1091
|
return this.getModuleRecord(resolvedDepId, dependency);
|
|
921
1092
|
}
|
|
1093
|
+
|
|
922
1094
|
// execute the "top-level code" (the code outside of functions) of a module
|
|
923
|
-
|
|
1095
|
+
async topLevelEvaluation(moduleRecord) {
|
|
924
1096
|
await this.instantiateAll(moduleRecord, {});
|
|
925
1097
|
return this.evaluateModule(moduleRecord, {});
|
|
926
1098
|
}
|
|
1099
|
+
|
|
927
1100
|
// Returns a promise when a module and all of it's dependencies have finished instantiation
|
|
928
|
-
|
|
1101
|
+
async instantiateAll(
|
|
1102
|
+
moduleRecord,
|
|
1103
|
+
instantiatedMap,
|
|
1104
|
+
) {
|
|
929
1105
|
if (!instantiatedMap[moduleRecord.id]) {
|
|
930
1106
|
instantiatedMap[moduleRecord.id] = true;
|
|
931
1107
|
const dependencyModuleRecords = await moduleRecord.dependencyRecords;
|
|
@@ -938,46 +1114,61 @@ class ModuleRegistry {
|
|
|
938
1114
|
}
|
|
939
1115
|
}
|
|
940
1116
|
}
|
|
941
|
-
|
|
1117
|
+
|
|
1118
|
+
async evaluateModule(
|
|
1119
|
+
moduleRecord,
|
|
1120
|
+
evaluationMap,
|
|
1121
|
+
) {
|
|
942
1122
|
const dependencyModuleRecords = await moduleRecord.dependencyRecords;
|
|
943
1123
|
if (dependencyModuleRecords.length > 0) {
|
|
944
1124
|
evaluationMap[moduleRecord.id] = true;
|
|
945
1125
|
// evaluate dependencies first
|
|
946
1126
|
await this.evaluateModuleDependencies(dependencyModuleRecords, evaluationMap);
|
|
947
1127
|
}
|
|
1128
|
+
|
|
948
1129
|
const { exporter, dependencies } = await moduleRecord.instantiation;
|
|
949
1130
|
// The exports object automatically gets filled in by the exporter evaluation
|
|
950
1131
|
const exports = {};
|
|
951
|
-
const depsMapped = await Promise.all(
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
1132
|
+
const depsMapped = await Promise.all(
|
|
1133
|
+
dependencies.map(async (dep) => {
|
|
1134
|
+
if (dep === 'exports') {
|
|
1135
|
+
return exports;
|
|
1136
|
+
}
|
|
1137
|
+
const resolvedDepId = await this.resolve(dep);
|
|
1138
|
+
|
|
1139
|
+
const moduleRecord = this.moduleRegistry.get(resolvedDepId) ;
|
|
1140
|
+
if (!moduleRecord) {
|
|
1141
|
+
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
const module = moduleRecord.module;
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* Circular dependencies are handled properly when named exports are used,
|
|
1148
|
+
* however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
|
|
1149
|
+
*
|
|
1150
|
+
* The workaround below applies for circular dependencies (!moduleRecord.evaluated)
|
|
1151
|
+
*/
|
|
1152
|
+
if (!moduleRecord.evaluated) {
|
|
1153
|
+
return this.getCircularDependencyWrapper(module);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
if (module) {
|
|
1157
|
+
return module.__defaultInterop ? module.default : module;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
958
1160
|
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
* Circular dependencies are handled properly when named exports are used,
|
|
963
|
-
* however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
|
|
964
|
-
*
|
|
965
|
-
* The workaround below applies for circular dependencies (!moduleRecord.evaluated)
|
|
966
|
-
*/
|
|
967
|
-
if (!moduleRecord.evaluated) {
|
|
968
|
-
return this.getCircularDependencyWrapper(module);
|
|
969
|
-
}
|
|
970
|
-
if (module) {
|
|
971
|
-
return module.__defaultInterop ? module.default : module;
|
|
972
|
-
}
|
|
973
|
-
throw new LoaderError(FAILED_DEP, [resolvedDepId]);
|
|
974
|
-
}));
|
|
1161
|
+
}),
|
|
1162
|
+
);
|
|
1163
|
+
|
|
975
1164
|
// 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
|
|
976
1165
|
if (moduleRecord.evaluated) {
|
|
977
1166
|
return moduleRecord.module;
|
|
978
1167
|
}
|
|
1168
|
+
|
|
979
1169
|
// evaluates the module function
|
|
980
1170
|
let moduleDefault = exporter(...depsMapped);
|
|
1171
|
+
|
|
981
1172
|
// value is returned from exporter, then we are not using named exports
|
|
982
1173
|
if (moduleDefault !== undefined) {
|
|
983
1174
|
moduleDefault = { default: moduleDefault };
|
|
@@ -993,7 +1184,9 @@ class ModuleRegistry {
|
|
|
993
1184
|
Object.defineProperty(exports, '__useDefault', { value: true });
|
|
994
1185
|
}
|
|
995
1186
|
}
|
|
1187
|
+
|
|
996
1188
|
const moduleExports = moduleDefault || exports;
|
|
1189
|
+
|
|
997
1190
|
// update the module record
|
|
998
1191
|
// copy over enumerable public methods to module
|
|
999
1192
|
for (const key in moduleExports) {
|
|
@@ -1007,6 +1200,7 @@ class ModuleRegistry {
|
|
|
1007
1200
|
},
|
|
1008
1201
|
});
|
|
1009
1202
|
}
|
|
1203
|
+
|
|
1010
1204
|
// copy non-enumerable to module
|
|
1011
1205
|
if (moduleExports.__useDefault) {
|
|
1012
1206
|
Object.defineProperty(moduleRecord.module, '__useDefault', { value: true });
|
|
@@ -1017,27 +1211,36 @@ class ModuleRegistry {
|
|
|
1017
1211
|
if (moduleExports.__esModule) {
|
|
1018
1212
|
Object.defineProperty(moduleRecord.module, '__esModule', { value: true });
|
|
1019
1213
|
}
|
|
1214
|
+
|
|
1020
1215
|
moduleRecord.evaluated = true;
|
|
1021
1216
|
Object.freeze(moduleRecord.module);
|
|
1022
1217
|
return moduleRecord.module;
|
|
1023
1218
|
}
|
|
1219
|
+
|
|
1024
1220
|
// Determines if named exports module has only default export
|
|
1025
|
-
|
|
1026
|
-
return (
|
|
1221
|
+
isNamedExportDefaultOnly(exports) {
|
|
1222
|
+
return (
|
|
1223
|
+
exports !== undefined &&
|
|
1027
1224
|
Object.getOwnPropertyNames(exports).length === 2 &&
|
|
1028
1225
|
Object.prototype.hasOwnProperty.call(exports, 'default') &&
|
|
1029
|
-
Object.prototype.hasOwnProperty.call(exports, '__esModule')
|
|
1226
|
+
Object.prototype.hasOwnProperty.call(exports, '__esModule')
|
|
1227
|
+
);
|
|
1030
1228
|
}
|
|
1229
|
+
|
|
1031
1230
|
// Wrap the dependency in a function that can be called and detected by __circular__ property.
|
|
1032
1231
|
// The LWC engine checks for __circular__ to detect circular dependencies.
|
|
1033
|
-
|
|
1232
|
+
getCircularDependencyWrapper(module) {
|
|
1034
1233
|
const tmp = () => {
|
|
1035
1234
|
return module.__useDefault || module.__defaultInterop ? module.default : module;
|
|
1036
1235
|
};
|
|
1037
1236
|
tmp.__circular__ = true;
|
|
1038
1237
|
return tmp;
|
|
1039
1238
|
}
|
|
1040
|
-
|
|
1239
|
+
|
|
1240
|
+
async evaluateModuleDependencies(
|
|
1241
|
+
dependencyModuleRecords,
|
|
1242
|
+
evaluationMap,
|
|
1243
|
+
) {
|
|
1041
1244
|
for (let i = 0; i < dependencyModuleRecords.length; i++) {
|
|
1042
1245
|
const depRecord = dependencyModuleRecords[i];
|
|
1043
1246
|
if (!depRecord.evaluated && !evaluationMap[depRecord.id]) {
|
|
@@ -1047,15 +1250,17 @@ class ModuleRegistry {
|
|
|
1047
1250
|
}
|
|
1048
1251
|
}
|
|
1049
1252
|
}
|
|
1050
|
-
|
|
1253
|
+
|
|
1254
|
+
async getModuleDef(resolvedId, originalId) {
|
|
1051
1255
|
// reset lastDefine
|
|
1052
1256
|
this.lastDefine = undefined;
|
|
1257
|
+
|
|
1053
1258
|
// the module name can be the resolved ID or the original ID if neither are URL's.
|
|
1054
1259
|
const moduleName = !isUrl(resolvedId)
|
|
1055
1260
|
? resolvedId
|
|
1056
1261
|
: originalId !== resolvedId
|
|
1057
|
-
|
|
1058
|
-
|
|
1262
|
+
? originalId
|
|
1263
|
+
: undefined;
|
|
1059
1264
|
let moduleDef = moduleName && this.namedDefineRegistry.get(moduleName);
|
|
1060
1265
|
if (moduleDef && moduleDef.external) {
|
|
1061
1266
|
return moduleDef.external.moduleDefPromise;
|
|
@@ -1068,67 +1273,75 @@ class ModuleRegistry {
|
|
|
1068
1273
|
this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
|
|
1069
1274
|
return Promise.resolve()
|
|
1070
1275
|
.then(async () => {
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1276
|
+
const loadHooks = this.loadHook;
|
|
1277
|
+
if (loadHooks) {
|
|
1278
|
+
for (let i = 0; i < loadHooks.length; i++) {
|
|
1279
|
+
const loadHook = loadHooks[i];
|
|
1280
|
+
const response = loadHook(resolvedId, parentUrl);
|
|
1281
|
+
const result = (
|
|
1282
|
+
isResponseAPromise(response)
|
|
1283
|
+
? // eslint-disable-next-line no-await-in-loop
|
|
1284
|
+
await evaluateLoadHook(resolvedId, response)
|
|
1285
|
+
: response
|
|
1286
|
+
) ;
|
|
1287
|
+
if (result === undefined) {
|
|
1288
|
+
throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
|
|
1289
|
+
}
|
|
1290
|
+
if (result && result !== null) {
|
|
1291
|
+
return evaluateLoadHookResponse(result, resolvedId);
|
|
1292
|
+
}
|
|
1085
1293
|
}
|
|
1086
1294
|
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
})
|
|
1295
|
+
return false;
|
|
1296
|
+
})
|
|
1090
1297
|
.then((result) => {
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1298
|
+
if (result !== true && hasDocument) {
|
|
1299
|
+
return loadModuleDef(resolvedId);
|
|
1300
|
+
}
|
|
1301
|
+
})
|
|
1095
1302
|
.then(() => {
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
moduleDef
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1303
|
+
// Attempt to retrieve the module definition by name first
|
|
1304
|
+
moduleDef = moduleName && this.namedDefineRegistry.get(moduleName);
|
|
1305
|
+
|
|
1306
|
+
// Fallback to the last loader.define call
|
|
1307
|
+
if (!moduleDef) {
|
|
1308
|
+
moduleDef = this.lastDefine;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
// This should not happen
|
|
1312
|
+
if (!moduleDef) {
|
|
1313
|
+
throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
|
|
1317
|
+
return moduleDef;
|
|
1318
|
+
})
|
|
1109
1319
|
.catch((e) => {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1320
|
+
this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
|
|
1321
|
+
throw e;
|
|
1322
|
+
});
|
|
1113
1323
|
}
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1114
1328
|
addLoaderPlugin(hooks) {
|
|
1115
1329
|
if (typeof hooks !== 'object') {
|
|
1116
1330
|
throw new LoaderError(INVALID_HOOK);
|
|
1117
1331
|
}
|
|
1118
1332
|
const { loadModule: loadHook, resolveModule: resolveHook, loadMapping } = hooks;
|
|
1333
|
+
|
|
1119
1334
|
if (resolveHook) {
|
|
1120
1335
|
if (this.resolveHook) {
|
|
1121
1336
|
this.resolveHook.push(resolveHook);
|
|
1122
|
-
}
|
|
1123
|
-
else {
|
|
1337
|
+
} else {
|
|
1124
1338
|
this.resolveHook = [resolveHook];
|
|
1125
1339
|
}
|
|
1126
1340
|
}
|
|
1127
1341
|
if (loadHook) {
|
|
1128
1342
|
if (this.loadHook) {
|
|
1129
1343
|
this.loadHook.push(loadHook);
|
|
1130
|
-
}
|
|
1131
|
-
else {
|
|
1344
|
+
} else {
|
|
1132
1345
|
this.loadHook = [loadHook];
|
|
1133
1346
|
}
|
|
1134
1347
|
}
|
|
@@ -1136,7 +1349,8 @@ class ModuleRegistry {
|
|
|
1136
1349
|
this.resolver.addLoadMappingHook(loadMapping);
|
|
1137
1350
|
}
|
|
1138
1351
|
}
|
|
1139
|
-
|
|
1352
|
+
|
|
1353
|
+
importMetadataInvalidationCallback({ name, oldUrl, newUrl }) {
|
|
1140
1354
|
const handleStaleModuleHooks = this.handleStaleModuleHook;
|
|
1141
1355
|
if (handleStaleModuleHooks) {
|
|
1142
1356
|
evaluateHandleStaleModuleHooks(handleStaleModuleHooks, {
|
|
@@ -1144,19 +1358,19 @@ class ModuleRegistry {
|
|
|
1144
1358
|
oldUrl,
|
|
1145
1359
|
newUrl,
|
|
1146
1360
|
});
|
|
1147
|
-
}
|
|
1148
|
-
else {
|
|
1361
|
+
} else {
|
|
1149
1362
|
if (process.env.NODE_ENV !== 'production' && hasConsole) {
|
|
1150
1363
|
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
1151
1364
|
console.warn(`stale module detected ${name}, current URL:${oldUrl}, new URL:${newUrl}`);
|
|
1152
1365
|
}
|
|
1153
1366
|
}
|
|
1154
1367
|
}
|
|
1368
|
+
|
|
1369
|
+
|
|
1155
1370
|
registerHandleStaleModuleHook(handleStaleModule) {
|
|
1156
1371
|
if (this.handleStaleModuleHook) {
|
|
1157
1372
|
this.handleStaleModuleHook.push(handleStaleModule);
|
|
1158
|
-
}
|
|
1159
|
-
else {
|
|
1373
|
+
} else {
|
|
1160
1374
|
this.handleStaleModuleHook = [handleStaleModule];
|
|
1161
1375
|
}
|
|
1162
1376
|
}
|
|
@@ -1166,15 +1380,21 @@ class ModuleRegistry {
|
|
|
1166
1380
|
* The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
|
|
1167
1381
|
*/
|
|
1168
1382
|
class Loader {
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1169
1386
|
constructor(config) {
|
|
1170
1387
|
let baseUrl = config.baseUrl;
|
|
1171
1388
|
const mappingEndpoint = config.endpoints ? config.endpoints.uris.mapping : undefined;
|
|
1172
1389
|
let profiler = config.profiler;
|
|
1390
|
+
|
|
1173
1391
|
if (!mappingEndpoint) {
|
|
1174
1392
|
throw new LoaderError(NO_MAPPING_URL);
|
|
1175
1393
|
}
|
|
1394
|
+
|
|
1176
1395
|
// add a trailing slash, if it does not exist
|
|
1177
1396
|
config.endpoints.uris.mapping = mappingEndpoint.replace(/\/?$/, '/');
|
|
1397
|
+
|
|
1178
1398
|
if (baseUrl) {
|
|
1179
1399
|
// add a trailing slash, if it does not exist
|
|
1180
1400
|
baseUrl = baseUrl.replace(/\/?$/, '/');
|
|
@@ -1185,6 +1405,7 @@ class Loader {
|
|
|
1185
1405
|
if (!baseUrl) {
|
|
1186
1406
|
throw new LoaderError(NO_BASE_URL);
|
|
1187
1407
|
}
|
|
1408
|
+
|
|
1188
1409
|
if (!profiler) {
|
|
1189
1410
|
// default noop profiler
|
|
1190
1411
|
profiler = {
|
|
@@ -1196,7 +1417,9 @@ class Loader {
|
|
|
1196
1417
|
},
|
|
1197
1418
|
};
|
|
1198
1419
|
}
|
|
1420
|
+
|
|
1199
1421
|
this.registry = new ModuleRegistry(Object.freeze({ endpoints: config.endpoints, baseUrl, profiler }));
|
|
1422
|
+
|
|
1200
1423
|
// TODO: W-10539691 - temp workaround for LWR-Java -- remove once appId is implemented there
|
|
1201
1424
|
if (config.appMetadata && !config.appMetadata.appId) {
|
|
1202
1425
|
// Parse the appId from the bootstrapModule
|
|
@@ -1205,6 +1428,7 @@ class Loader {
|
|
|
1205
1428
|
const appId = match && match[1];
|
|
1206
1429
|
config.appMetadata.appId = appId;
|
|
1207
1430
|
}
|
|
1431
|
+
|
|
1208
1432
|
// TODO: https://github.com/salesforce-experience-platform-emu/lwr/issues/1087
|
|
1209
1433
|
this.services = Object.freeze({
|
|
1210
1434
|
addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
|
|
@@ -1212,6 +1436,7 @@ class Loader {
|
|
|
1212
1436
|
appMetadata: config.appMetadata,
|
|
1213
1437
|
});
|
|
1214
1438
|
}
|
|
1439
|
+
|
|
1215
1440
|
/**
|
|
1216
1441
|
* Defines/registers a single named AMD module definition.
|
|
1217
1442
|
*
|
|
@@ -1224,14 +1449,18 @@ class Loader {
|
|
|
1224
1449
|
invariant(typeof name === 'string', MISSING_NAME);
|
|
1225
1450
|
let ctor = execute;
|
|
1226
1451
|
let deps = dependencies;
|
|
1452
|
+
|
|
1227
1453
|
// Convert no dependencies form `define('name', function(){}, {});` to: `define('name', [], function(){}, {})`
|
|
1228
1454
|
if (typeof deps === 'function') {
|
|
1229
1455
|
ctor = dependencies;
|
|
1230
1456
|
deps = [];
|
|
1231
1457
|
}
|
|
1458
|
+
|
|
1232
1459
|
invariant(Array.isArray(deps), INVALID_DEPS);
|
|
1233
|
-
|
|
1460
|
+
|
|
1461
|
+
this.registry.define(name, deps, ctor );
|
|
1234
1462
|
}
|
|
1463
|
+
|
|
1235
1464
|
/**
|
|
1236
1465
|
* Retrieves/loads a module, returning it from the registry if it exists and fetching it if it doesn't.
|
|
1237
1466
|
*
|
|
@@ -1243,6 +1472,7 @@ class Loader {
|
|
|
1243
1472
|
async load(id, importer) {
|
|
1244
1473
|
return this.registry.load(id, importer);
|
|
1245
1474
|
}
|
|
1475
|
+
|
|
1246
1476
|
/**
|
|
1247
1477
|
* 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).
|
|
1248
1478
|
*
|
|
@@ -1252,6 +1482,7 @@ class Loader {
|
|
|
1252
1482
|
has(id) {
|
|
1253
1483
|
return this.registry.has(id);
|
|
1254
1484
|
}
|
|
1485
|
+
|
|
1255
1486
|
/**
|
|
1256
1487
|
* Resolves the module identifier or URL. Returns the module identifier if the moduleDefinition exists, or the full resolved URL if a URL is given.
|
|
1257
1488
|
*
|
|
@@ -1263,9 +1494,11 @@ class Loader {
|
|
|
1263
1494
|
async resolve(id, importer) {
|
|
1264
1495
|
return this.registry.resolve(id, importer);
|
|
1265
1496
|
}
|
|
1497
|
+
|
|
1266
1498
|
async registerImportMappings(mappings, rootSpecifiers) {
|
|
1267
1499
|
this.registry.getImportMetadataResolver().registerImportMappings(mappings, rootSpecifiers);
|
|
1268
1500
|
}
|
|
1501
|
+
|
|
1269
1502
|
/**
|
|
1270
1503
|
* Marks modules as "externally" loaded/provided (e.g. preloaded), so that the loader does not attempt to load them.
|
|
1271
1504
|
*
|
|
@@ -1276,11 +1509,15 @@ class Loader {
|
|
|
1276
1509
|
}
|
|
1277
1510
|
}
|
|
1278
1511
|
|
|
1512
|
+
function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
1513
|
+
|
|
1279
1514
|
/** LWR Everywhere module for AMD **/
|
|
1515
|
+
|
|
1280
1516
|
// Process the LWR Client Bootstrap Config
|
|
1281
|
-
const configOptions = {
|
|
1517
|
+
const configOptions = {"format":"amd","apiVersion":"v57.0","apiPrefix":"/lwr","bundle":false,"debug":true} ;
|
|
1282
1518
|
const configuration = getConfiguration(configOptions);
|
|
1283
1519
|
const { bootstrapModule, endpoints, baseUrl, imports, index } = configuration;
|
|
1520
|
+
|
|
1284
1521
|
// Initialize the AMD loader (its code is bundled into the LWR Everywhere module)
|
|
1285
1522
|
const loader = new Loader({ endpoints, baseUrl });
|
|
1286
1523
|
loader.define('lwr/loader/v/0_0_1', ['exports'], (exports) => {
|
|
@@ -1290,89 +1527,110 @@ loader.define('lwr/loader/v/0_0_1', ['exports'], (exports) => {
|
|
|
1290
1527
|
});
|
|
1291
1528
|
});
|
|
1292
1529
|
await loader.registerImportMappings({ imports, index }, [bootstrapModule]);
|
|
1293
|
-
globalThis.LWR = Object.freeze({ define: loader.define.bind(loader) });
|
|
1530
|
+
(globalThis ).LWR = Object.freeze({ define: loader.define.bind(loader) });
|
|
1531
|
+
|
|
1294
1532
|
// Define preload modules (ie: ABS, vault, etc) BEFORE loading them
|
|
1295
|
-
LWR.define('lwr/everywhereAmd/v/0_0_1', ['exports', 'lwr/vault/v/0_0_1'], function (exports,
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
return null; // defer to loader
|
|
1309
|
-
}
|
|
1310
|
-
// Add a hook for adding OAuth headers to module and mapping requests
|
|
1311
|
-
// Workaround: core does not have proper versioning; replace all versions with 0_0_1
|
|
1312
|
-
function everywhereHooks(serviceAPI, mappingEndpoint, coreVersion) {
|
|
1313
|
-
serviceAPI.addLoaderPlugin({
|
|
1314
|
-
loadModule: async (url) => {
|
|
1315
|
-
return authFetch(url);
|
|
1316
|
-
},
|
|
1317
|
-
loadMapping: async (specifier) => {
|
|
1318
|
-
const res = await authFetch(`${mappingEndpoint}${encodeURIComponent(specifier)}`, 'json');
|
|
1319
|
-
if (res && res.ok) {
|
|
1320
|
-
return res.json();
|
|
1321
|
-
}
|
|
1322
|
-
return null;
|
|
1323
|
-
},
|
|
1324
|
-
resolveModule: specifier => {
|
|
1325
|
-
// Workaround W-8010259 (dynamic imports without versions)
|
|
1326
|
-
// by adding a default version to version-less specifiers
|
|
1327
|
-
if (specifier && !/\/v\/[^/]+?$/.test(specifier)) {
|
|
1328
|
-
return `${specifier}/v/${coreVersion}`;
|
|
1329
|
-
}
|
|
1330
|
-
return null; // defer to loader
|
|
1331
|
-
}
|
|
1332
|
-
});
|
|
1333
|
-
}
|
|
1334
|
-
function initAmd(services, mappingEndpoint, coreVersion) {
|
|
1335
|
-
// Initialize the loader hooks for LWR-S endpoints
|
|
1336
|
-
if (mappingEndpoint.match(/\/v5\d\.0\//)) {
|
|
1337
|
-
everywhereHooks(services, mappingEndpoint, coreVersion);
|
|
1338
|
-
}
|
|
1533
|
+
LWR.define('lwr/everywhereAmd/v/0_0_1', ['exports', 'lwr/vault/v/0_0_1'], (function (exports, _0_10_0Alpha_20) {
|
|
1534
|
+
// IMPORTANT: Do not add any static imports to this file that are not bundled with the amd-client
|
|
1535
|
+
|
|
1536
|
+
// Do an OAuthed fetch
|
|
1537
|
+
async function authFetch(url, accept = 'javascript') {
|
|
1538
|
+
const authInfo = _0_10_0Alpha_20.getAuthInfo();
|
|
1539
|
+
if (authInfo) {
|
|
1540
|
+
return fetch(url, {
|
|
1541
|
+
headers: {
|
|
1542
|
+
Accept: `application/${accept}`,
|
|
1543
|
+
Authorization: `Bearer ${authInfo.access_token}`
|
|
1544
|
+
}
|
|
1545
|
+
});
|
|
1339
1546
|
}
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1547
|
+
return null; // defer to loader
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// Add a hook for adding OAuth headers to module and mapping requests
|
|
1551
|
+
// Workaround: core does not have proper versioning; replace all versions with 0_0_1
|
|
1552
|
+
function everywhereHooks(serviceAPI, mappingEndpoint, coreVersion) {
|
|
1553
|
+
serviceAPI.addLoaderPlugin({
|
|
1554
|
+
loadModule: async url => {
|
|
1555
|
+
return authFetch(url);
|
|
1556
|
+
},
|
|
1557
|
+
loadMapping: async specifier => {
|
|
1558
|
+
const res = await authFetch(`${mappingEndpoint}${encodeURIComponent(specifier)}`, 'json');
|
|
1559
|
+
if (res && res.ok) {
|
|
1560
|
+
return res.json();
|
|
1561
|
+
}
|
|
1562
|
+
return null;
|
|
1563
|
+
},
|
|
1564
|
+
resolveModule: specifier => {
|
|
1565
|
+
// Workaround W-8010259 (dynamic imports without versions)
|
|
1566
|
+
// by adding a default version to version-less specifiers
|
|
1567
|
+
if (specifier && !/\/v\/[^/]+?$/.test(specifier)) {
|
|
1568
|
+
return `${specifier}/v/${coreVersion}`;
|
|
1347
1569
|
}
|
|
1570
|
+
return null; // defer to loader
|
|
1571
|
+
}
|
|
1572
|
+
});
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
function initAmd(services, mappingEndpoint, coreVersion) {
|
|
1576
|
+
// Initialize the loader hooks for LWR-S endpoints
|
|
1577
|
+
if (mappingEndpoint.match(/\/v5\d\.0\//)) {
|
|
1578
|
+
everywhereHooks(services, mappingEndpoint, coreVersion);
|
|
1348
1579
|
}
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
//
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
function getAuthInfo() {
|
|
1359
|
-
return authInfo;
|
|
1360
|
-
}
|
|
1361
|
-
function setAuthInfo(ai) {
|
|
1362
|
-
authInfo = ai;
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
// Export the LWRE authenticate() API
|
|
1583
|
+
// This is a duplicate of the function exported from "lwr/everywhere"
|
|
1584
|
+
// because it MUST be bundled into the LWRE module via "lwr/everywhereAmd"
|
|
1585
|
+
function authenticate(authInfo) {
|
|
1586
|
+
// Put OAuth info into the vault
|
|
1587
|
+
if (authInfo) {
|
|
1588
|
+
_0_10_0Alpha_20.setAuthInfo(authInfo);
|
|
1363
1589
|
}
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
exports.authenticate = authenticate;
|
|
1593
|
+
exports.initAmd = initAmd;
|
|
1594
|
+
|
|
1595
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1596
|
+
|
|
1597
|
+
}));
|
|
1598
|
+
LWR.define('lwr/vault/v/0_0_1', ['exports'], (function (exports) {
|
|
1599
|
+
// Credential vault for storing setters and subscribers for OAuth info
|
|
1600
|
+
// This module is a SINGLETON and must be excluded from ESM bundling
|
|
1601
|
+
|
|
1602
|
+
let authInfo;
|
|
1603
|
+
|
|
1604
|
+
// Get and set the OAuth info
|
|
1605
|
+
function getAuthInfo() {
|
|
1606
|
+
return authInfo;
|
|
1607
|
+
}
|
|
1608
|
+
function setAuthInfo(ai) {
|
|
1609
|
+
authInfo = ai;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
exports.getAuthInfo = getAuthInfo;
|
|
1613
|
+
exports.setAuthInfo = setAuthInfo;
|
|
1614
|
+
|
|
1615
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1616
|
+
|
|
1617
|
+
}));
|
|
1618
|
+
|
|
1619
|
+
|
|
1368
1620
|
// Set up the AMD loader hooks for OAuth
|
|
1369
1621
|
const { initAmd, authenticate } = await loader.load(bootstrapModule);
|
|
1370
|
-
initAmd(loader.services, endpoints
|
|
1622
|
+
initAmd(loader.services, _optionalChain([endpoints, 'optionalAccess', _ => _.uris, 'access', _2 => _2.mapping]), '0_0_1');
|
|
1623
|
+
|
|
1371
1624
|
// Wrap the createComponent API to allow proper ordering of module definitions:
|
|
1372
1625
|
// 1. LWRE module bundle (this): lwr/loader, lwr/everywhereAmd, lwr/vault
|
|
1373
1626
|
// 2. lwr/everywhere (deps: lwc, synthetic shadow, lwr/host, etc)
|
|
1374
1627
|
// 3. Salesforce hosted UI content (i.e. embedded components and deps)
|
|
1375
|
-
async function createComponent(
|
|
1628
|
+
async function createComponent(
|
|
1629
|
+
specifier,
|
|
1630
|
+
nodeId,
|
|
1631
|
+
properties = {},
|
|
1632
|
+
config,
|
|
1633
|
+
) {
|
|
1376
1634
|
// Do not load the "lwr/everywhere" API module ahead of time
|
|
1377
1635
|
// This import MUST be done AFTER the loader hooks are in place via authenticate() so
|
|
1378
1636
|
// modules [statically] imported from core have the appropriate Authorization headers
|