@lwrjs/loader 0.6.0-alpha.9 → 0.6.2
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-loader-shim-legacy.bundle.js +326 -218
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +2 -2
- package/build/assets/prod/lwr-loader-shim-legacy.js +289 -213
- package/build/assets/prod/lwr-loader-shim.bundle.js +324 -258
- package/build/assets/prod/lwr-loader-shim.bundle.min.js +2 -2
- package/build/assets/prod/lwr-loader-shim.js +289 -246
- package/build/bundle/prod/lwr/esmLoader/esmLoader.js +1 -1
- package/build/cjs/modules/lwr/esmLoader/esmLoader.cjs +9 -6
- package/build/cjs/modules/lwr/esmLoader/importResolver.cjs +24 -3
- package/build/cjs/modules/lwr/loader/constants/constants.cjs +0 -10
- package/build/cjs/modules/lwr/loader/moduleRegistry/importMetadataResolver.cjs +4 -3
- package/build/cjs/modules/lwr/loader/moduleRegistry/moduleRegistry.cjs +8 -3
- package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.cjs +12 -2
- package/build/modules/lwr/esmLoader/esmLoader.js +38 -10
- package/build/modules/lwr/loader/loader.js +35 -12
- package/build/modules/lwr/loaderLegacy/loaderLegacy.js +37 -5
- package/package.json +12 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class i{constructor(i){var t,e;this.importURICache=new Map,this.modifiers="",this.normalizeMetadata(i),this.mappingEndpoint=(null==i?void 0:i.importMappings)||null===(t=null==i?void 0:i.endpoints)||void 0===t?void 0:t.uris.mapping,(null===(e=null==i?void 0:i.endpoints)||void 0===e?void 0:e.modifiers)&&(this.modifiers=Object.entries(i.endpoints.modifiers).reduce(((i,[t,e])=>i+`${t}=${e}&`),"?"))}normalizeMetadata(i){if(i&&i.imports)for(const[t,e]of Object.entries(i.imports))if(t&&e){(Array.isArray(e)?e:[]).forEach((i=>{this.importURICache.set(i,t)}))}}async fetchMappings(i){const t=`${this.mappingEndpoint}${encodeURIComponent(i)}${this.modifiers}`,e=await globalThis.fetch(t);if(e.ok){const i=await e.json();this.normalizeMetadata(i)}}async resolve(i){let t=this.importURICache.get(i);return!t&&this.mappingEndpoint&&(await this.fetchMappings(i),t=this.importURICache.get(i)),t}}class t{constructor(i){this.importURICache=i&&i.imports?i:{imports:{}}}legacyResolve(i){return this.importURICache.imports[i]}}let e,o,n;function s(s){e=s;const{imports:r,index:a,importMappings:p,endpoints:c}=s;o=new i({imports:r,index:a,endpoints:c,importMappings:p}),n=new t(p)}async function r(i,t){const s=await async function(i,t){let s;if(!o||!n)throw new Error("The ESM Loader was not initialized");if(s=await o.resolve(i),s)return s;if(s=n.legacyResolve(i),s)return s;if(s=i,s.indexOf("://")<0&&!s.startsWith("/")){const{endpoints:o}=e;o&&o.uris&&o.uris.module&&(s=o.uris.module+encodeURIComponent(i),t&&(s+=`?importer=${encodeURIComponent(t)}`),o.modifiers&&(s+=Object.entries(o.modifiers).reduce(((i,[t,e])=>i+`${t}=${e}&`),t?"&":"?")))}return s}(i,t);return import(s)}export{s as init,r as load};
|
|
@@ -34,20 +34,20 @@ var resolver;
|
|
|
34
34
|
var resolverLegacy;
|
|
35
35
|
function init(config) {
|
|
36
36
|
esmLoaderConfig = config;
|
|
37
|
-
const {imports, index, importMappings} = config;
|
|
38
|
-
resolver = new import_importResolver.default({imports, index});
|
|
37
|
+
const {imports, index, importMappings, endpoints} = config;
|
|
38
|
+
resolver = new import_importResolver.default({imports, index, endpoints, importMappings});
|
|
39
39
|
resolverLegacy = new import_importResolverLegacy.default(importMappings);
|
|
40
40
|
}
|
|
41
|
-
function load(specifier, importer) {
|
|
42
|
-
const uri = resolveUrl(specifier, importer);
|
|
41
|
+
async function load(specifier, importer) {
|
|
42
|
+
const uri = await resolveUrl(specifier, importer);
|
|
43
43
|
return Promise.resolve().then(() => __toModule(require(uri)));
|
|
44
44
|
}
|
|
45
|
-
function resolveUrl(specifier, importer) {
|
|
45
|
+
async function resolveUrl(specifier, importer) {
|
|
46
46
|
let uri;
|
|
47
47
|
if (!resolver || !resolverLegacy) {
|
|
48
48
|
throw new Error("The ESM Loader was not initialized");
|
|
49
49
|
}
|
|
50
|
-
uri = resolver.resolve(specifier);
|
|
50
|
+
uri = await resolver.resolve(specifier);
|
|
51
51
|
if (uri) {
|
|
52
52
|
return uri;
|
|
53
53
|
}
|
|
@@ -63,6 +63,9 @@ function resolveUrl(specifier, importer) {
|
|
|
63
63
|
if (importer) {
|
|
64
64
|
uri += `?importer=${encodeURIComponent(importer)}`;
|
|
65
65
|
}
|
|
66
|
+
if (endpoints.modifiers) {
|
|
67
|
+
uri += Object.entries(endpoints.modifiers).reduce((q, [k, v]) => q += `${k}=${v}&`, importer ? "&" : "?");
|
|
68
|
+
}
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
return uri;
|
|
@@ -11,8 +11,16 @@ __export(exports, {
|
|
|
11
11
|
default: () => importResolver_default
|
|
12
12
|
});
|
|
13
13
|
var ImportResolver = class {
|
|
14
|
-
constructor(
|
|
14
|
+
constructor(config) {
|
|
15
15
|
this.importURICache = new Map();
|
|
16
|
+
this.modifiers = "";
|
|
17
|
+
this.normalizeMetadata(config);
|
|
18
|
+
this.mappingEndpoint = config?.importMappings ? void 0 : config?.endpoints?.uris.mapping;
|
|
19
|
+
if (config?.endpoints?.modifiers) {
|
|
20
|
+
this.modifiers = Object.entries(config.endpoints.modifiers).reduce((q, [k, v]) => q += `${k}=${v}&`, "?");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
normalizeMetadata(importMetada) {
|
|
16
24
|
if (importMetada && importMetada.imports) {
|
|
17
25
|
for (const [uri, value] of Object.entries(importMetada.imports)) {
|
|
18
26
|
if (uri && value) {
|
|
@@ -24,8 +32,21 @@ var ImportResolver = class {
|
|
|
24
32
|
}
|
|
25
33
|
}
|
|
26
34
|
}
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
async fetchMappings(specifier) {
|
|
36
|
+
const mappingUri = `${this.mappingEndpoint}${encodeURIComponent(specifier)}${this.modifiers}`;
|
|
37
|
+
const res = await globalThis.fetch(mappingUri);
|
|
38
|
+
if (res.ok) {
|
|
39
|
+
const mappings = await res.json();
|
|
40
|
+
this.normalizeMetadata(mappings);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async resolve(specifier) {
|
|
44
|
+
let uri = this.importURICache.get(specifier);
|
|
45
|
+
if (!uri && this.mappingEndpoint) {
|
|
46
|
+
await this.fetchMappings(specifier);
|
|
47
|
+
uri = this.importURICache.get(specifier);
|
|
48
|
+
}
|
|
49
|
+
return uri;
|
|
29
50
|
}
|
|
30
51
|
};
|
|
31
52
|
var importResolver_default = ImportResolver;
|
|
@@ -8,16 +8,6 @@ var __export = (target, all) => {
|
|
|
8
8
|
// packages/@lwrjs/loader/src/modules/lwr/loader/constants/constants.ts
|
|
9
9
|
__markAsModule(exports);
|
|
10
10
|
__export(exports, {
|
|
11
|
-
METRIC_MAPPINGS_ERROR: () => METRIC_MAPPINGS_ERROR,
|
|
12
|
-
METRIC_MAPPINGS_FETCH: () => METRIC_MAPPINGS_FETCH,
|
|
13
|
-
METRIC_MODULE_DEFINE: () => METRIC_MODULE_DEFINE,
|
|
14
|
-
METRIC_MODULE_ERROR: () => METRIC_MODULE_ERROR,
|
|
15
|
-
METRIC_MODULE_FETCH: () => METRIC_MODULE_FETCH,
|
|
16
11
|
MODULE_LOAD_TIMEOUT_TIMER: () => MODULE_LOAD_TIMEOUT_TIMER
|
|
17
12
|
});
|
|
18
13
|
var MODULE_LOAD_TIMEOUT_TIMER = 3e5;
|
|
19
|
-
var METRIC_MODULE_DEFINE = "lwr.loader.module.define";
|
|
20
|
-
var METRIC_MODULE_FETCH = "lwr.loader.module.fetch";
|
|
21
|
-
var METRIC_MAPPINGS_FETCH = "lwr.loader.mappings.fetch";
|
|
22
|
-
var METRIC_MODULE_ERROR = "lwr.loader.module.error";
|
|
23
|
-
var METRIC_MAPPINGS_ERROR = "lwr.loader.mappings.error";
|
|
@@ -28,7 +28,7 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_messages = __toModule(require("../errors/messages"));
|
|
30
30
|
var import_url = __toModule(require("../utils/url"));
|
|
31
|
-
var
|
|
31
|
+
var import_metrics = __toModule(require("lwr/metrics"));
|
|
32
32
|
var ImportMetadataResolver = class {
|
|
33
33
|
constructor(config, invalidationCallback) {
|
|
34
34
|
this.importURICache = new Map();
|
|
@@ -119,7 +119,7 @@ var ImportMetadataResolver = class {
|
|
|
119
119
|
if (pending) {
|
|
120
120
|
return pending;
|
|
121
121
|
}
|
|
122
|
-
this.config.profiler.logOperationStart({id:
|
|
122
|
+
this.config.profiler.logOperationStart({id: import_metrics.MAPPINGS_FETCH, specifier});
|
|
123
123
|
const fetchMappingService = this.hasMappingHooks() ? this.evaluateMappingHooks : this.fetchNewMappings;
|
|
124
124
|
const promise = fetchMappingService.bind(this)(specifier).then((importMetadata) => {
|
|
125
125
|
if (!importMetadata || !importMetadata.imports) {
|
|
@@ -130,6 +130,7 @@ var ImportMetadataResolver = class {
|
|
|
130
130
|
if (!uri) {
|
|
131
131
|
throw new import_messages.LoaderError(import_messages.UNRESOLVED, [specifier]);
|
|
132
132
|
}
|
|
133
|
+
this.config.profiler.logOperationEnd({id: import_metrics.MAPPINGS_FETCH, specifier});
|
|
133
134
|
return uri;
|
|
134
135
|
}).finally(() => {
|
|
135
136
|
this.pendingURICache.delete(specifier);
|
|
@@ -162,7 +163,7 @@ var ImportMetadataResolver = class {
|
|
|
162
163
|
const uri = (0, import_url.resolveUrl)(this.buildMappingUrl(specifier), this.getBaseUrl());
|
|
163
164
|
return globalThis.fetch(uri).then((res) => {
|
|
164
165
|
if (!res.ok) {
|
|
165
|
-
this.config.profiler.logOperationStart({id:
|
|
166
|
+
this.config.profiler.logOperationStart({id: import_metrics.MAPPINGS_ERROR, specifier});
|
|
166
167
|
throw new import_messages.LoaderError(import_messages.UNRESOLVED, [specifier]);
|
|
167
168
|
}
|
|
168
169
|
return res.json().then((ret) => {
|
|
@@ -33,6 +33,7 @@ var import_scriptLoad = __toModule(require("./scriptLoad"));
|
|
|
33
33
|
var import_importMetadataResolver = __toModule(require("./importMetadataResolver"));
|
|
34
34
|
var import_moduleInvalidation = __toModule(require("../hooks/moduleInvalidation"));
|
|
35
35
|
var import_resolveAndLoadHook = __toModule(require("../hooks/resolveAndLoadHook"));
|
|
36
|
+
var import_metrics = __toModule(require("lwr/metrics"));
|
|
36
37
|
var import_constants = __toModule(require("../constants/constants"));
|
|
37
38
|
var ModuleRegistry = class {
|
|
38
39
|
constructor(config) {
|
|
@@ -144,7 +145,7 @@ var ModuleRegistry = class {
|
|
|
144
145
|
if (mod && mod.external) {
|
|
145
146
|
mod.external.resolveExternal(moduleDef);
|
|
146
147
|
}
|
|
147
|
-
this.profiler.logOperationStart({id:
|
|
148
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_DEFINE, specifier: name});
|
|
148
149
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
149
150
|
this.lastDefine = moduleDef;
|
|
150
151
|
}
|
|
@@ -290,6 +291,9 @@ var ModuleRegistry = class {
|
|
|
290
291
|
if (moduleExports.__defaultInterop) {
|
|
291
292
|
Object.defineProperty(moduleRecord.module, "__defaultInterop", {value: true});
|
|
292
293
|
}
|
|
294
|
+
if (moduleExports.__esModule) {
|
|
295
|
+
Object.defineProperty(moduleRecord.module, "__esModule", {value: true});
|
|
296
|
+
}
|
|
293
297
|
moduleRecord.evaluated = true;
|
|
294
298
|
Object.freeze(moduleRecord.module);
|
|
295
299
|
return moduleRecord.module;
|
|
@@ -325,7 +329,7 @@ var ModuleRegistry = class {
|
|
|
325
329
|
}
|
|
326
330
|
const parentUrl = this.resolver.getBaseUrl();
|
|
327
331
|
const specifier = moduleName || originalId;
|
|
328
|
-
this.profiler.logOperationStart({id:
|
|
332
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_FETCH, specifier});
|
|
329
333
|
return Promise.resolve().then(async () => {
|
|
330
334
|
const loadHooks = this.loadHook;
|
|
331
335
|
if (loadHooks) {
|
|
@@ -354,9 +358,10 @@ var ModuleRegistry = class {
|
|
|
354
358
|
if (!moduleDef) {
|
|
355
359
|
throw new import_messages.LoaderError(import_messages.FAIL_INSTANTIATE, [resolvedId]);
|
|
356
360
|
}
|
|
361
|
+
this.profiler.logOperationEnd({id: import_metrics.MODULE_FETCH, specifier});
|
|
357
362
|
return moduleDef;
|
|
358
363
|
}).catch((e) => {
|
|
359
|
-
this.profiler.logOperationStart({id:
|
|
364
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_ERROR, specifier});
|
|
360
365
|
throw e;
|
|
361
366
|
});
|
|
362
367
|
}
|
|
@@ -33,11 +33,13 @@ var import_scriptLoad = __toModule(require("./scriptLoad"));
|
|
|
33
33
|
var import_resolveAndLoadHook = __toModule(require("../hooks/resolveAndLoadHook"));
|
|
34
34
|
var import_moduleInvalidation = __toModule(require("../hooks/moduleInvalidation"));
|
|
35
35
|
var import_constants = __toModule(require("../constants/constants"));
|
|
36
|
+
var import_metrics = __toModule(require("lwr/metrics"));
|
|
36
37
|
var ModuleRegistry = class {
|
|
37
|
-
constructor(
|
|
38
|
+
constructor(config) {
|
|
38
39
|
this.namedDefineRegistry = new Map();
|
|
39
40
|
this.moduleRegistry = new Map();
|
|
40
|
-
this.baseUrl = baseUrl;
|
|
41
|
+
this.baseUrl = config.baseUrl;
|
|
42
|
+
this.profiler = config.profiler;
|
|
41
43
|
}
|
|
42
44
|
async load(id, importer) {
|
|
43
45
|
const resolvedId = await this.resolve(id, importer);
|
|
@@ -137,6 +139,7 @@ var ModuleRegistry = class {
|
|
|
137
139
|
if (mod && mod.external) {
|
|
138
140
|
mod.external.resolveExternal(moduleDef);
|
|
139
141
|
}
|
|
142
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_DEFINE, specifier: name});
|
|
140
143
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
141
144
|
this.lastDefine = moduleDef;
|
|
142
145
|
if (signatures.hashes) {
|
|
@@ -316,6 +319,9 @@ var ModuleRegistry = class {
|
|
|
316
319
|
if (moduleExports.__defaultInterop) {
|
|
317
320
|
Object.defineProperty(moduleRecord.module, "__defaultInterop", {value: true});
|
|
318
321
|
}
|
|
322
|
+
if (moduleExports.__esModule) {
|
|
323
|
+
Object.defineProperty(moduleRecord.module, "__esModule", {value: true});
|
|
324
|
+
}
|
|
319
325
|
moduleRecord.evaluated = true;
|
|
320
326
|
Object.freeze(moduleRecord.module);
|
|
321
327
|
return moduleRecord.module;
|
|
@@ -350,6 +356,8 @@ var ModuleRegistry = class {
|
|
|
350
356
|
return moduleDef;
|
|
351
357
|
}
|
|
352
358
|
const parentUrl = this.baseUrl;
|
|
359
|
+
const specifier = moduleName || originalId;
|
|
360
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_FETCH, specifier});
|
|
353
361
|
return Promise.resolve().then(async () => {
|
|
354
362
|
const loadHooks = this.loadHook;
|
|
355
363
|
if (loadHooks) {
|
|
@@ -378,8 +386,10 @@ var ModuleRegistry = class {
|
|
|
378
386
|
if (!moduleDef) {
|
|
379
387
|
throw new import_messages.LoaderError(import_messages.FAIL_INSTANTIATE, [resolvedId]);
|
|
380
388
|
}
|
|
389
|
+
this.profiler.logOperationEnd({id: import_metrics.MODULE_FETCH, specifier});
|
|
381
390
|
return moduleDef;
|
|
382
391
|
}).catch((e) => {
|
|
392
|
+
this.profiler.logOperationStart({id: import_metrics.MODULE_ERROR, specifier});
|
|
383
393
|
throw e;
|
|
384
394
|
});
|
|
385
395
|
}
|
|
@@ -4,14 +4,25 @@
|
|
|
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 ESM Module Loader v0.6.
|
|
7
|
+
/* LWR ESM Module Loader v0.6.2 */
|
|
8
8
|
/**
|
|
9
9
|
* Simplified version of the AMD Import Metadata Resolver.
|
|
10
10
|
* Just reads the ImportMetadata at construction time.
|
|
11
11
|
*/
|
|
12
12
|
class ImportResolver {
|
|
13
|
-
constructor(
|
|
13
|
+
constructor(config) {
|
|
14
|
+
var _a, _b;
|
|
14
15
|
this.importURICache = new Map();
|
|
16
|
+
this.modifiers = '';
|
|
17
|
+
this.normalizeMetadata(config);
|
|
18
|
+
// only fetch mappings if fingerprints is ON
|
|
19
|
+
this.mappingEndpoint = (config === null || config === void 0 ? void 0 : config.importMappings) ? undefined : (_a = config === null || config === void 0 ? void 0 : config.endpoints) === null || _a === void 0 ? void 0 : _a.uris.mapping;
|
|
20
|
+
if ((_b = config === null || config === void 0 ? void 0 : config.endpoints) === null || _b === void 0 ? void 0 : _b.modifiers) {
|
|
21
|
+
// Add URI modifiers to mapping endpoint query
|
|
22
|
+
this.modifiers = Object.entries(config.endpoints.modifiers).reduce((q, [k, v]) => (q += `${k}=${v}&`), '?');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
normalizeMetadata(importMetada) {
|
|
15
26
|
// Normalize the URI cache to optimize retrieval
|
|
16
27
|
if (importMetada && importMetada.imports) {
|
|
17
28
|
for (const [uri, value] of Object.entries(importMetada.imports)) {
|
|
@@ -24,8 +35,21 @@ class ImportResolver {
|
|
|
24
35
|
}
|
|
25
36
|
}
|
|
26
37
|
}
|
|
27
|
-
|
|
28
|
-
|
|
38
|
+
async fetchMappings(specifier) {
|
|
39
|
+
const mappingUri = `${this.mappingEndpoint}${encodeURIComponent(specifier)}${this.modifiers}`;
|
|
40
|
+
const res = await globalThis.fetch(mappingUri);
|
|
41
|
+
if (res.ok) {
|
|
42
|
+
const mappings = await res.json();
|
|
43
|
+
this.normalizeMetadata(mappings);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async resolve(specifier) {
|
|
47
|
+
let uri = this.importURICache.get(specifier);
|
|
48
|
+
if (!uri && this.mappingEndpoint) {
|
|
49
|
+
await this.fetchMappings(specifier);
|
|
50
|
+
uri = this.importURICache.get(specifier);
|
|
51
|
+
}
|
|
52
|
+
return uri;
|
|
29
53
|
}
|
|
30
54
|
}
|
|
31
55
|
|
|
@@ -54,21 +78,21 @@ let resolverLegacy;
|
|
|
54
78
|
function init(config) {
|
|
55
79
|
// Save config from globalThis.LWR
|
|
56
80
|
esmLoaderConfig = config;
|
|
57
|
-
const { imports, index, importMappings } = config;
|
|
58
|
-
resolver = new ImportResolver({ imports, index });
|
|
81
|
+
const { imports, index, importMappings, endpoints } = config;
|
|
82
|
+
resolver = new ImportResolver({ imports, index, endpoints, importMappings });
|
|
59
83
|
resolverLegacy = new ImportResolverLegacy(importMappings);
|
|
60
84
|
}
|
|
61
|
-
function load(specifier, importer) {
|
|
62
|
-
const uri = resolveUrl(specifier, importer);
|
|
85
|
+
async function load(specifier, importer) {
|
|
86
|
+
const uri = await resolveUrl(specifier, importer);
|
|
63
87
|
return import(uri);
|
|
64
88
|
}
|
|
65
|
-
function resolveUrl(specifier, importer) {
|
|
89
|
+
async function resolveUrl(specifier, importer) {
|
|
66
90
|
let uri;
|
|
67
91
|
if (!resolver || !resolverLegacy) {
|
|
68
92
|
throw new Error('The ESM Loader was not initialized');
|
|
69
93
|
}
|
|
70
94
|
// Check if the URI is in the import metadata
|
|
71
|
-
uri = resolver.resolve(specifier);
|
|
95
|
+
uri = await resolver.resolve(specifier);
|
|
72
96
|
if (uri) {
|
|
73
97
|
return uri;
|
|
74
98
|
}
|
|
@@ -88,6 +112,10 @@ function resolveUrl(specifier, importer) {
|
|
|
88
112
|
if (importer) {
|
|
89
113
|
uri += `?importer=${encodeURIComponent(importer)}`;
|
|
90
114
|
}
|
|
115
|
+
if (endpoints.modifiers) {
|
|
116
|
+
// Add URI modifiers to query
|
|
117
|
+
uri += Object.entries(endpoints.modifiers).reduce((q, [k, v]) => (q += `${k}=${v}&`), importer ? '&' : '?');
|
|
118
|
+
}
|
|
91
119
|
}
|
|
92
120
|
}
|
|
93
121
|
return uri;
|
|
@@ -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 v0.6.
|
|
7
|
+
/* LWR Module Loader v0.6.2 */
|
|
8
8
|
const templateRegex = /\{([0-9]+)\}/g;
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
10
|
function templateString(template, args) {
|
|
@@ -300,12 +300,15 @@ if (hasDocument) {
|
|
|
300
300
|
});
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const
|
|
306
|
-
const
|
|
307
|
-
const
|
|
308
|
-
const
|
|
303
|
+
// Bootstrap / shim
|
|
304
|
+
|
|
305
|
+
const LOADER_PREFIX = 'lwr.loader.';
|
|
306
|
+
const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
|
|
307
|
+
const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
|
|
308
|
+
const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
|
|
309
|
+
|
|
310
|
+
const MAPPINGS_FETCH = `${LOADER_PREFIX}mappings.fetch`;
|
|
311
|
+
const MAPPINGS_ERROR = `${LOADER_PREFIX}mappings.error`;
|
|
309
312
|
|
|
310
313
|
/* spec based import map resolver */
|
|
311
314
|
class ImportMetadataResolver {
|
|
@@ -422,7 +425,7 @@ class ImportMetadataResolver {
|
|
|
422
425
|
if (pending) {
|
|
423
426
|
return pending;
|
|
424
427
|
}
|
|
425
|
-
this.config.profiler.logOperationStart({ id:
|
|
428
|
+
this.config.profiler.logOperationStart({ id: MAPPINGS_FETCH, specifier });
|
|
426
429
|
const fetchMappingService = this.hasMappingHooks()
|
|
427
430
|
? this.evaluateMappingHooks
|
|
428
431
|
: this.fetchNewMappings;
|
|
@@ -437,6 +440,7 @@ class ImportMetadataResolver {
|
|
|
437
440
|
if (!uri) {
|
|
438
441
|
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
439
442
|
}
|
|
443
|
+
this.config.profiler.logOperationEnd({ id: MAPPINGS_FETCH, specifier });
|
|
440
444
|
return uri;
|
|
441
445
|
})
|
|
442
446
|
.finally(() => {
|
|
@@ -481,7 +485,7 @@ class ImportMetadataResolver {
|
|
|
481
485
|
const uri = resolveUrl(this.buildMappingUrl(specifier), this.getBaseUrl());
|
|
482
486
|
return globalThis.fetch(uri).then((res) => {
|
|
483
487
|
if (!res.ok) {
|
|
484
|
-
this.config.profiler.logOperationStart({ id:
|
|
488
|
+
this.config.profiler.logOperationStart({ id: MAPPINGS_ERROR, specifier });
|
|
485
489
|
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
486
490
|
}
|
|
487
491
|
return res
|
|
@@ -535,6 +539,8 @@ function evaluateHandleStaleModuleHooks(handleStaleModuleHooks, hookArgs) {
|
|
|
535
539
|
}
|
|
536
540
|
}
|
|
537
541
|
|
|
542
|
+
const MODULE_LOAD_TIMEOUT_TIMER = 300000;
|
|
543
|
+
|
|
538
544
|
let lastWindowError;
|
|
539
545
|
if (hasDocument) {
|
|
540
546
|
globalThis.addEventListener('error', (evt) => {
|
|
@@ -749,7 +755,7 @@ class ModuleRegistry {
|
|
|
749
755
|
// if module is "external", resolve the external promise to notify any dependees
|
|
750
756
|
mod.external.resolveExternal(moduleDef);
|
|
751
757
|
}
|
|
752
|
-
this.profiler.logOperationStart({ id:
|
|
758
|
+
this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
|
|
753
759
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
754
760
|
this.lastDefine = moduleDef;
|
|
755
761
|
}
|
|
@@ -932,6 +938,9 @@ class ModuleRegistry {
|
|
|
932
938
|
if (moduleExports.__defaultInterop) {
|
|
933
939
|
Object.defineProperty(moduleRecord.module, '__defaultInterop', { value: true });
|
|
934
940
|
}
|
|
941
|
+
if (moduleExports.__esModule) {
|
|
942
|
+
Object.defineProperty(moduleRecord.module, '__esModule', { value: true });
|
|
943
|
+
}
|
|
935
944
|
moduleRecord.evaluated = true;
|
|
936
945
|
Object.freeze(moduleRecord.module);
|
|
937
946
|
return moduleRecord.module;
|
|
@@ -980,7 +989,7 @@ class ModuleRegistry {
|
|
|
980
989
|
}
|
|
981
990
|
const parentUrl = this.resolver.getBaseUrl(); // only support baseUrl for now
|
|
982
991
|
const specifier = moduleName || originalId;
|
|
983
|
-
this.profiler.logOperationStart({ id:
|
|
992
|
+
this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
|
|
984
993
|
return Promise.resolve()
|
|
985
994
|
.then(async () => {
|
|
986
995
|
const loadHooks = this.loadHook;
|
|
@@ -1018,10 +1027,11 @@ class ModuleRegistry {
|
|
|
1018
1027
|
if (!moduleDef) {
|
|
1019
1028
|
throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
|
|
1020
1029
|
}
|
|
1030
|
+
this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
|
|
1021
1031
|
return moduleDef;
|
|
1022
1032
|
})
|
|
1023
1033
|
.catch((e) => {
|
|
1024
|
-
this.profiler.logOperationStart({ id:
|
|
1034
|
+
this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
|
|
1025
1035
|
throw e;
|
|
1026
1036
|
});
|
|
1027
1037
|
}
|
|
@@ -1105,12 +1115,25 @@ class Loader {
|
|
|
1105
1115
|
logOperationStart: () => {
|
|
1106
1116
|
/* noop */
|
|
1107
1117
|
},
|
|
1118
|
+
logOperationEnd: () => {
|
|
1119
|
+
/* noop */
|
|
1120
|
+
},
|
|
1108
1121
|
};
|
|
1109
1122
|
}
|
|
1110
1123
|
this.registry = new ModuleRegistry(Object.freeze({ endpoints: config.endpoints, baseUrl, profiler }));
|
|
1124
|
+
// TODO: W-10539691 - temp workaround for LWR-Java -- remove once appId is implemented there
|
|
1125
|
+
if (config.appMetadata && !config.appMetadata.appId) {
|
|
1126
|
+
// Parse the appId from the bootstrapModule
|
|
1127
|
+
// LWR-Java bootstrap module format: @lwr-bootstrap/my/app/v/0_0_1 -- my/app is the appId
|
|
1128
|
+
const match = config.appMetadata.bootstrapModule.match(/@lwr-bootstrap\/(.+)\/v\/.+/);
|
|
1129
|
+
const appId = match && match[1];
|
|
1130
|
+
config.appMetadata.appId = appId;
|
|
1131
|
+
}
|
|
1132
|
+
// TODO: https://github.com/salesforce/lwr/issues/1087
|
|
1111
1133
|
this.services = Object.freeze({
|
|
1112
1134
|
addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
|
|
1113
1135
|
handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
|
|
1136
|
+
appMetadata: config.appMetadata,
|
|
1114
1137
|
});
|
|
1115
1138
|
}
|
|
1116
1139
|
/**
|
|
@@ -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 Legacy Module Loader v0.6.
|
|
7
|
+
/* LWR Legacy Module Loader v0.6.2 */
|
|
8
8
|
const templateRegex = /\{([0-9]+)\}/g;
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
10
|
function templateString(template, args) {
|
|
@@ -401,14 +401,22 @@ function evaluateHandleStaleModuleHooks(handleStaleModuleHooks, hookArgs) {
|
|
|
401
401
|
}
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
+
// Bootstrap / shim
|
|
405
|
+
|
|
406
|
+
const LOADER_PREFIX = 'lwr.loader.';
|
|
407
|
+
const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
|
|
408
|
+
const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
|
|
409
|
+
const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
|
|
410
|
+
|
|
404
411
|
/* global console,process */
|
|
405
412
|
class ModuleRegistry {
|
|
406
|
-
constructor(
|
|
413
|
+
constructor(config) {
|
|
407
414
|
// A registry for named AMD defines containing the *metadata* of AMD module
|
|
408
415
|
this.namedDefineRegistry = new Map();
|
|
409
416
|
// The evaluted module registry where the module identifier (name or URL?) is the key
|
|
410
417
|
this.moduleRegistry = new Map();
|
|
411
|
-
this.baseUrl = baseUrl;
|
|
418
|
+
this.baseUrl = config.baseUrl;
|
|
419
|
+
this.profiler = config.profiler;
|
|
412
420
|
}
|
|
413
421
|
async load(id, importer) {
|
|
414
422
|
const resolvedId = await this.resolve(id, importer);
|
|
@@ -523,6 +531,7 @@ class ModuleRegistry {
|
|
|
523
531
|
// if module is "external", resolve the external promise to notify any dependees
|
|
524
532
|
mod.external.resolveExternal(moduleDef);
|
|
525
533
|
}
|
|
534
|
+
this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
|
|
526
535
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
527
536
|
this.lastDefine = moduleDef;
|
|
528
537
|
// Check signatures of dependencies against those in the namedDefineRegistry
|
|
@@ -743,6 +752,9 @@ class ModuleRegistry {
|
|
|
743
752
|
if (moduleExports.__defaultInterop) {
|
|
744
753
|
Object.defineProperty(moduleRecord.module, '__defaultInterop', { value: true });
|
|
745
754
|
}
|
|
755
|
+
if (moduleExports.__esModule) {
|
|
756
|
+
Object.defineProperty(moduleRecord.module, '__esModule', { value: true });
|
|
757
|
+
}
|
|
746
758
|
moduleRecord.evaluated = true;
|
|
747
759
|
Object.freeze(moduleRecord.module);
|
|
748
760
|
return moduleRecord.module;
|
|
@@ -790,6 +802,8 @@ class ModuleRegistry {
|
|
|
790
802
|
return moduleDef;
|
|
791
803
|
}
|
|
792
804
|
const parentUrl = this.baseUrl; // only support baseUrl for now
|
|
805
|
+
const specifier = moduleName || originalId;
|
|
806
|
+
this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
|
|
793
807
|
return Promise.resolve()
|
|
794
808
|
.then(async () => {
|
|
795
809
|
const loadHooks = this.loadHook;
|
|
@@ -827,9 +841,11 @@ class ModuleRegistry {
|
|
|
827
841
|
if (!moduleDef) {
|
|
828
842
|
throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
|
|
829
843
|
}
|
|
844
|
+
this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
|
|
830
845
|
return moduleDef;
|
|
831
846
|
})
|
|
832
847
|
.catch((e) => {
|
|
848
|
+
this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
|
|
833
849
|
throw e;
|
|
834
850
|
});
|
|
835
851
|
}
|
|
@@ -1057,7 +1073,10 @@ async function evaluateImportMaps(baseUrl) {
|
|
|
1057
1073
|
* The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
|
|
1058
1074
|
*/
|
|
1059
1075
|
class Loader {
|
|
1060
|
-
constructor(
|
|
1076
|
+
constructor(config) {
|
|
1077
|
+
config = config || {};
|
|
1078
|
+
let baseUrl = config.baseUrl;
|
|
1079
|
+
let profiler = config.profiler;
|
|
1061
1080
|
if (baseUrl) {
|
|
1062
1081
|
// add a trailing slash, if it does not exist
|
|
1063
1082
|
baseUrl = baseUrl.replace(/\/?$/, '/');
|
|
@@ -1069,10 +1088,23 @@ class Loader {
|
|
|
1069
1088
|
throw new LoaderError(NO_BASE_URL);
|
|
1070
1089
|
}
|
|
1071
1090
|
this.baseUrl = baseUrl;
|
|
1072
|
-
|
|
1091
|
+
if (!profiler) {
|
|
1092
|
+
// default noop profiler
|
|
1093
|
+
profiler = {
|
|
1094
|
+
logOperationStart: () => {
|
|
1095
|
+
/* noop */
|
|
1096
|
+
},
|
|
1097
|
+
logOperationEnd: () => {
|
|
1098
|
+
/* noop */
|
|
1099
|
+
},
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
this.registry = new ModuleRegistry({ baseUrl, profiler });
|
|
1103
|
+
// TODO: https://github.com/salesforce/lwr/issues/1087
|
|
1073
1104
|
this.services = Object.freeze({
|
|
1074
1105
|
addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
|
|
1075
1106
|
handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
|
|
1107
|
+
appMetadata: config.appMetadata,
|
|
1076
1108
|
});
|
|
1077
1109
|
}
|
|
1078
1110
|
/**
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.6.
|
|
8
|
+
"version": "0.6.2",
|
|
9
9
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"import": "./build/index.js",
|
|
25
25
|
"require": "./build/cjs/index.cjs"
|
|
26
26
|
},
|
|
27
|
+
"./metrics": {
|
|
28
|
+
"import": "./build/metrics.js",
|
|
29
|
+
"require": "./build/cjs/metrics.cjs"
|
|
30
|
+
},
|
|
27
31
|
"./prod-loader-shim": "./build/assets/prod/lwr-loader-shim.js",
|
|
28
32
|
"./prod-loader-shim-bundle": "./build/assets/prod/lwr-loader-shim.bundle.js",
|
|
29
33
|
"./prod-loader-shim-bundle-min": "./build/assets/prod/lwr-loader-shim.bundle.min.js",
|
|
@@ -51,16 +55,17 @@
|
|
|
51
55
|
"build:shim:bundle:minify": "MINIFY=true yarn build:shim:bundle",
|
|
52
56
|
"build:shim": "rollup --config scripts/rollup-shim.config.cjs",
|
|
53
57
|
"build:loader": "rollup --config scripts/rollup.config.cjs",
|
|
54
|
-
"build": "tsc -b
|
|
58
|
+
"build:ts": "tsc -b",
|
|
59
|
+
"build": "yarn build:ts && yarn build:shim && yarn build:loader && yarn build:shim:bundle && yarn build:shim:bundle:minify"
|
|
55
60
|
},
|
|
56
61
|
"devDependencies": {
|
|
57
|
-
"@lwrjs/compiler": "0.6.
|
|
58
|
-
"@lwrjs/diagnostics": "0.6.
|
|
59
|
-
"@lwrjs/types": "0.6.
|
|
62
|
+
"@lwrjs/compiler": "0.6.2",
|
|
63
|
+
"@lwrjs/diagnostics": "0.6.2",
|
|
64
|
+
"@lwrjs/types": "0.6.2",
|
|
60
65
|
"rollup-plugin-terser": "^7.0.2"
|
|
61
66
|
},
|
|
62
67
|
"dependencies": {
|
|
63
|
-
"@lwrjs/shared-utils": "0.6.
|
|
68
|
+
"@lwrjs/shared-utils": "0.6.2"
|
|
64
69
|
},
|
|
65
70
|
"lwc": {
|
|
66
71
|
"modules": [
|
|
@@ -77,5 +82,5 @@
|
|
|
77
82
|
"engines": {
|
|
78
83
|
"node": ">=14.15.4 <17"
|
|
79
84
|
},
|
|
80
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "ef85bdc48adde58b7c648561d67acbb408bbe189"
|
|
81
86
|
}
|