@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.
Files changed (119) hide show
  1. package/build/assets/prod/lwr-error-shim.js +2 -2
  2. package/build/assets/prod/lwr-loader-shim-legacy.bundle.js +34 -29
  3. package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +3 -3
  4. package/build/assets/prod/lwr-loader-shim-legacy.js +21 -16
  5. package/build/assets/prod/lwr-loader-shim.bundle.js +45 -23
  6. package/build/assets/prod/lwr-loader-shim.bundle.min.js +3 -3
  7. package/build/assets/prod/lwr-loader-shim.js +27 -12
  8. package/build/bundle/prod/lwr/esmLoader/esmLoader.js +1 -1
  9. package/build/cjs/index.cjs +28 -1
  10. package/build/cjs/modules/lwr/esmLoader/esmLoader.cjs +2 -2
  11. package/build/cjs/modules/lwr/esmLoader/importResolver.cjs +0 -17
  12. package/build/cjs/modules/lwr/esmLoader/importResolverLegacy.cjs +1 -18
  13. package/build/cjs/modules/lwr/loader/errors/messages.cjs +7 -1
  14. package/build/cjs/modules/lwr/loader/errors/reportError.cjs +1 -2
  15. package/build/cjs/modules/lwr/loader/hooks/moduleInvalidation.cjs +2 -3
  16. package/build/cjs/modules/lwr/loader/hooks/resolveAndLoadHook.cjs +3 -4
  17. package/build/cjs/modules/lwr/loader/moduleRegistry/importMetadataResolver.cjs +7 -6
  18. package/build/cjs/modules/lwr/loader/moduleRegistry/moduleRegistry.cjs +8 -10
  19. package/build/cjs/modules/lwr/loader/moduleRegistry/scriptLoad.cjs +2 -2
  20. package/build/cjs/modules/lwr/loader/utils/url.cjs +1 -1
  21. package/build/cjs/modules/lwr/loaderLegacy/errors/messages.cjs +1 -1
  22. package/build/cjs/modules/lwr/loaderLegacy/errors/reportError.cjs +1 -2
  23. package/build/cjs/modules/lwr/loaderLegacy/hooks/moduleInvalidation.cjs +2 -3
  24. package/build/cjs/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.cjs +3 -4
  25. package/build/cjs/modules/lwr/loaderLegacy/importMap/dom.cjs +4 -4
  26. package/build/cjs/modules/lwr/loaderLegacy/importMap/importMap.cjs +2 -2
  27. package/build/cjs/modules/lwr/loaderLegacy/importMap/importMapResolver.cjs +1 -2
  28. package/build/cjs/modules/lwr/loaderLegacy/importMap/utils.cjs +1 -1
  29. package/build/cjs/modules/lwr/loaderLegacy/importResolver/importResolver.cjs +0 -17
  30. package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.cjs +7 -10
  31. package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.cjs +2 -2
  32. package/build/cjs/modules/lwr/loaderLegacy/utils/url.cjs +1 -1
  33. package/build/cjs/package.d.cjs +0 -0
  34. package/build/cjs/types.cjs +5 -0
  35. package/build/error-shim/index.d.ts +2 -0
  36. package/build/error-shim/index.js +17 -0
  37. package/build/index.d.ts +1 -1
  38. package/build/index.js +28 -1
  39. package/build/modules/lwr/esmLoader/esmLoader.d.ts +6 -0
  40. package/build/modules/lwr/esmLoader/esmLoader.js +2 -2
  41. package/build/modules/lwr/esmLoader/importResolver.d.ts +17 -0
  42. package/build/modules/lwr/esmLoader/importResolver.js +47 -0
  43. package/build/modules/lwr/esmLoader/importResolverLegacy.d.ts +16 -0
  44. package/build/modules/lwr/esmLoader/importResolverLegacy.js +18 -0
  45. package/build/modules/lwr/loader/constants/constants.d.ts +2 -0
  46. package/build/modules/lwr/loader/constants/constants.js +2 -0
  47. package/build/modules/lwr/loader/errors/messages.d.ts +33 -0
  48. package/build/modules/lwr/loader/errors/messages.js +128 -0
  49. package/build/modules/lwr/loader/errors/reportError.d.ts +4 -0
  50. package/build/modules/lwr/loader/errors/reportError.js +9 -0
  51. package/build/modules/lwr/loader/errors/utils.d.ts +2 -0
  52. package/build/modules/lwr/loader/errors/utils.js +8 -0
  53. package/build/modules/lwr/loader/hooks/moduleInvalidation.d.ts +3 -0
  54. package/build/modules/lwr/loader/hooks/moduleInvalidation.js +19 -0
  55. package/build/modules/lwr/loader/hooks/resolveAndLoadHook.d.ts +7 -0
  56. package/build/modules/lwr/loader/hooks/resolveAndLoadHook.js +94 -0
  57. package/build/modules/lwr/loader/loader.d.ts +53 -0
  58. package/build/modules/lwr/loader/loader.js +18 -11
  59. package/build/modules/lwr/loader/moduleRegistry/importMetadataResolver.d.ts +44 -0
  60. package/build/modules/lwr/loader/moduleRegistry/importMetadataResolver.js +211 -0
  61. package/build/modules/lwr/loader/moduleRegistry/moduleRegistry.d.ts +47 -0
  62. package/build/modules/lwr/loader/moduleRegistry/moduleRegistry.js +530 -0
  63. package/build/modules/lwr/loader/moduleRegistry/scriptLoad.d.ts +3 -0
  64. package/build/modules/lwr/loader/moduleRegistry/scriptLoad.js +43 -0
  65. package/build/modules/lwr/loader/utils/dom.d.ts +4 -0
  66. package/build/modules/lwr/loader/utils/dom.js +6 -0
  67. package/build/modules/lwr/loader/utils/url.d.ts +28 -0
  68. package/build/modules/lwr/loader/utils/url.js +128 -0
  69. package/build/modules/lwr/loaderLegacy/constants/constants.d.ts +2 -0
  70. package/build/modules/lwr/loaderLegacy/constants/constants.js +2 -0
  71. package/build/modules/lwr/loaderLegacy/errors/messages.d.ts +30 -0
  72. package/build/modules/lwr/loaderLegacy/errors/messages.js +113 -0
  73. package/build/modules/lwr/loaderLegacy/errors/reportError.d.ts +4 -0
  74. package/build/modules/lwr/loaderLegacy/errors/reportError.js +9 -0
  75. package/build/modules/lwr/loaderLegacy/errors/utils.d.ts +2 -0
  76. package/build/modules/lwr/loaderLegacy/errors/utils.js +8 -0
  77. package/build/modules/lwr/loaderLegacy/hooks/moduleInvalidation.d.ts +3 -0
  78. package/build/modules/lwr/loaderLegacy/hooks/moduleInvalidation.js +19 -0
  79. package/build/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.d.ts +7 -0
  80. package/build/modules/lwr/loaderLegacy/hooks/resolveAndLoadHook.js +94 -0
  81. package/build/modules/lwr/loaderLegacy/importMap/dom.d.ts +10 -0
  82. package/build/modules/lwr/loaderLegacy/importMap/dom.js +63 -0
  83. package/build/modules/lwr/loaderLegacy/importMap/importMap.d.ts +22 -0
  84. package/build/modules/lwr/loaderLegacy/importMap/importMap.js +108 -0
  85. package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.d.ts +8 -0
  86. package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.js +11 -0
  87. package/build/modules/lwr/loaderLegacy/importMap/utils.d.ts +4 -0
  88. package/build/modules/lwr/loaderLegacy/importMap/utils.js +23 -0
  89. package/build/modules/lwr/loaderLegacy/importResolver/importResolver.d.ts +6 -0
  90. package/build/modules/lwr/loaderLegacy/importResolver/importResolver.js +2 -0
  91. package/build/modules/lwr/loaderLegacy/loaderLegacy.d.ts +57 -0
  92. package/build/modules/lwr/loaderLegacy/loaderLegacy.js +13 -13
  93. package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.d.ts +53 -0
  94. package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.js +544 -0
  95. package/build/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.d.ts +3 -0
  96. package/build/modules/lwr/loaderLegacy/moduleRegistry/scriptLoad.js +43 -0
  97. package/build/modules/lwr/loaderLegacy/utils/dom.d.ts +4 -0
  98. package/build/modules/lwr/loaderLegacy/utils/dom.js +6 -0
  99. package/build/modules/lwr/loaderLegacy/utils/url.d.ts +28 -0
  100. package/build/modules/lwr/loaderLegacy/utils/url.js +128 -0
  101. package/build/shim/constants.d.ts +2 -0
  102. package/build/shim/constants.js +2 -0
  103. package/build/shim/customInit.d.ts +4 -0
  104. package/build/shim/customInit.js +29 -0
  105. package/build/shim/index.d.ts +2 -0
  106. package/build/shim/index.js +9 -0
  107. package/build/shim/loader.d.ts +3 -0
  108. package/build/shim/loader.js +27 -0
  109. package/build/shim/shim.d.ts +31 -0
  110. package/build/shim/shim.js +194 -0
  111. package/build/shim-legacy/index.d.ts +2 -0
  112. package/build/shim-legacy/index.js +9 -0
  113. package/build/shim-legacy/loaderLegacy.d.ts +3 -0
  114. package/build/shim-legacy/loaderLegacy.js +29 -0
  115. package/build/shim-legacy/shimLegacy.d.ts +31 -0
  116. package/build/shim-legacy/shimLegacy.js +188 -0
  117. package/build/types.d.ts +56 -0
  118. package/build/types.js +2 -0
  119. package/package.json +9 -10
