@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 { hasDocument } from './dom.js';
2
+ export function getBaseUrl() {
3
+ let baseUrl = undefined;
4
+ if (hasDocument) {
5
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
6
+ const baseEl = document.querySelector('base[href]');
7
+ baseUrl = baseEl && baseEl.href;
8
+ }
9
+ // eslint-disable-next-line lwr/no-unguarded-apis
10
+ if (!baseUrl && typeof location !== 'undefined') {
11
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
12
+ baseUrl = location.href.split('#')[0].split('?')[0];
13
+ const lastSepIndex = baseUrl.lastIndexOf('/');
14
+ if (lastSepIndex !== -1) {
15
+ baseUrl = baseUrl.slice(0, lastSepIndex + 1);
16
+ }
17
+ }
18
+ return baseUrl;
19
+ }
20
+ /**
21
+ * Check if a string is a URL based on Common Internet Scheme Syntax
22
+ * https://www.ietf.org/rfc/rfc1738.txt
23
+ *
24
+ * URL Format:
25
+ * <scheme>:<scheme-specific-part>
26
+ * Common Internet Scheme Syntax:
27
+ * The scheme specific part starts with a double slash('//')
28
+ *
29
+ * A valid URL has a colon that is followed by a double slash.
30
+ *
31
+ * @param url - the url that is being checked
32
+ * @returns boolean
33
+ *
34
+ * @example Valid URLs
35
+ * 'https://salesforce.com'
36
+ * 'http://localhost:3000'
37
+ *
38
+ * @example Invalid URLs
39
+ * 'salesforce.com'
40
+ * 'localhost:3000'
41
+ * '@salesforce/label/type:namespace:name'
42
+ */
43
+ export function isUrl(url) {
44
+ return url.indexOf('://') !== -1;
45
+ }
46
+ // Borrowed and adapted from https://github.com/systemjs/systemjs/blob/master/src/common.js
47
+ // Resolves the first path segment relative to the second/parent URL
48
+ // eg: resolveIfNotPlainOrUrl('../test', 'http://www.site.com/one/two') => 'http://www.site.com/test'
49
+ // eg: resolveIfNotPlainOrUrl('./x/y/z', 'https://my.com/segment')).toBe('https://my.com/x/y/z')
50
+ export function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
51
+ const backslashRegEx = /\\/g;
52
+ if (relUrl.indexOf('\\') !== -1)
53
+ relUrl = relUrl.replace(backslashRegEx, '/');
54
+ // protocol-relative
55
+ if (relUrl[0] === '/' && relUrl[1] === '/') {
56
+ return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
57
+ }
58
+ // relative-url
59
+ else if ((relUrl[0] === '.' &&
60
+ (relUrl[1] === '/' ||
61
+ (relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
62
+ (relUrl.length === 1 && (relUrl += '/')))) ||
63
+ relUrl[0] === '/') {
64
+ const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
65
+ let pathname;
66
+ if (parentUrl[parentProtocol.length + 1] === '/') {
67
+ // resolving to a :// so we need to read out the auth and host
68
+ if (parentProtocol !== 'file:') {
69
+ pathname = parentUrl.slice(parentProtocol.length + 2);
70
+ pathname = pathname.slice(pathname.indexOf('/') + 1);
71
+ }
72
+ else {
73
+ pathname = parentUrl.slice(8);
74
+ }
75
+ }
76
+ else {
77
+ // resolving to :/ so pathname is the /... part
78
+ pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/' ? 1 : 0));
79
+ }
80
+ if (relUrl[0] === '/')
81
+ return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
82
+ // join together and split for removal of .. and . segments
83
+ // looping the string instead of anything fancy for perf reasons
84
+ // '../../../../../z' resolved to 'x/y' is just 'z'
85
+ const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
86
+ const output = [];
87
+ let segmentIndex = -1;
88
+ for (let i = 0; i < segmented.length; i++) {
89
+ // busy reading a segment - only terminate on '/'
90
+ if (segmentIndex !== -1) {
91
+ if (segmented[i] === '/') {
92
+ output.push(segmented.slice(segmentIndex, i + 1));
93
+ segmentIndex = -1;
94
+ }
95
+ }
96
+ // new segment - check if it is relative
97
+ else if (segmented[i] === '.') {
98
+ // ../ segment
99
+ if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
100
+ output.pop();
101
+ i += 2;
102
+ }
103
+ // ./ segment
104
+ else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
105
+ i += 1;
106
+ }
107
+ else {
108
+ // the start of a new segment as below
109
+ segmentIndex = i;
110
+ }
111
+ }
112
+ // it is the start of a new segment
113
+ else {
114
+ segmentIndex = i;
115
+ }
116
+ }
117
+ // finish reading out the last segment
118
+ if (segmentIndex !== -1)
119
+ output.push(segmented.slice(segmentIndex));
120
+ return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
121
+ }
122
+ }
123
+ export function resolveUrl(relUrl, parentUrl) {
124
+ const resolvedUrl = resolveIfNotPlainOrUrl(relUrl, parentUrl) ||
125
+ (isUrl(relUrl) ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
126
+ return resolvedUrl;
127
+ }
128
+ //# sourceMappingURL=url.js.map
@@ -0,0 +1,2 @@
1
+ export declare const MODULE_LOAD_TIMEOUT_TIMER: number;
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,2 @@
1
+ export const MODULE_LOAD_TIMEOUT_TIMER = 60 * 1000; // 1m
2
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,30 @@
1
+ declare class LoaderError extends Error {
2
+ constructor(errorInfo: ErrorInfo, errorArgs?: string[]);
3
+ }
4
+ declare function invariant(condition: boolean, errorInfo: ErrorInfo): void;
5
+ interface ErrorInfo {
6
+ code: number;
7
+ message: string;
8
+ level: number;
9
+ }
10
+ declare const MISSING_NAME: ErrorInfo;
11
+ declare const FAIL_INSTANTIATE: ErrorInfo;
12
+ declare const NO_AMD_REQUIRE: ErrorInfo;
13
+ declare const FAILED_DEP: ErrorInfo;
14
+ declare const INVALID_DEPS: ErrorInfo;
15
+ declare const FAIL_LOAD: ErrorInfo;
16
+ declare const UNRESOLVED: ErrorInfo;
17
+ declare const NO_BASE_URL: ErrorInfo;
18
+ declare const HOOK_ALREADY_SET: ErrorInfo;
19
+ declare const INVALID_HOOK: ErrorInfo;
20
+ declare const INVALID_LOADER_SERVICE_RESPONSE: ErrorInfo;
21
+ declare const MODULE_LOAD_TIMEOUT: ErrorInfo;
22
+ declare const HTTP_FAIL_LOAD: ErrorInfo;
23
+ declare const STALE_HOOK_ERROR: ErrorInfo;
24
+ declare const MODULE_ALREADY_LOADED: ErrorInfo;
25
+ declare const FAIL_HOOK_LOAD: ErrorInfo;
26
+ declare const EXPORTER_ERROR: ErrorInfo;
27
+ declare const BAD_IMPORT_MAP: ErrorInfo;
28
+ export { MISSING_NAME, FAIL_INSTANTIATE, NO_AMD_REQUIRE, FAILED_DEP, INVALID_DEPS, FAIL_LOAD, UNRESOLVED, NO_BASE_URL, BAD_IMPORT_MAP, 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, };
29
+ export { invariant, LoaderError };
30
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1,113 @@
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: {0}',
92
+ });
93
+ const FAIL_HOOK_LOAD = Object.freeze({
94
+ code: 3018,
95
+ level: 0,
96
+ message: 'Error loading "{0}" from hook',
97
+ });
98
+ const EXPORTER_ERROR = Object.freeze({
99
+ code: 3021,
100
+ level: 0,
101
+ message: 'Error evaluating module "{0}", error was {1}',
102
+ });
103
+ /* importMap errors */
104
+ const BAD_IMPORT_MAP = Object.freeze({
105
+ code: 3011,
106
+ level: 0,
107
+ message: 'import map is not valid',
108
+ });
109
+ /* Errors */
110
+ export { MISSING_NAME, FAIL_INSTANTIATE, NO_AMD_REQUIRE, FAILED_DEP, INVALID_DEPS, FAIL_LOAD, UNRESOLVED, NO_BASE_URL, BAD_IMPORT_MAP, 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, };
111
+ /* API */
112
+ export { invariant, LoaderError };
113
+ //# 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 { DEPRECIATEDHandleStaleModuleHookArgs, HandleStaleModuleHook } from '@lwrjs/types';
2
+ export declare function evaluateHandleStaleModuleHooks(handleStaleModuleHooks: HandleStaleModuleHook[], hookArgs: DEPRECIATEDHandleStaleModuleHookArgs): 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, oldHash, newHash } = 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, oldHash, newHash });
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,10 @@
1
+ /**
2
+ * Import map support for LWR based on the spec: https://github.com/WICG/import-maps
3
+ *
4
+ * This implementation is adapted from https://github.com/systemjs/systemjs/blob/master/src/features/import-map.js
5
+ */
6
+ import { ImportMap } from './importMap.js';
7
+ declare const IMPORTMAP_SCRIPT_TYPE = "lwr-importmap";
8
+ declare function evaluateImportMaps(baseUrl?: string): Promise<ImportMap | undefined>;
9
+ export { IMPORTMAP_SCRIPT_TYPE, evaluateImportMaps };
10
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Import map support for LWR based on the spec: https://github.com/WICG/import-maps
3
+ *
4
+ * This implementation is adapted from https://github.com/systemjs/systemjs/blob/master/src/features/import-map.js
5
+ */
6
+ import { resolveAndComposeImportMap } from './importMap.js';
7
+ import { getBaseUrl } from '../utils/url.js';
8
+ import { hasDocument, hasConsole } from '../utils/dom.js';
9
+ import { LoaderError, BAD_IMPORT_MAP, NO_BASE_URL } from '../errors/messages.js';
10
+ const IMPORTMAP_SCRIPT_TYPE = 'lwr-importmap';
11
+ // iterates on the any <script type="${IMPORTMAP_SCRIPT_TYPE}", invoking the given callback for each
12
+ function iterateDocumentImportMaps(callBack, extraSelector) {
13
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
14
+ const nodeList = document.querySelectorAll(`script[type="${IMPORTMAP_SCRIPT_TYPE}"]` + extraSelector);
15
+ const filtered = Array.from(nodeList).filter((node) => {
16
+ if (node.src) {
17
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
18
+ if (hasConsole)
19
+ console.warn('LWR does not support import maps from script src');
20
+ return false;
21
+ }
22
+ return true;
23
+ });
24
+ Array.prototype.forEach.call(filtered, callBack);
25
+ }
26
+ // retrives the import map text from a <script type="${IMPORTMAP_SCRIPT_TYPE}"
27
+ async function getImportMapFromScript(script) {
28
+ return Promise.resolve(script.innerHTML);
29
+ }
30
+ // get importMap from <script type="lwr-importmap">
31
+ async function evaluateImportMaps(baseUrl) {
32
+ let importMap = { imports: {}, scopes: {} };
33
+ let importMapPromise = Promise.resolve(importMap);
34
+ if (hasDocument) {
35
+ if (!baseUrl) {
36
+ baseUrl = getBaseUrl();
37
+ }
38
+ if (!baseUrl) {
39
+ throw new LoaderError(NO_BASE_URL);
40
+ }
41
+ iterateDocumentImportMaps((script) => {
42
+ importMapPromise = importMapPromise
43
+ .then(() => {
44
+ return getImportMapFromScript(script);
45
+ })
46
+ .then((importMapTxt) => {
47
+ try {
48
+ return JSON.parse(importMapTxt);
49
+ }
50
+ catch (e) {
51
+ throw new LoaderError(BAD_IMPORT_MAP);
52
+ }
53
+ })
54
+ .then((jsonImportMap) => {
55
+ importMap = resolveAndComposeImportMap(jsonImportMap, script.src || baseUrl, importMap);
56
+ return importMap;
57
+ });
58
+ }, '');
59
+ }
60
+ return importMapPromise;
61
+ }
62
+ export { IMPORTMAP_SCRIPT_TYPE, evaluateImportMaps };
63
+ //# sourceMappingURL=dom.js.map
@@ -0,0 +1,22 @@
1
+ export interface ImportMap {
2
+ imports?: ImportMapImports;
3
+ scopes?: {
4
+ [key: string]: ImportMapImports;
5
+ };
6
+ default?: string;
7
+ }
8
+ export type ImportMapImports = {
9
+ [key: string]: string;
10
+ };
11
+ /**
12
+ * Return type of the import resolver
13
+ *
14
+ */
15
+ export type ImportDefinition = {
16
+ uri: string;
17
+ defaultUri?: boolean;
18
+ };
19
+ declare function resolveImportMapEntry(importMap: ImportMap, resolvedOrPlain: string, parentUrl?: string): ImportDefinition | undefined;
20
+ declare function resolveAndComposeImportMap(json: ImportMap, baseUrl: string, parentMap?: ImportMap): ImportMap;
21
+ export { resolveAndComposeImportMap, resolveImportMapEntry };
22
+ //# sourceMappingURL=importMap.d.ts.map
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Import map support for LWR based on the spec: https://github.com/WICG/import-maps
3
+ *
4
+ * This implementation is adapted from https://github.com/systemjs/systemjs/blob/master/src/features/import-map.js
5
+ */
6
+ import { resolveUrl, resolveIfNotPlainOrUrl, isUrl } from '../utils/url.js';
7
+ import { getMatch, targetWarning } from './utils.js';
8
+ // Resolves an import map package entry
9
+ function applyPackages(id, packages, defaultUri) {
10
+ const pkgName = getMatch(id, packages);
11
+ if (pkgName) {
12
+ const pkg = packages[pkgName];
13
+ if (pkg === null) {
14
+ return;
15
+ }
16
+ if (id.length > pkgName.length && pkg[pkg.length - 1] !== '/') {
17
+ targetWarning(pkgName, pkg, "should have a trailing '/'");
18
+ }
19
+ else {
20
+ const isPackage = id.length > pkgName.length &&
21
+ pkg[pkg.length - 1] === '/' &&
22
+ pkg.lastIndexOf(pkgName) === pkg.length - pkgName.length;
23
+ if (isPackage) {
24
+ // Encode the specifier to create a well-formed LWR module URI
25
+ const uri = pkg.substring(0, pkg.lastIndexOf(pkgName)) + encodeURIComponent(id);
26
+ return { uri };
27
+ }
28
+ const uri = pkg + id.slice(pkgName.length);
29
+ return { uri };
30
+ }
31
+ }
32
+ else if (defaultUri) {
33
+ // When a specifier's URI cannot be resolved via the imports, fallback to "default".
34
+ // -> https://rfcs.lwc.dev/rfcs/lwr/0000-import-metadata#json-schema
35
+ // However, if `id` is already a fully resolved url,
36
+ // we cannot prepend the defaultUri -> https://github.com/salesforce-experience-platform-emu/lwr/issues/378.
37
+ // In this case we do not apply any package mappings and allow the caller (resolveImportMapEntry) to handle it.
38
+ if (!isUrl(id)) {
39
+ const uri = defaultUri + encodeURIComponent(id);
40
+ return {
41
+ uri,
42
+ defaultUri: true,
43
+ };
44
+ }
45
+ }
46
+ }
47
+ // Resolves an entry in the import map
48
+ function resolveImportMapEntry(importMap, resolvedOrPlain, parentUrl) {
49
+ if (!importMap.scopes) {
50
+ importMap.scopes = {};
51
+ }
52
+ if (!importMap.imports) {
53
+ importMap.imports = {};
54
+ }
55
+ const scopes = importMap.scopes;
56
+ let scopeUrl = parentUrl && getMatch(parentUrl, scopes);
57
+ while (scopeUrl) {
58
+ const packageResolution = applyPackages(resolvedOrPlain, scopes[scopeUrl]);
59
+ if (packageResolution) {
60
+ return packageResolution;
61
+ }
62
+ scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), scopes);
63
+ }
64
+ return (applyPackages(resolvedOrPlain, importMap.imports, importMap.default) ||
65
+ (isUrl(resolvedOrPlain) && { uri: resolvedOrPlain }) ||
66
+ undefined);
67
+ }
68
+ // In place transformation of the ImportMap object
69
+ function resolveAndComposePackages(packages, outPackages, baseUrl, parentMap, parentUrl) {
70
+ for (const p in packages) {
71
+ const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
72
+ const rhs = packages[p];
73
+ // package fallbacks not currently supported
74
+ if (typeof rhs !== 'string') {
75
+ continue;
76
+ }
77
+ const mapped = resolveImportMapEntry(parentMap, resolveIfNotPlainOrUrl(rhs, baseUrl) || rhs, parentUrl);
78
+ if (!mapped) {
79
+ targetWarning(p, rhs, 'bare specifier did not resolve');
80
+ }
81
+ else {
82
+ outPackages[resolvedLhs] = mapped.uri;
83
+ }
84
+ }
85
+ }
86
+ // Composes a single import map object given a child and parent import map
87
+ function resolveAndComposeImportMap(json, baseUrl, parentMap = { imports: {}, scopes: {} }) {
88
+ const outMap = {
89
+ imports: Object.assign({}, parentMap.imports),
90
+ scopes: Object.assign({}, parentMap.scopes),
91
+ default: json.default,
92
+ };
93
+ if (json.imports) {
94
+ resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
95
+ }
96
+ if (json.scopes) {
97
+ for (const s in json.scopes) {
98
+ const resolvedScope = resolveUrl(s, baseUrl);
99
+ resolveAndComposePackages(json.scopes[s], outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}), baseUrl, parentMap, resolvedScope);
100
+ }
101
+ }
102
+ if (json.default) {
103
+ outMap.default = resolveIfNotPlainOrUrl(json.default, baseUrl);
104
+ }
105
+ return outMap;
106
+ }
107
+ export { resolveAndComposeImportMap, resolveImportMapEntry };
108
+ //# sourceMappingURL=importMap.js.map
@@ -0,0 +1,8 @@
1
+ import type { ImportDefinition, ImportMap } from './importMap.js';
2
+ import type { ImportResolver } from '../importResolver/importResolver.js';
3
+ export declare class ImportMapResolver implements ImportResolver {
4
+ importMap: ImportMap;
5
+ constructor(importMap: ImportMap);
6
+ resolve(resolvedOrPlain: string, parentUrl: string): ImportDefinition | undefined;
7
+ }
8
+ //# sourceMappingURL=importMapResolver.d.ts.map
@@ -0,0 +1,11 @@
1
+ import { resolveImportMapEntry } from './importMap.js';
2
+ /* spec based import map resolver */
3
+ export class ImportMapResolver {
4
+ constructor(importMap) {
5
+ this.importMap = importMap;
6
+ }
7
+ resolve(resolvedOrPlain, parentUrl) {
8
+ return resolveImportMapEntry(this.importMap, resolvedOrPlain, parentUrl);
9
+ }
10
+ }
11
+ //# sourceMappingURL=importMapResolver.js.map