@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 REQUIRED_MODULES_TIMEOUT: number;
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,2 @@
1
+ export const REQUIRED_MODULES_TIMEOUT = 60 * 1000; // 2m
2
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,4 @@
1
+ import type { ClientBootstrapConfig } from '@lwrjs/types';
2
+ import type { InitializeApp, LoaderDefine, RegisterCustomErrorHandler } from '../types.js';
3
+ export declare function customInit(config: ClientBootstrapConfig, initializeApp: InitializeApp, define: LoaderDefine, onBootstrapError: RegisterCustomErrorHandler): void;
4
+ //# sourceMappingURL=customInit.d.ts.map
@@ -0,0 +1,29 @@
1
+ import { attachDispatcher } from 'lwr/profiler';
2
+ // Check for errors with autoBoot and customInit
3
+ function validatePreInit(autoBoot, customInit) {
4
+ // If autoBoot === false, there must be a customInit hook
5
+ if (!autoBoot && !customInit) {
6
+ throw new Error('The customInit hook is required when autoBoot is false');
7
+ }
8
+ // If autoBoot === true, there must NOT be a customInit hook
9
+ if (autoBoot && customInit) {
10
+ throw new Error('The customInit hook must not be defined when autoBoot is true');
11
+ }
12
+ }
13
+ // Process the customInit hook
14
+ export function customInit(config, initializeApp, define, onBootstrapError) {
15
+ // Validate config
16
+ const { autoBoot, customInit } = config;
17
+ validatePreInit(autoBoot, customInit);
18
+ // Set up arguments and call the customInit hook, if available
19
+ if (customInit) {
20
+ const lwr = {
21
+ initializeApp,
22
+ define,
23
+ onBootstrapError,
24
+ attachDispatcher,
25
+ };
26
+ customInit(lwr, config);
27
+ }
28
+ }
29
+ //# sourceMappingURL=customInit.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,9 @@
1
+ import LoaderShim from './shim.js';
2
+ // The loader module is ALWAYS required
3
+ const GLOBAL = globalThis;
4
+ GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
5
+ if (GLOBAL.LWR.requiredModules.indexOf('lwr/loader/v/__VERSION__') < 0) {
6
+ GLOBAL.LWR.requiredModules.push('lwr/loader/v/__VERSION__');
7
+ }
8
+ new LoaderShim(GLOBAL);
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import type { DefineArguments, FingerprintsLoaderAPI as LoaderAPI, FingerprintsLoaderConfig as LoaderConfig } from '../types.js';
2
+ export declare function createLoader(name: string, definition: DefineArguments, config: LoaderConfig, externalModules?: string[]): LoaderAPI;
3
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1,27 @@
1
+ export function createLoader(name, definition, config, externalModules) {
2
+ if (!definition || typeof definition[2] !== 'function') {
3
+ throw new Error(`Expected loader with specifier "${name}" to be a module`);
4
+ }
5
+ // Create a Loader instance
6
+ const exports = {};
7
+ definition[2].call(null, exports);
8
+ const { Loader } = exports;
9
+ if (!Loader) {
10
+ throw new Error('Expected Loader class to be defined');
11
+ }
12
+ const loader = new Loader(config);
13
+ if (externalModules && externalModules.length) {
14
+ loader.registerExternalModules(externalModules);
15
+ }
16
+ // Define the loader module with public API: { define, load, services }
17
+ const exporter = (exports) => {
18
+ Object.assign(exports, {
19
+ define: loader.define.bind(loader),
20
+ load: loader.load.bind(loader),
21
+ services: loader.services,
22
+ });
23
+ };
24
+ loader.define(name, ['exports'], exporter);
25
+ return loader;
26
+ }
27
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1,31 @@
1
+ import type { GlobalThis } from '../types.js';
2
+ export default class LoaderShim {
3
+ private global;
4
+ private config;
5
+ private bootReady;
6
+ private loaderSpecifier;
7
+ private defineCache;
8
+ private orderedDefs;
9
+ private errorHandler?;
10
+ private watchdogTimerId?;
11
+ constructor(global: GlobalThis);
12
+ private canInit;
13
+ /**
14
+ * Create a temporary LWR.define() function which captures all
15
+ * calls that occur BEFORE the full loader module is available
16
+ *
17
+ * Each call to LWR.define() is stored in 2 ways:
18
+ * - in a map as [moduleName, arguments] pairs
19
+ * - each moduleName is pushed onto an array, to preserve
20
+ * the order in which the modules were defined
21
+ */
22
+ private tempDefine;
23
+ private postCustomInit;
24
+ private initApp;
25
+ private waitForDOMContentLoaded;
26
+ private createProfilerModule;
27
+ private mountApp;
28
+ private enterErrorState;
29
+ private startWatchdogTimer;
30
+ }
31
+ //# sourceMappingURL=shim.d.ts.map
@@ -0,0 +1,194 @@
1
+ /* global document */
2
+ import { BOOTSTRAP_ERROR } from 'lwr/metrics';
3
+ import { logOperationStart, logOperationEnd } from 'lwr/profiler';
4
+ import { createLoader } from './loader.js';
5
+ import { REQUIRED_MODULES_TIMEOUT } from './constants.js';
6
+ import { customInit } from './customInit.js';
7
+ /* eslint-disable lwr/no-unguarded-apis */
8
+ const hasSetTimeout = typeof setTimeout === 'function';
9
+ const hasConsole = typeof console !== 'undefined';
10
+ /* eslint-enable lwr/no-unguarded-apis */
11
+ export default class LoaderShim {
12
+ constructor(global) {
13
+ this.defineCache = {};
14
+ this.orderedDefs = [];
15
+ // Start watchdog timer
16
+ if (hasSetTimeout) {
17
+ this.watchdogTimerId = this.startWatchdogTimer();
18
+ }
19
+ // Parse configuration
20
+ this.global = global;
21
+ this.config = global.LWR;
22
+ this.loaderSpecifier = 'lwr/loader/v/__VERSION__';
23
+ // Set up error handler
24
+ this.errorHandler = this.config.onError;
25
+ // Set up the temporary LWR.define function and customInit hook
26
+ const tempDefine = this.tempDefine.bind(this);
27
+ global.LWR.define = tempDefine;
28
+ this.bootReady = this.config.autoBoot;
29
+ try {
30
+ this.createProfilerModule(global.LWR.define);
31
+ customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
32
+ // customInit handlers can overwrite
33
+ // the error handler with onBootstrapError
34
+ this.errorHandler = e;
35
+ });
36
+ }
37
+ catch (e) {
38
+ this.enterErrorState(e);
39
+ }
40
+ }
41
+ // Return true if the app can be initialized
42
+ canInit() {
43
+ if (!this.config.requiredModules) {
44
+ throw new Error('Unexpected missing requiredModules');
45
+ }
46
+ // Initialize the app if:
47
+ // - bootReady: autoBoot is on OR customInit has finished
48
+ // - all required modules are defined
49
+ const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
50
+ return this.bootReady && allDefined;
51
+ }
52
+ /**
53
+ * Create a temporary LWR.define() function which captures all
54
+ * calls that occur BEFORE the full loader module is available
55
+ *
56
+ * Each call to LWR.define() is stored in 2 ways:
57
+ * - in a map as [moduleName, arguments] pairs
58
+ * - each moduleName is pushed onto an array, to preserve
59
+ * the order in which the modules were defined
60
+ */
61
+ tempDefine(...args) {
62
+ // Cache the incoming module
63
+ const moduleName = args[0];
64
+ this.defineCache[moduleName] = args;
65
+ this.orderedDefs.push(moduleName);
66
+ if (this.canInit()) {
67
+ if (hasSetTimeout) {
68
+ // requiredModules are defined, clear watchdog timer
69
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
70
+ clearTimeout(this.watchdogTimerId);
71
+ }
72
+ this.initApp();
73
+ }
74
+ }
75
+ // Called by the customInit hook via lwr.initializeApp()
76
+ postCustomInit() {
77
+ this.bootReady = true;
78
+ if (this.canInit()) {
79
+ if (hasSetTimeout) {
80
+ // requiredModules are defined, clear watchdog timer
81
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
82
+ clearTimeout(this.watchdogTimerId);
83
+ }
84
+ this.initApp();
85
+ }
86
+ }
87
+ // Create the loader and initialize the application
88
+ initApp() {
89
+ try {
90
+ const loaderConfig = {
91
+ endpoints: this.config.endpoints,
92
+ baseUrl: this.config.baseUrl,
93
+ profiler: { logOperationStart, logOperationEnd },
94
+ // TODO: can be removed following https://github.com/salesforce-experience-platform-emu/lwr/issues/1087
95
+ appMetadata: {
96
+ appId: this.config.appId,
97
+ bootstrapModule: this.config.bootstrapModule,
98
+ rootComponent: this.config.rootComponent,
99
+ rootComponents: this.config.rootComponents,
100
+ },
101
+ };
102
+ const loader = createLoader(this.loaderSpecifier, this.defineCache[this.loaderSpecifier], loaderConfig, this.config.preloadModules);
103
+ this.mountApp(loader);
104
+ }
105
+ catch (e) {
106
+ this.enterErrorState(e);
107
+ }
108
+ }
109
+ waitForDOMContentLoaded() {
110
+ // eslint-disable-next-line lwr/no-unguarded-apis
111
+ if (typeof document === undefined) {
112
+ return Promise.resolve();
113
+ }
114
+ // Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
115
+ // eslint-disable-next-line lwr/no-unguarded-apis
116
+ if (document.readyState === 'interactive' || document.readyState === 'complete') {
117
+ return Promise.resolve();
118
+ }
119
+ return new Promise((resolve) => {
120
+ // eslint-disable-next-line lwr/no-unguarded-apis
121
+ document.addEventListener('DOMContentLoaded', () => {
122
+ resolve();
123
+ });
124
+ });
125
+ }
126
+ // Create a module out of the profiler
127
+ // Note: The profiler is also available as a module through lwc module resolution (see package.json)
128
+ createProfilerModule(define) {
129
+ const exporter = (exports) => {
130
+ Object.assign(exports, { logOperationStart, logOperationEnd });
131
+ };
132
+ define('lwr/profiler/v/__VERSION__', ['exports'], exporter);
133
+ }
134
+ // Set up the application globals, import map, root custom element...
135
+ mountApp(loader) {
136
+ const { bootstrapModule, rootComponent, rootComponents, serverData, endpoints, imports, index } = this.config;
137
+ const importsObj = imports || {};
138
+ // Set global LWR.define to loader.define
139
+ this.global.LWR = Object.freeze({
140
+ define: loader.define.bind(loader),
141
+ rootComponent,
142
+ rootComponents,
143
+ serverData: serverData || {},
144
+ endpoints,
145
+ imports: importsObj,
146
+ index: index || {},
147
+ env: this.global.LWR.env,
148
+ });
149
+ // Redefine all modules in the temporary cache
150
+ this.orderedDefs.forEach((specifier) => {
151
+ if (specifier !== this.loaderSpecifier) {
152
+ loader.define(...this.defineCache[specifier]);
153
+ }
154
+ });
155
+ // by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
156
+ const { disableInitDefer } = this.config;
157
+ // Load the import mappings and application bootstrap module
158
+ loader
159
+ .registerImportMappings({ imports: importsObj, index }, [
160
+ bootstrapModule,
161
+ rootComponent,
162
+ ])
163
+ .then(() => {
164
+ if (!disableInitDefer) {
165
+ return this.waitForDOMContentLoaded();
166
+ }
167
+ })
168
+ .then(() => loader.load(bootstrapModule))
169
+ .catch((reason) => {
170
+ this.enterErrorState(new Error(`Application ${rootComponent || bootstrapModule} could not be loaded: ${reason}`));
171
+ });
172
+ }
173
+ // Trigger bootstrap error state, and call error handler if registered
174
+ enterErrorState(error) {
175
+ logOperationStart({ id: BOOTSTRAP_ERROR });
176
+ if (this.errorHandler) {
177
+ this.errorHandler(error);
178
+ }
179
+ else {
180
+ if (hasConsole) {
181
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
182
+ console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
183
+ }
184
+ }
185
+ }
186
+ // eslint-disable-next-line no-undef, lwr/no-unguarded-apis
187
+ startWatchdogTimer() {
188
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
189
+ return setTimeout(() => {
190
+ this.enterErrorState(new Error('Failed to load required modules - timed out'));
191
+ }, REQUIRED_MODULES_TIMEOUT);
192
+ }
193
+ }
194
+ //# sourceMappingURL=shim.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,9 @@
1
+ import LoaderShim from './shimLegacy.js';
2
+ // The loader module is ALWAYS required
3
+ const GLOBAL = globalThis;
4
+ GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
5
+ if (GLOBAL.LWR.requiredModules.indexOf('lwr/loaderLegacy/v/__VERSION__') < 0) {
6
+ GLOBAL.LWR.requiredModules.push('lwr/loaderLegacy/v/__VERSION__');
7
+ }
8
+ new LoaderShim(GLOBAL);
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import type { LoaderConfig, DefineArguments, LoaderAPI } from '../types.js';
2
+ export declare function createLoader(name: string, definition: DefineArguments, config: LoaderConfig, externalModules?: string[]): LoaderAPI;
3
+ //# sourceMappingURL=loaderLegacy.d.ts.map
@@ -0,0 +1,29 @@
1
+ export function createLoader(name, definition, config, externalModules) {
2
+ if (!definition || typeof definition[2] !== 'function') {
3
+ throw new Error(`Expected loader with specifier "${name}" to be a module`);
4
+ }
5
+ // Create a Loader instance
6
+ const exports = {};
7
+ definition[2].call(null, exports);
8
+ const { Loader } = exports;
9
+ if (!Loader) {
10
+ throw new Error('Expected Loader class to be defined');
11
+ }
12
+ const loader = new Loader(config);
13
+ // register externally loaded modules
14
+ if (externalModules && externalModules.length) {
15
+ loader.registerExternalModules(externalModules);
16
+ }
17
+ // Define the loader module with public API: { define, load, services }
18
+ const exporter = (exports) => {
19
+ Object.assign(exports, {
20
+ define: loader.define.bind(loader),
21
+ load: loader.load.bind(loader),
22
+ services: loader.services,
23
+ });
24
+ return;
25
+ };
26
+ loader.define(name, ['exports'], exporter, definition[3]);
27
+ return loader;
28
+ }
29
+ //# sourceMappingURL=loaderLegacy.js.map
@@ -0,0 +1,31 @@
1
+ import type { GlobalThis } from '../types.js';
2
+ export default class LoaderShim {
3
+ private global;
4
+ private config;
5
+ private bootReady;
6
+ private loaderModule;
7
+ private defineCache;
8
+ private orderedDefs;
9
+ private errorHandler?;
10
+ private watchdogTimerId?;
11
+ constructor(global: GlobalThis);
12
+ private canInit;
13
+ /**
14
+ * Create a temporary LWR.define() function which captures all
15
+ * calls that occur BEFORE the full loader module is available
16
+ *
17
+ * Each call to LWR.define() is stored in 2 ways:
18
+ * - in a map as [moduleName, arguments] pairs
19
+ * - each moduleName is pushed onto an array, to preserve
20
+ * the order in which the modules were defined
21
+ */
22
+ private tempDefine;
23
+ private postCustomInit;
24
+ private initApp;
25
+ private waitForDOMContentLoaded;
26
+ private createProfilerModule;
27
+ private mountApp;
28
+ private enterErrorState;
29
+ private startWatchdogTimer;
30
+ }
31
+ //# sourceMappingURL=shimLegacy.d.ts.map