@lwrjs/loader 0.13.0-alpha.2 → 0.13.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/assets/prod/lwr-error-shim.js +2 -2
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.js +34 -29
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +3 -3
- package/build/assets/prod/lwr-loader-shim-legacy.js +21 -16
- package/build/assets/prod/lwr-loader-shim.bundle.js +45 -23
- package/build/assets/prod/lwr-loader-shim.bundle.min.js +3 -3
- package/build/assets/prod/lwr-loader-shim.js +27 -12
- package/build/bundle/prod/lwr/esmLoader/esmLoader.js +1 -1
- package/build/cjs/index.cjs +28 -1
- package/build/cjs/modules/lwr/esmLoader/esmLoader.cjs +2 -2
- package/build/cjs/modules/lwr/esmLoader/importResolver.cjs +0 -17
- package/build/cjs/modules/lwr/esmLoader/importResolverLegacy.cjs +1 -18
- package/build/cjs/modules/lwr/loader/errors/messages.cjs +7 -1
- package/build/cjs/modules/lwr/loader/errors/reportError.cjs +1 -2
- package/build/cjs/modules/lwr/loader/hooks/moduleInvalidation.cjs +2 -3
- package/build/cjs/modules/lwr/loader/hooks/resolveAndLoadHook.cjs +3 -4
- package/build/cjs/modules/lwr/loader/moduleRegistry/importMetadataResolver.cjs +7 -6
- package/build/cjs/modules/lwr/loader/moduleRegistry/moduleRegistry.cjs +8 -10
- package/build/cjs/modules/lwr/loader/moduleRegistry/scriptLoad.cjs +2 -2
- package/build/cjs/modules/lwr/loader/utils/url.cjs +1 -1
- package/build/cjs/modules/lwr/loaderLegacy/errors/messages.cjs +1 -1
- package/build/cjs/modules/lwr/loaderLegacy/errors/reportError.cjs +1 -2
- package/build/cjs/modules/lwr/loaderLegacy/hooks/moduleInvalidation.cjs +2 -3
- package/build/cjs/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.cjs +3 -4
- package/build/cjs/modules/lwr/loaderLegacy/importMap/dom.cjs +4 -4
- package/build/cjs/modules/lwr/loaderLegacy/importMap/importMap.cjs +2 -2
- package/build/cjs/modules/lwr/loaderLegacy/importMap/importMapResolver.cjs +1 -2
- package/build/cjs/modules/lwr/loaderLegacy/importMap/utils.cjs +1 -1
- package/build/cjs/modules/lwr/loaderLegacy/importResolver/importResolver.cjs +0 -17
- package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.cjs +7 -10
- package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.cjs +2 -2
- package/build/cjs/modules/lwr/loaderLegacy/utils/url.cjs +1 -1
- package/build/cjs/package.d.cjs +0 -0
- package/build/cjs/types.cjs +5 -0
- package/build/error-shim/index.d.ts +2 -0
- package/build/error-shim/index.js +17 -0
- package/build/index.d.ts +1 -1
- package/build/index.js +28 -1
- package/build/modules/lwr/esmLoader/esmLoader.d.ts +6 -0
- package/build/modules/lwr/esmLoader/esmLoader.js +2 -2
- package/build/modules/lwr/esmLoader/importResolver.d.ts +17 -0
- package/build/modules/lwr/esmLoader/importResolver.js +47 -0
- package/build/modules/lwr/esmLoader/importResolverLegacy.d.ts +16 -0
- package/build/modules/lwr/esmLoader/importResolverLegacy.js +18 -0
- package/build/modules/lwr/loader/constants/constants.d.ts +2 -0
- package/build/modules/lwr/loader/constants/constants.js +2 -0
- package/build/modules/lwr/loader/errors/messages.d.ts +33 -0
- package/build/modules/lwr/loader/errors/messages.js +128 -0
- package/build/modules/lwr/loader/errors/reportError.d.ts +4 -0
- package/build/modules/lwr/loader/errors/reportError.js +9 -0
- package/build/modules/lwr/loader/errors/utils.d.ts +2 -0
- package/build/modules/lwr/loader/errors/utils.js +8 -0
- package/build/modules/lwr/loader/hooks/moduleInvalidation.d.ts +3 -0
- package/build/modules/lwr/loader/hooks/moduleInvalidation.js +19 -0
- package/build/modules/lwr/loader/hooks/resolveAndLoadHook.d.ts +7 -0
- package/build/modules/lwr/loader/hooks/resolveAndLoadHook.js +94 -0
- package/build/modules/lwr/loader/loader.d.ts +53 -0
- package/build/modules/lwr/loader/loader.js +18 -11
- package/build/modules/lwr/loader/moduleRegistry/importMetadataResolver.d.ts +44 -0
- package/build/modules/lwr/loader/moduleRegistry/importMetadataResolver.js +211 -0
- package/build/modules/lwr/loader/moduleRegistry/moduleRegistry.d.ts +47 -0
- package/build/modules/lwr/loader/moduleRegistry/moduleRegistry.js +530 -0
- package/build/modules/lwr/loader/moduleRegistry/scriptLoad.d.ts +3 -0
- package/build/modules/lwr/loader/moduleRegistry/scriptLoad.js +43 -0
- package/build/modules/lwr/loader/utils/dom.d.ts +4 -0
- package/build/modules/lwr/loader/utils/dom.js +6 -0
- package/build/modules/lwr/loader/utils/url.d.ts +28 -0
- package/build/modules/lwr/loader/utils/url.js +128 -0
- package/build/modules/lwr/loaderLegacy/constants/constants.d.ts +2 -0
- package/build/modules/lwr/loaderLegacy/constants/constants.js +2 -0
- package/build/modules/lwr/loaderLegacy/errors/messages.d.ts +30 -0
- package/build/modules/lwr/loaderLegacy/errors/messages.js +113 -0
- package/build/modules/lwr/loaderLegacy/errors/reportError.d.ts +4 -0
- package/build/modules/lwr/loaderLegacy/errors/reportError.js +9 -0
- package/build/modules/lwr/loaderLegacy/errors/utils.d.ts +2 -0
- package/build/modules/lwr/loaderLegacy/errors/utils.js +8 -0
- package/build/modules/lwr/loaderLegacy/hooks/moduleInvalidation.d.ts +3 -0
- package/build/modules/lwr/loaderLegacy/hooks/moduleInvalidation.js +19 -0
- package/build/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.d.ts +7 -0
- package/build/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.js +94 -0
- package/build/modules/lwr/loaderLegacy/importMap/dom.d.ts +10 -0
- package/build/modules/lwr/loaderLegacy/importMap/dom.js +63 -0
- package/build/modules/lwr/loaderLegacy/importMap/importMap.d.ts +22 -0
- package/build/modules/lwr/loaderLegacy/importMap/importMap.js +108 -0
- package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.d.ts +8 -0
- package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.js +11 -0
- package/build/modules/lwr/loaderLegacy/importMap/utils.d.ts +4 -0
- package/build/modules/lwr/loaderLegacy/importMap/utils.js +23 -0
- package/build/modules/lwr/loaderLegacy/importResolver/importResolver.d.ts +6 -0
- package/build/modules/lwr/loaderLegacy/importResolver/importResolver.js +2 -0
- package/build/modules/lwr/loaderLegacy/loaderLegacy.d.ts +57 -0
- package/build/modules/lwr/loaderLegacy/loaderLegacy.js +13 -13
- package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.d.ts +53 -0
- package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.js +544 -0
- package/build/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.d.ts +3 -0
- package/build/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.js +43 -0
- package/build/modules/lwr/loaderLegacy/utils/dom.d.ts +4 -0
- package/build/modules/lwr/loaderLegacy/utils/dom.js +6 -0
- package/build/modules/lwr/loaderLegacy/utils/url.d.ts +28 -0
- package/build/modules/lwr/loaderLegacy/utils/url.js +128 -0
- package/build/shim/constants.d.ts +2 -0
- package/build/shim/constants.js +2 -0
- package/build/shim/customInit.d.ts +4 -0
- package/build/shim/customInit.js +29 -0
- package/build/shim/index.d.ts +2 -0
- package/build/shim/index.js +9 -0
- package/build/shim/loader.d.ts +3 -0
- package/build/shim/loader.js +27 -0
- package/build/shim/shim.d.ts +31 -0
- package/build/shim/shim.js +194 -0
- package/build/shim-legacy/index.d.ts +2 -0
- package/build/shim-legacy/index.js +9 -0
- package/build/shim-legacy/loaderLegacy.d.ts +3 -0
- package/build/shim-legacy/loaderLegacy.js +29 -0
- package/build/shim-legacy/shimLegacy.d.ts +31 -0
- package/build/shim-legacy/shimLegacy.js +188 -0
- package/build/types.d.ts +56 -0
- package/build/types.js +2 -0
- package/package.json +9 -10
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { LoaderError, BAD_IMPORT_METADATA, UNRESOLVED, UNRESOLVEABLE_MAPPING_ERROR, } from '../errors/messages.js';
|
|
2
|
+
import { isUrl, resolveUrl } from '../utils/url.js';
|
|
3
|
+
import { MAPPINGS_ERROR, MAPPINGS_FETCH } from 'lwr/metrics';
|
|
4
|
+
/* spec based import map resolver */
|
|
5
|
+
export class ImportMetadataResolver {
|
|
6
|
+
constructor(config, invalidationCallback) {
|
|
7
|
+
// Default to empty mappings
|
|
8
|
+
this.importURICache = new Map();
|
|
9
|
+
this.pendingURICache = new Map();
|
|
10
|
+
this.loadMappingHooks = [];
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.invalidationCallback = invalidationCallback;
|
|
13
|
+
}
|
|
14
|
+
addLoadMappingHook(hook) {
|
|
15
|
+
this.loadMappingHooks.push(hook);
|
|
16
|
+
}
|
|
17
|
+
getMappingEndpoint() {
|
|
18
|
+
return this.config.endpoints && this.config.endpoints.uris
|
|
19
|
+
? this.config.endpoints.uris.mapping
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
getModifiersAsUrlParams() {
|
|
23
|
+
const modifiers = this.config.endpoints ? this.config.endpoints.modifiers : undefined;
|
|
24
|
+
if (!modifiers) {
|
|
25
|
+
// No modifiers return an empty string to append to the URL
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const qs = Object.keys(modifiers)
|
|
30
|
+
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(modifiers[key])}`)
|
|
31
|
+
.join('&');
|
|
32
|
+
return `?${qs}`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
buildMappingUrl(specifier) {
|
|
36
|
+
const mappingEndpoint = this.getMappingEndpoint();
|
|
37
|
+
const specifiers = encodeURIComponent(specifier);
|
|
38
|
+
const modifiers = this.getModifiersAsUrlParams();
|
|
39
|
+
return `${mappingEndpoint}${specifiers}${modifiers}`;
|
|
40
|
+
}
|
|
41
|
+
getBaseUrl() {
|
|
42
|
+
return this.config.baseUrl || '';
|
|
43
|
+
}
|
|
44
|
+
registerImportMappings(newImportMetadata, rootSpecifiers) {
|
|
45
|
+
if (!rootSpecifiers || rootSpecifiers.length === 0) {
|
|
46
|
+
const imports = newImportMetadata ? JSON.stringify(newImportMetadata) : 'undefined';
|
|
47
|
+
throw new LoaderError(BAD_IMPORT_METADATA, [imports, rootSpecifiers ? '[]' : 'undefined']);
|
|
48
|
+
}
|
|
49
|
+
if (!newImportMetadata) {
|
|
50
|
+
throw new LoaderError(BAD_IMPORT_METADATA, ['undefined', JSON.stringify(rootSpecifiers)]);
|
|
51
|
+
}
|
|
52
|
+
if (!newImportMetadata.imports || Object.keys(newImportMetadata.imports).length === 0) {
|
|
53
|
+
throw new LoaderError(BAD_IMPORT_METADATA, [
|
|
54
|
+
JSON.stringify(newImportMetadata),
|
|
55
|
+
JSON.stringify(rootSpecifiers),
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
const index = newImportMetadata.index || {};
|
|
59
|
+
for (const [uri, specifiers] of Object.entries(newImportMetadata.imports)) {
|
|
60
|
+
specifiers.forEach((specifier) => {
|
|
61
|
+
const indexValue = index[specifier];
|
|
62
|
+
const existing = this.importURICache.get(specifier);
|
|
63
|
+
if (!existing) {
|
|
64
|
+
this.saveImportURIRecord(specifier, uri, indexValue, rootSpecifiers.includes(specifier));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const identity = indexValue || uri;
|
|
68
|
+
const existingIdentity = existing.identity || existing.uri;
|
|
69
|
+
if (existingIdentity !== identity) {
|
|
70
|
+
this.invalidationCallback({
|
|
71
|
+
name: specifier,
|
|
72
|
+
oldUrl: existingIdentity,
|
|
73
|
+
newUrl: identity,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Get URL from the local cache or return undefiend
|
|
81
|
+
getURI(specifier) {
|
|
82
|
+
return this.importURICache && this.importURICache.has(specifier)
|
|
83
|
+
? resolveUrl(this.importURICache.get(specifier).uri, this.getBaseUrl())
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
resolveLocal(specifier) {
|
|
87
|
+
const uri = this.getURI(specifier);
|
|
88
|
+
if (uri) {
|
|
89
|
+
return uri;
|
|
90
|
+
}
|
|
91
|
+
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
92
|
+
return specifier;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Resolves a the URI for a specified module. It will return the value in this order:
|
|
98
|
+
*
|
|
99
|
+
* 1) Mapping from local URI cache
|
|
100
|
+
* 2) The URI if a specifier is already an absolute URI
|
|
101
|
+
* 3) Mapping fetched from a registered loader hook
|
|
102
|
+
* 4)
|
|
103
|
+
* @param specifier
|
|
104
|
+
* @returns module URI
|
|
105
|
+
*/
|
|
106
|
+
async resolve(specifier) {
|
|
107
|
+
let uri = this.getURI(specifier);
|
|
108
|
+
if (uri) {
|
|
109
|
+
return uri;
|
|
110
|
+
}
|
|
111
|
+
else if (isUrl(specifier) || specifier.startsWith('/')) {
|
|
112
|
+
return specifier;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const pending = this.pendingURICache.get(specifier);
|
|
116
|
+
if (pending) {
|
|
117
|
+
return pending;
|
|
118
|
+
}
|
|
119
|
+
this.config.profiler.logOperationStart({ id: MAPPINGS_FETCH, specifier });
|
|
120
|
+
const fetchMappingService = this.hasMappingHooks()
|
|
121
|
+
? this.evaluateMappingHooks
|
|
122
|
+
: this.fetchNewMappings;
|
|
123
|
+
const promise = fetchMappingService
|
|
124
|
+
.bind(this)(specifier)
|
|
125
|
+
.then((importMetadata) => {
|
|
126
|
+
if (!importMetadata || !importMetadata.imports) {
|
|
127
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
128
|
+
}
|
|
129
|
+
this.registerImportMappings(importMetadata, [specifier]);
|
|
130
|
+
uri = this.getURI(specifier);
|
|
131
|
+
if (!uri) {
|
|
132
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
133
|
+
}
|
|
134
|
+
this.config.profiler.logOperationEnd({ id: MAPPINGS_FETCH, specifier });
|
|
135
|
+
return uri;
|
|
136
|
+
})
|
|
137
|
+
.finally(() => {
|
|
138
|
+
this.pendingURICache.delete(specifier);
|
|
139
|
+
});
|
|
140
|
+
this.pendingURICache.set(specifier, promise);
|
|
141
|
+
return promise;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
hasMappingHooks() {
|
|
145
|
+
return this.loadMappingHooks.length > 0;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Evaluates mapping hooks. Returns first match. If all hooks return null call the mapping service.
|
|
149
|
+
* @param specifier Request module identifier
|
|
150
|
+
* @returns Import Metadata from the module root
|
|
151
|
+
*/
|
|
152
|
+
async evaluateMappingHooks(specifier) {
|
|
153
|
+
// Check with any registered loadMappingHooks
|
|
154
|
+
const loadMappingHooks = this.loadMappingHooks;
|
|
155
|
+
if (loadMappingHooks.length) {
|
|
156
|
+
const knownModules = Array.from(this.importURICache.keys());
|
|
157
|
+
for (let i = 0; i < loadMappingHooks.length; i++) {
|
|
158
|
+
const loadMappingHook = loadMappingHooks[i];
|
|
159
|
+
// eslint-disable-next-line no-await-in-loop
|
|
160
|
+
const response = await loadMappingHook(specifier, { knownModules });
|
|
161
|
+
// undefined (but not null) is considered an un expected response so we will stop processing hooks here and throw an error
|
|
162
|
+
if (response || response === undefined) {
|
|
163
|
+
return response;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// If we still do not have a match call the mapping service
|
|
168
|
+
return this.fetchNewMappings(specifier);
|
|
169
|
+
}
|
|
170
|
+
async fetchNewMappings(specifier) {
|
|
171
|
+
if (typeof globalThis.fetch !== 'function') {
|
|
172
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
173
|
+
}
|
|
174
|
+
// TODO For module invalidation with bundles it is recommended we have to send back all loaded root specified
|
|
175
|
+
// to ensure we detect all conflicts.
|
|
176
|
+
const uri = resolveUrl(this.buildMappingUrl(specifier), this.getBaseUrl());
|
|
177
|
+
if (!uri) {
|
|
178
|
+
throw new LoaderError(UNRESOLVEABLE_MAPPING_ERROR, [specifier]);
|
|
179
|
+
}
|
|
180
|
+
return globalThis.fetch(uri).then((res) => {
|
|
181
|
+
if (!res.ok) {
|
|
182
|
+
this.config.profiler.logOperationStart({ id: MAPPINGS_ERROR, specifier });
|
|
183
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
184
|
+
}
|
|
185
|
+
return res
|
|
186
|
+
.json()
|
|
187
|
+
.then((ret) => {
|
|
188
|
+
return ret;
|
|
189
|
+
})
|
|
190
|
+
.catch((err) => {
|
|
191
|
+
throw new LoaderError(UNRESOLVED, [specifier]);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
saveImportURIRecord(specifier, uri, identity, isRoot) {
|
|
196
|
+
if (!identity || uri === identity) {
|
|
197
|
+
this.importURICache.set(specifier, {
|
|
198
|
+
uri,
|
|
199
|
+
isRoot: isRoot,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
this.importURICache.set(specifier, {
|
|
204
|
+
uri,
|
|
205
|
+
identity,
|
|
206
|
+
isRoot: isRoot,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=importMetadataResolver.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ImportMetadataResolver } from './importMetadataResolver.js';
|
|
2
|
+
import type { HandleStaleModuleHook, LoaderHooks, ResolveHookResponse } from '@lwrjs/types';
|
|
3
|
+
import type { LoaderConfig } from '../../../../types.js';
|
|
4
|
+
export type Module = {
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
__useDefault?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare class ModuleRegistry {
|
|
9
|
+
private profiler;
|
|
10
|
+
constructor(config: LoaderConfig);
|
|
11
|
+
load(id: string, importer?: string): Promise<Module>;
|
|
12
|
+
resolve(id: string, importer?: string): Promise<string>;
|
|
13
|
+
has(id: string): boolean;
|
|
14
|
+
define(name: string, dependencies: string[], exporter: Function): void;
|
|
15
|
+
/**
|
|
16
|
+
* Marks modules as "externally" loaded/provided, so that the loader does not attempt to fetch them.
|
|
17
|
+
*
|
|
18
|
+
* @param modules - list of module identifiers
|
|
19
|
+
*/
|
|
20
|
+
registerExternalModules(modules: string[]): void;
|
|
21
|
+
private resolver;
|
|
22
|
+
private namedDefineRegistry;
|
|
23
|
+
private moduleRegistry;
|
|
24
|
+
aliases: Map<string, string>;
|
|
25
|
+
private lastDefine;
|
|
26
|
+
getImportMetadataResolver(): ImportMetadataResolver;
|
|
27
|
+
private getExistingModuleRecord;
|
|
28
|
+
private getModuleRecord;
|
|
29
|
+
private storeModuleAlias;
|
|
30
|
+
private getModuleDependencyRecord;
|
|
31
|
+
private topLevelEvaluation;
|
|
32
|
+
private instantiateAll;
|
|
33
|
+
private evaluateModule;
|
|
34
|
+
private isNamedExportDefaultOnly;
|
|
35
|
+
private getCircularDependencyWrapper;
|
|
36
|
+
private evaluateModuleDependencies;
|
|
37
|
+
private getModuleDef;
|
|
38
|
+
private resolveHook?;
|
|
39
|
+
private loadHook?;
|
|
40
|
+
private loadMappingHooks?;
|
|
41
|
+
addLoaderPlugin(hooks: LoaderHooks): void;
|
|
42
|
+
private importMetadataInvalidationCallback;
|
|
43
|
+
private handleStaleModuleHook?;
|
|
44
|
+
registerHandleStaleModuleHook(handleStaleModule: HandleStaleModuleHook): void;
|
|
45
|
+
isValidResolveResponse(res: ResolveHookResponse): boolean;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=moduleRegistry.d.ts.map
|