@@ -0,0 +1,128 @@
1
+ import { templateString } from './utils.js';
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ function generateErrorMessage(errorInfo, args) {
4
+ const message = Array.isArray(args) ? templateString(errorInfo.message, args) : errorInfo.message;
5
+ return `LWR${errorInfo.code}: ${message}`;
6
+ }
7
+ class LoaderError extends Error {
8
+ constructor(errorInfo, errorArgs) {
9
+ super();
10
+ this.message = generateErrorMessage(errorInfo, errorArgs);
11
+ }
12
+ }
13
+ function invariant(condition, errorInfo) {
14
+ if (!condition) {
15
+ throw new LoaderError(errorInfo);
16
+ }
17
+ }
18
+ const MISSING_NAME = Object.freeze({
19
+ code: 3000,
20
+ message: 'A module name is required.',
21
+ level: 0,
22
+ });
23
+ const FAIL_INSTANTIATE = Object.freeze({
24
+ code: 3004,
25
+ message: 'Failed to instantiate module: {0}',
26
+ level: 0,
27
+ });
28
+ const NO_AMD_REQUIRE = Object.freeze({
29
+ code: 3005,
30
+ message: 'AMD require not supported.',
31
+ level: 0,
32
+ });
33
+ const FAILED_DEP = Object.freeze({
34
+ code: 3006,
35
+ level: 0,
36
+ message: 'Failed to load dependency: {0}',
37
+ });
38
+ const INVALID_DEPS = Object.freeze({
39
+ code: 3007,
40
+ message: 'Unexpected value received for dependencies argument; expected an array.',
41
+ level: 0,
42
+ });
43
+ const FAIL_LOAD = Object.freeze({
44
+ code: 3008,
45
+ level: 0,
46
+ message: 'Error loading {0}',
47
+ });
48
+ const UNRESOLVED = Object.freeze({
49
+ code: 3009,
50
+ level: 0,
51
+ message: 'Unable to resolve bare specifier: {0}',
52
+ });
53
+ const NO_BASE_URL = Object.freeze({
54
+ code: 3010,
55
+ level: 0,
56
+ message: 'baseUrl not set',
57
+ });
58
+ const HOOK_ALREADY_SET = Object.freeze({
59
+ code: 3011,
60
+ level: 0,
61
+ message: 'Cannot set a loader service multiple times',
62
+ });
63
+ const INVALID_HOOK = Object.freeze({
64
+ code: 3012,
65
+ level: 0,
66
+ message: 'Invalid hook received',
67
+ });
68
+ const INVALID_LOADER_SERVICE_RESPONSE = Object.freeze({
69
+ code: 3013,
70
+ level: 0,
71
+ message: 'Invalid response received from hook',
72
+ });
73
+ const MODULE_LOAD_TIMEOUT = Object.freeze({
74
+ code: 3014,
75
+ level: 0,
76
+ message: 'Error loading {0} - timed out',
77
+ });
78
+ const HTTP_FAIL_LOAD = Object.freeze({
79
+ code: 3015,
80
+ level: 0,
81
+ message: 'Error loading {0}, status code {1}',
82
+ });
83
+ const STALE_HOOK_ERROR = Object.freeze({
84
+ code: 3016,
85
+ level: 0,
86
+ message: 'An error occurred handling module conflict',
87
+ });
88
+ const MODULE_ALREADY_LOADED = Object.freeze({
89
+ code: 3017,
90
+ level: 0,
91
+ message: 'Marking module(s) as externally loaded, but they are already loaded:',
92
+ });
93
+ const FAIL_HOOK_LOAD = Object.freeze({
94
+ code: 3018,
95
+ level: 0,
96
+ message: 'Error loading "{0}" from hook',
97
+ });
98
+ const NO_MAPPING_URL = Object.freeze({
99
+ code: 3019,
100
+ level: 0,
101
+ message: 'Mapping endpoint not set',
102
+ });
103
+ const BAD_IMPORT_METADATA = Object.freeze({
104
+ code: 3020,
105
+ level: 0,
106
+ message: 'Invalid import metadata: {0} {1}',
107
+ });
108
+ const EXPORTER_ERROR = Object.freeze({
109
+ code: 3021,
110
+ level: 0,
111
+ message: 'Error evaluating module "{0}", error was "{1}"',
112
+ });
113
+ const UNRESOLVEABLE_MAPPING_ERROR = Object.freeze({
114
+ code: 3022,
115
+ level: 0,
116
+ message: 'Unexpected undefined URI resolving mapping for "{0}"',
117
+ });
118
+ /* importMap errors */
119
+ const BAD_IMPORT_MAP = Object.freeze({
120
+ code: 3011,
121
+ level: 0,
122
+ message: 'import map is not valid',
123
+ });
124
+ /* Errors */
125
+ export { MISSING_NAME, FAIL_INSTANTIATE, NO_AMD_REQUIRE, FAILED_DEP, INVALID_DEPS, FAIL_LOAD, UNRESOLVED, NO_BASE_URL, NO_MAPPING_URL, BAD_IMPORT_MAP, BAD_IMPORT_METADATA, HOOK_ALREADY_SET, INVALID_HOOK, INVALID_LOADER_SERVICE_RESPONSE, MODULE_LOAD_TIMEOUT, HTTP_FAIL_LOAD, STALE_HOOK_ERROR, MODULE_ALREADY_LOADED, FAIL_HOOK_LOAD, EXPORTER_ERROR, UNRESOLVEABLE_MAPPING_ERROR, };
126
+ /* API */
127
+ export { invariant, LoaderError };
128
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1,4 @@
1
+ import type { LoaderError } from './messages.js';
2
+ declare function reportError(error: LoaderError): void;
3
+ export { reportError };
4
+ //# sourceMappingURL=reportError.d.ts.map
@@ -0,0 +1,9 @@
1
+ import { hasConsole } from '../utils/dom.js';
2
+ function reportError(error) {
3
+ // TODO eventually this should be configurable instrumentation to send this somewhere
4
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
5
+ if (hasConsole)
6
+ console.error(error);
7
+ }
8
+ export { reportError };
9
+ //# sourceMappingURL=reportError.js.map
@@ -0,0 +1,2 @@
1
+ export declare function templateString(template: string, args: any[]): string;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,8 @@
1
+ const templateRegex = /\{([0-9]+)\}/g;
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ export function templateString(template, args) {
4
+ return template.replace(templateRegex, (_, index) => {
5
+ return args[index];
6
+ });
7
+ }
8
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,3 @@
1
+ import type { HandleStaleModuleHookArgs, HandleStaleModuleHook } from '@lwrjs/types';
2
+ export declare function evaluateHandleStaleModuleHooks(handleStaleModuleHooks: HandleStaleModuleHook[], hookArgs: HandleStaleModuleHookArgs): void;
3
+ //# sourceMappingURL=moduleInvalidation.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { LoaderError, STALE_HOOK_ERROR } from '../errors/messages.js';
2
+ import { reportError } from '../errors/reportError.js';
3
+ export function evaluateHandleStaleModuleHooks(handleStaleModuleHooks, hookArgs) {
4
+ const { name, oldUrl, newUrl } = hookArgs;
5
+ // keep evaluating hooks if return value is null
6
+ for (let i = 0; i < handleStaleModuleHooks.length; i++) {
7
+ const hook = handleStaleModuleHooks[i];
8
+ try {
9
+ const hookResult = hook({ name, oldUrl, newUrl });
10
+ if (hookResult !== null) {
11
+ break;
12
+ }
13
+ }
14
+ catch (e) {
15
+ reportError(new LoaderError(STALE_HOOK_ERROR));
16
+ }
17
+ }
18
+ }
19
+ //# sourceMappingURL=moduleInvalidation.js.map
@@ -0,0 +1,7 @@
1
+ import type { CustomResponse, LoadHookResponse, ResolveHookResponse } from '@lwrjs/types';
2
+ export declare function isCustomResponse(response: CustomResponse | globalThis.Response): response is CustomResponse;
3
+ export declare function isFetchResponse(response: globalThis.Response | CustomResponse): response is globalThis.Response;
4
+ export declare function isResponseAPromise(response: ResolveHookResponse | LoadHookResponse | Promise<ResolveHookResponse> | Promise<LoadHookResponse>): boolean;
5
+ export declare function evaluateLoadHookResponse(response: LoadHookResponse, id: string): Promise<boolean>;
6
+ export declare function evaluateLoadHook(id: string, hookPromise: Promise<LoadHookResponse>): Promise<LoadHookResponse>;
7
+ //# sourceMappingURL=resolveAndLoadHook.d.ts.map
@@ -0,0 +1,94 @@
1
+ /* global console,process */
2
+ import { LoaderError, INVALID_LOADER_SERVICE_RESPONSE, FAIL_LOAD, HTTP_FAIL_LOAD, MODULE_LOAD_TIMEOUT, FAIL_HOOK_LOAD, } from '../errors/messages.js';
3
+ import { MODULE_LOAD_TIMEOUT_TIMER } from '../constants/constants.js';
4
+ import { hasDocument, hasSetTimeout, hasConsole } from '../utils/dom.js';
5
+ // eslint-disable-next-line lwr/only-allowed-imports
6
+ import { trusted } from '@locker/trusted-types';
7
+ let lastWindowError;
8
+ if (hasDocument) {
9
+ globalThis.addEventListener('error', (evt) => {
10
+ lastWindowError = evt.error;
11
+ });
12
+ }
13
+ if (process.env.NODE_ENV !== 'production') {
14
+ if (!hasSetTimeout && hasConsole) {
15
+ // eslint-disable-next-line lwr/no-unguarded-apis
16
+ console.warn('setTimeout API is not available, watchdog timer on load hook will not be set');
17
+ }
18
+ }
19
+ export function isCustomResponse(response) {
20
+ return (Object.prototype.hasOwnProperty.call(response, 'data') &&
21
+ !Object.prototype.hasOwnProperty.call(response, 'blob'));
22
+ }
23
+ export function isFetchResponse(response) {
24
+ // if it quacks like a duck...
25
+ return typeof response.blob === 'function';
26
+ }
27
+ export function isResponseAPromise(response) {
28
+ return !!(response && response.then);
29
+ }
30
+ export async function evaluateLoadHookResponse(response, id) {
31
+ return Promise.resolve().then(async () => {
32
+ if (!response || !response.status) {
33
+ throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
34
+ }
35
+ if (response.status !== 200) {
36
+ throw new LoaderError(HTTP_FAIL_LOAD, [id, `${response.status}`]);
37
+ }
38
+ const isResponse = isFetchResponse(response);
39
+ let code;
40
+ if (isCustomResponse(response)) {
41
+ code = response.data;
42
+ }
43
+ else if (isResponse) {
44
+ // handle fetch response
45
+ code = await response.text();
46
+ }
47
+ else {
48
+ throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
49
+ }
50
+ if (!code) {
51
+ throw new LoaderError(FAIL_LOAD, [id]);
52
+ }
53
+ code = `${code}\n//# sourceURL=${id}`; // append sourceURL for debugging
54
+ try {
55
+ // TODO eval source maps for debugging
56
+ eval(trusted.createScript(code));
57
+ }
58
+ catch (e) {
59
+ if (process.env.NODE_ENV !== 'production' && hasConsole) {
60
+ // eslint-disable-next-line lwr/no-unguarded-apis
61
+ console.error(e);
62
+ }
63
+ throw new LoaderError(FAIL_LOAD, [id]);
64
+ }
65
+ if (lastWindowError) {
66
+ throw new LoaderError(FAIL_LOAD, [id]);
67
+ }
68
+ return true;
69
+ });
70
+ }
71
+ export async function evaluateLoadHook(id, hookPromise) {
72
+ if (!hasSetTimeout) {
73
+ return hookPromise;
74
+ }
75
+ return new Promise((resolve, reject) => {
76
+ // wrap the hook in a watchdog timer
77
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
78
+ const timer = setTimeout(() => {
79
+ reject(new LoaderError(MODULE_LOAD_TIMEOUT, [id]));
80
+ }, MODULE_LOAD_TIMEOUT_TIMER);
81
+ hookPromise
82
+ .then((response) => {
83
+ resolve(response);
84
+ })
85
+ .catch(() => {
86
+ reject(new LoaderError(FAIL_HOOK_LOAD, [id]));
87
+ })
88
+ .finally(() => {
89
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
90
+ clearTimeout(timer);
91
+ });
92
+ });
93
+ }
94
+ //# sourceMappingURL=resolveAndLoadHook.js.map
@@ -0,0 +1,53 @@
1
+ import { Module } from './moduleRegistry/moduleRegistry.js';
2
+ import type { ServiceAPI, ImportMetadata } from '@lwrjs/types';
3
+ import type { FingerprintsLoaderConfig as LoaderConfig } from '../../../types.js';
4
+ /**
5
+ * The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
6
+ */
7
+ export declare class Loader {
8
+ private registry;
9
+ readonly services: Readonly<Pick<ServiceAPI, 'addLoaderPlugin' | 'handleStaleModule' | 'appMetadata'>>;
10
+ constructor(config: LoaderConfig);
11
+ /**
12
+ * Defines/registers a single named AMD module definition.
13
+ *
14
+ * @param {string} name The module name
15
+ * @param {string[]} dependencies A list of module dependencies (module imports)
16
+ * @param {Function} execute The function containing the module code. AKA exporter as it also returns the modules exports when executed
17
+ * @return {void}
18
+ */
19
+ define(name: string, dependencies: string[], execute: Function): void;
20
+ /**
21
+ * Retrieves/loads a module, returning it from the registry if it exists and fetching it if it doesn't.
22
+ *
23
+ * @param {string} id - A module identifier or URL
24
+ * @param {string} importer - The versioned specifier of the module importer
25
+ * Used when the ID is not versioned (eg: variable dynamic imports)
26
+ * @return {Promise<Module>}
27
+ */
28
+ load(id: string, importer?: string): Promise<Module>;
29
+ /**
30
+ * 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).
31
+ *
32
+ * @param {string} id - A module identifier or URL
33
+ * @return {boolean}
34
+ */
35
+ has(id: string): boolean;
36
+ /**
37
+ * Resolves the module identifier or URL. Returns the module identifier if the moduleDefinition exists, or the full resolved URL if a URL is given.
38
+ *
39
+ * @param {string} id - A module identifier or URL
40
+ * @param {string} importer - The versioned specifier of the module importer
41
+ * Used when the ID is not versioned (eg: variable dynamic imports)
42
+ * @return {string}
43
+ */
44
+ resolve(id: string, importer?: string): Promise<string>;
45
+ registerImportMappings(mappings: ImportMetadata, rootSpecifiers: string[]): Promise<void>;
46
+ /**
47
+ * Marks modules as "externally" loaded/provided (e.g. preloaded), so that the loader does not attempt to load them.
48
+ *
49
+ * @param modules - list of module identifiers
50
+ */
51
+ registerExternalModules(modules: string[]): void;
52
+ }
53
+ //# sourceMappingURL=loader.d.ts.map
@@ -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.13.0-alpha.2 */
7
+ /* LWR Module Loader v0.13.0-alpha.21 */
8
8
  const templateRegex = /\{([0-9]+)\}/g;
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  function templateString(template, args) {
@@ -133,6 +133,11 @@ const EXPORTER_ERROR = Object.freeze({
133
133
  level: 0,
134
134
  message: 'Error evaluating module "{0}", error was "{1}"',
135
135
  });
136
+ const UNRESOLVEABLE_MAPPING_ERROR = Object.freeze({
137
+ code: 3022,
138
+ level: 0,
139
+ message: 'Unexpected undefined URI resolving mapping for "{0}"',
140
+ });
136
141
 
137
142
  /* importMap errors */
138
143
  Object.freeze({
@@ -329,7 +334,7 @@ if (hasDocument) {
329
334
  // Loader: modules
330
335
  const LOADER_PREFIX = 'lwr.loader.';
331
336
  const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
332
- const MODULE_DYNAMIC_LOAD = `${LOADER_PREFIX}moduleRegistry.dynamicLoad`;
337
+ const MODULE_DYNAMIC_LOAD = `${LOADER_PREFIX}module.dynamicLoad`;
333
338
  const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
334
339
  const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
335
340
 
@@ -383,7 +388,7 @@ class ImportMetadataResolver {
383
388
  }
384
389
 
385
390
  getBaseUrl() {
386
- return this.config.baseUrl;
391
+ return this.config.baseUrl || '';
387
392
  }
388
393
 
389
394
  registerImportMappings(newImportMetadata, rootSpecifiers) {
@@ -424,8 +429,8 @@ class ImportMetadataResolver {
424
429
 
425
430
  // Get URL from the local cache or return undefiend
426
431
  getURI(specifier) {
427
- return this.importURICache.has(specifier)
428
- ? resolveUrl(this.importURICache.get(specifier).uri, this.getBaseUrl())
432
+ return this.importURICache && this.importURICache.has(specifier)
433
+ ? resolveUrl((this.importURICache.get(specifier) ).uri, this.getBaseUrl())
429
434
  : undefined;
430
435
  }
431
436
 
@@ -526,6 +531,9 @@ class ImportMetadataResolver {
526
531
  // TODO For module invalidation with bundles it is recommended we have to send back all loaded root specified
527
532
  // to ensure we detect all conflicts.
528
533
  const uri = resolveUrl(this.buildMappingUrl(specifier), this.getBaseUrl());
534
+ if (!uri) {
535
+ throw new LoaderError(UNRESOLVEABLE_MAPPING_ERROR, [specifier]);
536
+ }
529
537
  return globalThis.fetch(uri).then((res) => {
530
538
  if (!res.ok) {
531
539
  this.config.profiler.logOperationStart({ id: MAPPINGS_ERROR, specifier });
@@ -646,7 +654,7 @@ try {
646
654
  // swallow
647
655
  }
648
656
  const trusted = createPolicy('trusted', policyOptions);
649
- /*! version: 0.22.1 */
657
+ /*! version: 0.22.3 */
650
658
 
651
659
  /* global console,process */
652
660
 
@@ -848,7 +856,7 @@ class ModuleRegistry {
848
856
  // eslint-disable-next-line no-await-in-loop
849
857
  result = isResponseAPromise(response) ? await response : response;
850
858
  }
851
- if (!this.isValidResolveResponse(result)) {
859
+ if (!this.isValidResolveResponse(result )) {
852
860
  throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
853
861
  }
854
862
 
@@ -979,7 +987,7 @@ class ModuleRegistry {
979
987
  // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
980
988
  timer = setTimeout(() => {
981
989
  reject(new LoaderError(MODULE_LOAD_TIMEOUT, [id]));
982
- }, MODULE_LOAD_TIMEOUT_TIMER);
990
+ }, MODULE_LOAD_TIMEOUT_TIMER) ;
983
991
  }).finally(() => {
984
992
  // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
985
993
  clearTimeout(timer);
@@ -1417,7 +1425,7 @@ class Loader {
1417
1425
  const mappingEndpoint = config.endpoints ? config.endpoints.uris.mapping : undefined;
1418
1426
  let profiler = config.profiler;
1419
1427
 
1420
- if (!mappingEndpoint) {
1428
+ if (!mappingEndpoint || !config.endpoints) {
1421
1429
  throw new LoaderError(NO_MAPPING_URL);
1422
1430
  }
1423
1431
 
@@ -1462,8 +1470,7 @@ class Loader {
1462
1470
  this.services = Object.freeze({
1463
1471
  addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
1464
1472
  handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
1465
- appMetadata: config.appMetadata,
1466
- serverData: config.serverData || {},
1473
+ appMetadata: config.appMetadata ,
1467
1474
  });
1468
1475
  }
1469
1476
 
@@ -0,0 +1,44 @@
1
+ import type { ImportMetadata, LoadMappingHook } from '@lwrjs/types';
2
+ import type { FingerprintsLoaderConfig as LoaderConfig } from '../../../../types.js';
3
+ export type InvalidationCallback = (details: {
4
+ name: string;
5
+ oldUrl: string;
6
+ newUrl: string;
7
+ }) => void;
8
+ export declare class ImportMetadataResolver {
9
+ private importURICache;
10
+ private pendingURICache;
11
+ private config;
12
+ private invalidationCallback;
13
+ private loadMappingHooks;
14
+ constructor(config: LoaderConfig, invalidationCallback: Function);
15
+ addLoadMappingHook(hook: LoadMappingHook): void;
16
+ getMappingEndpoint(): string | undefined;
17
+ getModifiersAsUrlParams(): string;
18
+ buildMappingUrl(specifier: string): string;
19
+ getBaseUrl(): string;
20
+ registerImportMappings(newImportMetadata: ImportMetadata, rootSpecifiers: string[]): void;
21
+ private getURI;
22
+ resolveLocal(specifier: string): string | undefined;
23
+ /**
24
+ * Resolves a the URI for a specified module. It will return the value in this order:
25
+ *
26
+ * 1) Mapping from local URI cache
27
+ * 2) The URI if a specifier is already an absolute URI
28
+ * 3) Mapping fetched from a registered loader hook
29
+ * 4)
30
+ * @param specifier
31
+ * @returns module URI
32
+ */
33
+ resolve(specifier: string): Promise<string>;
34
+ private hasMappingHooks;
35
+ /**
36
+ * Evaluates mapping hooks. Returns first match. If all hooks return null call the mapping service.
37
+ * @param specifier Request module identifier
38
+ * @returns Import Metadata from the module root
39
+ */
40
+ private evaluateMappingHooks;
41
+ private fetchNewMappings;
42
+ private saveImportURIRecord;
43
+ }
44
+ //# sourceMappingURL=importMetadataResolver.d.ts.map