@lwrjs/loader 0.6.0-alpha.12 → 0.6.0-alpha.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.js +322 -229
- package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +2 -2
- package/build/assets/prod/lwr-loader-shim-legacy.js +288 -224
- package/build/assets/prod/lwr-loader-shim.bundle.js +16 -7
- package/build/assets/prod/lwr-loader-shim.bundle.min.js +2 -2
- package/build/assets/prod/lwr-loader-shim.js +6 -5
- package/build/bundle/prod/lwr/esmLoader/esmLoader.js +1 -1
- package/build/cjs/modules/lwr/esmLoader/esmLoader.cjs +9 -6
- package/build/cjs/modules/lwr/esmLoader/importResolver.cjs +24 -3
- package/build/cjs/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.cjs +9 -2
- package/build/modules/lwr/esmLoader/esmLoader.js +38 -10
- package/build/modules/lwr/loader/loader.js +10 -2
- package/build/modules/lwr/loaderLegacy/loaderLegacy.js +34 -5
- package/package.json +6 -6
|
@@ -4,244 +4,308 @@
|
|
|
4
4
|
* SPDX-License-Identifier: MIT
|
|
5
5
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
6
6
|
*/
|
|
7
|
-
/* LWR Legacy Module Loader Shim v0.6.0-alpha.
|
|
7
|
+
/* LWR Legacy Module Loader Shim v0.6.0-alpha.16 */
|
|
8
8
|
(function () {
|
|
9
|
-
|
|
9
|
+
'use strict';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
// Create a Loader instance
|
|
16
|
-
const exports = {};
|
|
17
|
-
definition[2].call(null, exports);
|
|
18
|
-
const { Loader } = exports;
|
|
19
|
-
const loader = new Loader(baseUrl);
|
|
20
|
-
// register externally loaded modules
|
|
21
|
-
if (externalModules && externalModules.length) {
|
|
22
|
-
loader.registerExternalModules(externalModules);
|
|
23
|
-
}
|
|
24
|
-
// Define the loader module with public API: { define, load, services }
|
|
25
|
-
const exporter = (exports) => {
|
|
26
|
-
Object.assign(exports, {
|
|
27
|
-
define: loader.define.bind(loader),
|
|
28
|
-
load: loader.load.bind(loader),
|
|
29
|
-
services: loader.services,
|
|
30
|
-
});
|
|
31
|
-
return;
|
|
32
|
-
};
|
|
33
|
-
loader.define(name, ['exports'], exporter, definition[3]);
|
|
34
|
-
return loader;
|
|
35
|
-
}
|
|
11
|
+
// Bootstrap / shim
|
|
12
|
+
const BOOTSTRAP_PREFIX = 'lwr.bootstrap.';
|
|
13
|
+
const BOOTSTRAP_ERROR = `${BOOTSTRAP_PREFIX}error`;
|
|
36
14
|
|
|
37
|
-
|
|
15
|
+
var Phase;
|
|
38
16
|
|
|
39
|
-
|
|
17
|
+
(function (Phase) {
|
|
18
|
+
Phase[Phase["Start"] = 0] = "Start";
|
|
19
|
+
Phase[Phase["End"] = 1] = "End";
|
|
20
|
+
})(Phase || (Phase = {}));
|
|
40
21
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
22
|
+
// Attach a custom dispatcher
|
|
23
|
+
let customDispatcher;
|
|
24
|
+
function attachDispatcher(dispatcher) {
|
|
25
|
+
customDispatcher = dispatcher;
|
|
26
|
+
} // Check if the Performance API is available
|
|
27
|
+
// e.g. JSDom (used in Jest) doesn't implement these
|
|
47
28
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (!autoBoot && !customInit) {
|
|
52
|
-
throw new Error('The customInit hook is required when autoBoot is false');
|
|
53
|
-
}
|
|
54
|
-
// If autoBoot === true, there must NOT be a customInit hook
|
|
55
|
-
if (autoBoot && customInit) {
|
|
56
|
-
throw new Error('The customInit hook must not be defined when autoBoot is true');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
// Process the customInit hook
|
|
60
|
-
function customInit(config, initializeApp, define, onBootstrapError) {
|
|
61
|
-
// Validate config
|
|
62
|
-
const { autoBoot, customInit } = config;
|
|
63
|
-
validatePreInit(autoBoot, customInit);
|
|
64
|
-
// Set up arguments and call the customInit hook, if available
|
|
65
|
-
if (customInit) {
|
|
66
|
-
const lwr = {
|
|
67
|
-
initializeApp,
|
|
68
|
-
define,
|
|
69
|
-
onBootstrapError,
|
|
70
|
-
attachDispatcher,
|
|
71
|
-
};
|
|
72
|
-
customInit(lwr, config);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
29
|
+
const perf = globalThis.performance;
|
|
30
|
+
const isPerfSupported = typeof perf !== 'undefined' && typeof perf.mark === 'function' && typeof perf.clearMarks === 'function' && typeof perf.measure === 'function' && typeof perf.clearMeasures === 'function'; // For marking request metrics
|
|
31
|
+
// Fallback to the Performance API if there is no custom dispatcher
|
|
75
32
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this.loaderModule = 'lwr/loaderLegacy/v/0_6_0-alpha_12';
|
|
89
|
-
// Set up the temporary LWR.define function and customInit hook
|
|
90
|
-
const tempDefine = this.tempDefine.bind(this);
|
|
91
|
-
global.LWR.define = tempDefine;
|
|
92
|
-
this.bootReady = this.config.autoBoot;
|
|
93
|
-
// Start watchdog timer
|
|
94
|
-
if (hasSetTimeout) {
|
|
95
|
-
this.watchdogTimerId = this.startWatchdogTimer();
|
|
96
|
-
}
|
|
97
|
-
try {
|
|
98
|
-
customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
|
|
99
|
-
this.errorHandler = e;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
catch (e) {
|
|
103
|
-
this.enterErrorState(e);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
// Return true if the app can be initialized
|
|
107
|
-
canInit() {
|
|
108
|
-
// Initialize the app if:
|
|
109
|
-
// - bootReady: autoBoot is on OR customInit has finished
|
|
110
|
-
// - all required modules are defined
|
|
111
|
-
const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
|
|
112
|
-
return this.bootReady && allDefined;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Create a temporary LWR.define() function which captures all
|
|
116
|
-
* calls that occur BEFORE the full loader module is available
|
|
117
|
-
*
|
|
118
|
-
* Each call to LWR.define() is stored in 2 ways:
|
|
119
|
-
* - in a map as [moduleName, arguments] pairs
|
|
120
|
-
* - each moduleName is pushed onto an array, to preserve
|
|
121
|
-
* the order in which the modules were defined
|
|
122
|
-
*/
|
|
123
|
-
tempDefine(...args) {
|
|
124
|
-
// Cache the incoming module
|
|
125
|
-
const moduleName = args[0];
|
|
126
|
-
this.defineCache[moduleName] = args;
|
|
127
|
-
this.orderedDefs.push(moduleName);
|
|
128
|
-
if (this.canInit()) {
|
|
129
|
-
if (hasSetTimeout) {
|
|
130
|
-
// requiredModules are defined, clear watchdog timer
|
|
131
|
-
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
132
|
-
clearTimeout(this.watchdogTimerId);
|
|
133
|
-
}
|
|
134
|
-
this.initApp();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
// Called by the customInit hook via lwr.initializeApp()
|
|
138
|
-
postCustomInit() {
|
|
139
|
-
this.bootReady = true;
|
|
140
|
-
if (this.canInit()) {
|
|
141
|
-
this.initApp();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
// Create the loader and initialize the application
|
|
145
|
-
initApp() {
|
|
146
|
-
try {
|
|
147
|
-
const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], this.config.baseUrl, this.config.preloadModules);
|
|
148
|
-
this.mountApp(loader);
|
|
149
|
-
}
|
|
150
|
-
catch (e) {
|
|
151
|
-
this.enterErrorState(e);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
waitForDOMContentLoaded() {
|
|
155
|
-
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
156
|
-
if (typeof document === undefined) {
|
|
157
|
-
return Promise.resolve();
|
|
158
|
-
}
|
|
159
|
-
// Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
|
|
160
|
-
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
161
|
-
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
162
|
-
return Promise.resolve();
|
|
163
|
-
}
|
|
164
|
-
return new Promise((resolve) => {
|
|
165
|
-
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
166
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
167
|
-
resolve();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
// Set up the application globals, import map, root custom element...
|
|
172
|
-
mountApp(loader) {
|
|
173
|
-
const { bootstrapModule, rootComponent, importMappings, rootComponents, endpoints } = this.config;
|
|
174
|
-
// Set global LWR.define to loader.define
|
|
175
|
-
this.global.LWR = Object.freeze({
|
|
176
|
-
define: loader.define.bind(loader),
|
|
177
|
-
rootComponent,
|
|
178
|
-
rootComponents,
|
|
179
|
-
importMappings,
|
|
180
|
-
endpoints,
|
|
181
|
-
});
|
|
182
|
-
// Redefine all modules in the temporary cache
|
|
183
|
-
this.orderedDefs.forEach((specifier) => {
|
|
184
|
-
if (specifier !== this.loaderModule) {
|
|
185
|
-
loader.define(...this.defineCache[specifier]);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
// Define a dummy profiler since the "lwr/profiler" module is used in "lwr/init" (which is global to all server modes)
|
|
189
|
-
const profilerExporter = (exports) => {
|
|
190
|
-
Object.assign(exports, {
|
|
191
|
-
logOperationStart: () => {
|
|
192
|
-
/* noop */
|
|
193
|
-
},
|
|
194
|
-
});
|
|
195
|
-
return;
|
|
196
|
-
};
|
|
197
|
-
loader.define('lwr/profiler/v/0_6_0-alpha_12', ['exports'], profilerExporter, {});
|
|
198
|
-
// by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
|
|
199
|
-
const { disableInitDefer } = this.config;
|
|
200
|
-
// Load the import mappings and application bootstrap module
|
|
201
|
-
loader
|
|
202
|
-
.registerImportMappings(importMappings)
|
|
203
|
-
.then(() => {
|
|
204
|
-
if (!disableInitDefer) {
|
|
205
|
-
return this.waitForDOMContentLoaded();
|
|
206
|
-
}
|
|
207
|
-
})
|
|
208
|
-
.then(() => loader.load(bootstrapModule))
|
|
209
|
-
.catch((reason) => {
|
|
210
|
-
this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
// Trigger bootstrap error state, and call error handler if registered
|
|
214
|
-
enterErrorState(error) {
|
|
215
|
-
if (this.errorHandler) {
|
|
216
|
-
this.errorHandler(error);
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
if (hasConsole) {
|
|
220
|
-
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
221
|
-
console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
// eslint-disable-next-line no-undef, lwr/no-unguarded-apis
|
|
226
|
-
startWatchdogTimer() {
|
|
227
|
-
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
228
|
-
return setTimeout(() => {
|
|
229
|
-
this.enterErrorState(new Error('Failed to load required modules - timed out'));
|
|
230
|
-
}, REQUIRED_MODULES_TIMEOUT);
|
|
231
|
-
}
|
|
33
|
+
function logOperationStart({
|
|
34
|
+
id,
|
|
35
|
+
specifier
|
|
36
|
+
}) {
|
|
37
|
+
if (customDispatcher) {
|
|
38
|
+
customDispatcher({
|
|
39
|
+
id,
|
|
40
|
+
phase: Phase.Start,
|
|
41
|
+
specifier
|
|
42
|
+
});
|
|
43
|
+
} else if (isPerfSupported) {
|
|
44
|
+
perf.mark(id + (specifier ? `.${specifier}` : ''));
|
|
232
45
|
}
|
|
46
|
+
} // For measuring duration metrics
|
|
47
|
+
// Fallback to the Performance API if there is no custom dispatcher
|
|
48
|
+
|
|
49
|
+
/* istanbul ignore next */
|
|
233
50
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
51
|
+
function logOperationEnd({
|
|
52
|
+
id,
|
|
53
|
+
specifier
|
|
54
|
+
}) {
|
|
55
|
+
if (customDispatcher) {
|
|
56
|
+
customDispatcher({
|
|
57
|
+
id,
|
|
58
|
+
phase: Phase.End,
|
|
59
|
+
specifier
|
|
60
|
+
});
|
|
61
|
+
} else if (isPerfSupported) {
|
|
62
|
+
const suffix = specifier ? `.${specifier}` : '';
|
|
63
|
+
const markName = id + suffix;
|
|
64
|
+
const measureName = `${id}.duration${suffix}`;
|
|
65
|
+
perf.measure(measureName, markName); // Clear the created mark and measure to avoid filling the performance entry buffer
|
|
66
|
+
// Even if they get deleted, existing PerformanceObservers preserve copies of the entries
|
|
67
|
+
|
|
68
|
+
perf.clearMarks(markName);
|
|
69
|
+
perf.clearMeasures(measureName);
|
|
239
70
|
}
|
|
240
|
-
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createLoader(name, definition, config, externalModules) {
|
|
74
|
+
if (!definition || typeof definition[2] !== 'function') {
|
|
75
|
+
throw new Error(`Expected loader with specifier "${name}" to be a module`);
|
|
76
|
+
}
|
|
77
|
+
// Create a Loader instance
|
|
78
|
+
const exports = {};
|
|
79
|
+
definition[2].call(null, exports);
|
|
80
|
+
const { Loader } = exports;
|
|
81
|
+
const loader = new Loader(config);
|
|
82
|
+
// register externally loaded modules
|
|
83
|
+
if (externalModules && externalModules.length) {
|
|
84
|
+
loader.registerExternalModules(externalModules);
|
|
85
|
+
}
|
|
86
|
+
// Define the loader module with public API: { define, load, services }
|
|
87
|
+
const exporter = (exports) => {
|
|
88
|
+
Object.assign(exports, {
|
|
89
|
+
define: loader.define.bind(loader),
|
|
90
|
+
load: loader.load.bind(loader),
|
|
91
|
+
services: loader.services,
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
};
|
|
95
|
+
loader.define(name, ['exports'], exporter, definition[3]);
|
|
96
|
+
return loader;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const REQUIRED_MODULES_TIMEOUT = 300 * 1000;
|
|
100
|
+
|
|
101
|
+
// Check for errors with autoBoot and customInit
|
|
102
|
+
function validatePreInit(autoBoot, customInit) {
|
|
103
|
+
// If autoBoot === false, there must be a customInit hook
|
|
104
|
+
if (!autoBoot && !customInit) {
|
|
105
|
+
throw new Error('The customInit hook is required when autoBoot is false');
|
|
106
|
+
}
|
|
107
|
+
// If autoBoot === true, there must NOT be a customInit hook
|
|
108
|
+
if (autoBoot && customInit) {
|
|
109
|
+
throw new Error('The customInit hook must not be defined when autoBoot is true');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Process the customInit hook
|
|
113
|
+
function customInit(config, initializeApp, define, onBootstrapError) {
|
|
114
|
+
// Validate config
|
|
115
|
+
const { autoBoot, customInit } = config;
|
|
116
|
+
validatePreInit(autoBoot, customInit);
|
|
117
|
+
// Set up arguments and call the customInit hook, if available
|
|
118
|
+
if (customInit) {
|
|
119
|
+
const lwr = {
|
|
120
|
+
initializeApp,
|
|
121
|
+
define,
|
|
122
|
+
onBootstrapError,
|
|
123
|
+
attachDispatcher,
|
|
124
|
+
};
|
|
125
|
+
customInit(lwr, config);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* global document */
|
|
130
|
+
/* eslint-disable lwr/no-unguarded-apis */
|
|
131
|
+
const hasSetTimeout = typeof setTimeout === 'function';
|
|
132
|
+
const hasConsole = typeof console !== 'undefined';
|
|
133
|
+
/* eslint-enable lwr/no-unguarded-apis */
|
|
134
|
+
class LoaderShim {
|
|
135
|
+
constructor(global) {
|
|
136
|
+
this.defineCache = {};
|
|
137
|
+
this.orderedDefs = [];
|
|
138
|
+
// Parse configuration
|
|
139
|
+
this.global = global;
|
|
140
|
+
this.config = global.LWR;
|
|
141
|
+
this.loaderModule = 'lwr/loaderLegacy/v/0_6_0-alpha_16';
|
|
142
|
+
// Set up the temporary LWR.define function and customInit hook
|
|
143
|
+
const tempDefine = this.tempDefine.bind(this);
|
|
144
|
+
global.LWR.define = tempDefine;
|
|
145
|
+
this.bootReady = this.config.autoBoot;
|
|
146
|
+
// Start watchdog timer
|
|
147
|
+
if (hasSetTimeout) {
|
|
148
|
+
this.watchdogTimerId = this.startWatchdogTimer();
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
customInit(Object.freeze(this.config), this.postCustomInit.bind(this), tempDefine, (e) => {
|
|
152
|
+
this.errorHandler = e;
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
this.enterErrorState(e);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Return true if the app can be initialized
|
|
160
|
+
canInit() {
|
|
161
|
+
// Initialize the app if:
|
|
162
|
+
// - bootReady: autoBoot is on OR customInit has finished
|
|
163
|
+
// - all required modules are defined
|
|
164
|
+
const allDefined = this.config.requiredModules.every((m) => this.orderedDefs.includes(m));
|
|
165
|
+
return this.bootReady && allDefined;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Create a temporary LWR.define() function which captures all
|
|
169
|
+
* calls that occur BEFORE the full loader module is available
|
|
170
|
+
*
|
|
171
|
+
* Each call to LWR.define() is stored in 2 ways:
|
|
172
|
+
* - in a map as [moduleName, arguments] pairs
|
|
173
|
+
* - each moduleName is pushed onto an array, to preserve
|
|
174
|
+
* the order in which the modules were defined
|
|
175
|
+
*/
|
|
176
|
+
tempDefine(...args) {
|
|
177
|
+
// Cache the incoming module
|
|
178
|
+
const moduleName = args[0];
|
|
179
|
+
this.defineCache[moduleName] = args;
|
|
180
|
+
this.orderedDefs.push(moduleName);
|
|
181
|
+
if (this.canInit()) {
|
|
182
|
+
if (hasSetTimeout) {
|
|
183
|
+
// requiredModules are defined, clear watchdog timer
|
|
184
|
+
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
185
|
+
clearTimeout(this.watchdogTimerId);
|
|
186
|
+
}
|
|
187
|
+
this.initApp();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Called by the customInit hook via lwr.initializeApp()
|
|
191
|
+
postCustomInit() {
|
|
192
|
+
this.bootReady = true;
|
|
193
|
+
if (this.canInit()) {
|
|
194
|
+
this.initApp();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// Create the loader and initialize the application
|
|
198
|
+
initApp() {
|
|
199
|
+
try {
|
|
200
|
+
const loaderConfig = {
|
|
201
|
+
baseUrl: this.config.baseUrl,
|
|
202
|
+
profiler: { logOperationStart, logOperationEnd },
|
|
203
|
+
// TODO: can be removed following https://github.com/salesforce/lwr/issues/1087
|
|
204
|
+
appMetadata: {
|
|
205
|
+
appId: this.config.appId,
|
|
206
|
+
bootstrapModule: this.config.bootstrapModule,
|
|
207
|
+
rootComponent: this.config.rootComponent,
|
|
208
|
+
rootComponents: this.config.rootComponents,
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], loaderConfig, this.config.preloadModules);
|
|
212
|
+
this.createProfilerModule(loader);
|
|
213
|
+
this.mountApp(loader);
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
this.enterErrorState(e);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
waitForDOMContentLoaded() {
|
|
220
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
221
|
+
if (typeof document === undefined) {
|
|
222
|
+
return Promise.resolve();
|
|
223
|
+
}
|
|
224
|
+
// Resolve if document is already "ready" https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
|
|
225
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
226
|
+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
227
|
+
return Promise.resolve();
|
|
228
|
+
}
|
|
229
|
+
return new Promise((resolve) => {
|
|
230
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
231
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
232
|
+
resolve();
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
// Create a module out of the profiler
|
|
237
|
+
// Note: The profiler is also available as a module through lwc module resolution (see package.json)
|
|
238
|
+
createProfilerModule(loader) {
|
|
239
|
+
const exporter = (exports) => {
|
|
240
|
+
Object.assign(exports, { logOperationStart, logOperationEnd });
|
|
241
|
+
};
|
|
242
|
+
loader.define('lwr/profiler/v/0_6_0-alpha_16', ['exports'], exporter, {});
|
|
243
|
+
}
|
|
244
|
+
// Set up the application globals, import map, root custom element...
|
|
245
|
+
mountApp(loader) {
|
|
246
|
+
const { bootstrapModule, rootComponent, importMappings, rootComponents, endpoints } = this.config;
|
|
247
|
+
// Set global LWR.define to loader.define
|
|
248
|
+
this.global.LWR = Object.freeze({
|
|
249
|
+
define: loader.define.bind(loader),
|
|
250
|
+
rootComponent,
|
|
251
|
+
rootComponents,
|
|
252
|
+
importMappings,
|
|
253
|
+
endpoints,
|
|
254
|
+
});
|
|
255
|
+
// Redefine all modules in the temporary cache
|
|
256
|
+
this.orderedDefs.forEach((specifier) => {
|
|
257
|
+
if (specifier !== this.loaderModule) {
|
|
258
|
+
loader.define(...this.defineCache[specifier]);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
// by default, app initialization is gated on waiting for document to be parsed (via DOMContentLoaded)
|
|
262
|
+
const { disableInitDefer } = this.config;
|
|
263
|
+
// Load the import mappings and application bootstrap module
|
|
264
|
+
loader
|
|
265
|
+
.registerImportMappings(importMappings)
|
|
266
|
+
.then(() => {
|
|
267
|
+
if (!disableInitDefer) {
|
|
268
|
+
return this.waitForDOMContentLoaded();
|
|
269
|
+
}
|
|
270
|
+
})
|
|
271
|
+
.then(() => loader.load(bootstrapModule))
|
|
272
|
+
.catch((reason) => {
|
|
273
|
+
this.enterErrorState(new Error(`Application ${rootComponent} could not be loaded: ${reason}`));
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
// Trigger bootstrap error state, and call error handler if registered
|
|
277
|
+
enterErrorState(error) {
|
|
278
|
+
logOperationStart({ id: BOOTSTRAP_ERROR });
|
|
279
|
+
if (this.errorHandler) {
|
|
280
|
+
this.errorHandler(error);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
if (hasConsole) {
|
|
284
|
+
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
285
|
+
console.error(`An error occurred during LWR bootstrap. ${error.message}`, error.stack);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// eslint-disable-next-line no-undef, lwr/no-unguarded-apis
|
|
290
|
+
startWatchdogTimer() {
|
|
291
|
+
// eslint-disable-next-line lwr/no-unguarded-apis, no-undef
|
|
292
|
+
return setTimeout(() => {
|
|
293
|
+
this.enterErrorState(new Error('Failed to load required modules - timed out'));
|
|
294
|
+
}, REQUIRED_MODULES_TIMEOUT);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// The loader module is ALWAYS required
|
|
299
|
+
const GLOBAL = globalThis;
|
|
300
|
+
GLOBAL.LWR.requiredModules = GLOBAL.LWR.requiredModules || [];
|
|
301
|
+
if (GLOBAL.LWR.requiredModules.indexOf('lwr/loaderLegacy/v/0_6_0-alpha_16') < 0) {
|
|
302
|
+
GLOBAL.LWR.requiredModules.push('lwr/loaderLegacy/v/0_6_0-alpha_16');
|
|
303
|
+
}
|
|
304
|
+
new LoaderShim(GLOBAL);
|
|
241
305
|
|
|
242
306
|
}());
|
|
243
307
|
|
|
244
|
-
LWR.define('lwr/loaderLegacy/v/0_6_0-
|
|
308
|
+
LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_16', ['exports'], function (exports) { 'use strict';
|
|
245
309
|
|
|
246
310
|
const templateRegex = /\{([0-9]+)\}/g;
|
|
247
311
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -639,14 +703,22 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
639
703
|
}
|
|
640
704
|
}
|
|
641
705
|
|
|
706
|
+
// Bootstrap / shim
|
|
707
|
+
|
|
708
|
+
const LOADER_PREFIX = 'lwr.loader.';
|
|
709
|
+
const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
|
|
710
|
+
const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
|
|
711
|
+
const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
|
|
712
|
+
|
|
642
713
|
/* global console,process */
|
|
643
714
|
class ModuleRegistry {
|
|
644
|
-
constructor(
|
|
715
|
+
constructor(config) {
|
|
645
716
|
// A registry for named AMD defines containing the *metadata* of AMD module
|
|
646
717
|
this.namedDefineRegistry = new Map();
|
|
647
718
|
// The evaluted module registry where the module identifier (name or URL?) is the key
|
|
648
719
|
this.moduleRegistry = new Map();
|
|
649
|
-
this.baseUrl = baseUrl;
|
|
720
|
+
this.baseUrl = config.baseUrl;
|
|
721
|
+
this.profiler = config.profiler;
|
|
650
722
|
}
|
|
651
723
|
async load(id, importer) {
|
|
652
724
|
const resolvedId = await this.resolve(id, importer);
|
|
@@ -761,6 +833,7 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
761
833
|
// if module is "external", resolve the external promise to notify any dependees
|
|
762
834
|
mod.external.resolveExternal(moduleDef);
|
|
763
835
|
}
|
|
836
|
+
this.profiler.logOperationStart({ id: MODULE_DEFINE, specifier: name });
|
|
764
837
|
this.namedDefineRegistry.set(name, moduleDef);
|
|
765
838
|
this.lastDefine = moduleDef;
|
|
766
839
|
// Check signatures of dependencies against those in the namedDefineRegistry
|
|
@@ -1031,6 +1104,8 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
1031
1104
|
return moduleDef;
|
|
1032
1105
|
}
|
|
1033
1106
|
const parentUrl = this.baseUrl; // only support baseUrl for now
|
|
1107
|
+
const specifier = moduleName || originalId;
|
|
1108
|
+
this.profiler.logOperationStart({ id: MODULE_FETCH, specifier });
|
|
1034
1109
|
return Promise.resolve()
|
|
1035
1110
|
.then(async () => {
|
|
1036
1111
|
const loadHooks = this.loadHook;
|
|
@@ -1068,9 +1143,11 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
1068
1143
|
if (!moduleDef) {
|
|
1069
1144
|
throw new LoaderError(FAIL_INSTANTIATE, [resolvedId]);
|
|
1070
1145
|
}
|
|
1146
|
+
this.profiler.logOperationEnd({ id: MODULE_FETCH, specifier });
|
|
1071
1147
|
return moduleDef;
|
|
1072
1148
|
})
|
|
1073
1149
|
.catch((e) => {
|
|
1150
|
+
this.profiler.logOperationStart({ id: MODULE_ERROR, specifier });
|
|
1074
1151
|
throw e;
|
|
1075
1152
|
});
|
|
1076
1153
|
}
|
|
@@ -1298,7 +1375,10 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
1298
1375
|
* The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
|
|
1299
1376
|
*/
|
|
1300
1377
|
class Loader {
|
|
1301
|
-
constructor(
|
|
1378
|
+
constructor(config) {
|
|
1379
|
+
config = config || {};
|
|
1380
|
+
let baseUrl = config.baseUrl;
|
|
1381
|
+
let profiler = config.profiler;
|
|
1302
1382
|
if (baseUrl) {
|
|
1303
1383
|
// add a trailing slash, if it does not exist
|
|
1304
1384
|
baseUrl = baseUrl.replace(/\/?$/, '/');
|
|
@@ -1310,10 +1390,23 @@ LWR.define('lwr/loaderLegacy/v/0_6_0-alpha_12', ['exports'], function (exports)
|
|
|
1310
1390
|
throw new LoaderError(NO_BASE_URL);
|
|
1311
1391
|
}
|
|
1312
1392
|
this.baseUrl = baseUrl;
|
|
1313
|
-
|
|
1393
|
+
if (!profiler) {
|
|
1394
|
+
// default noop profiler
|
|
1395
|
+
profiler = {
|
|
1396
|
+
logOperationStart: () => {
|
|
1397
|
+
/* noop */
|
|
1398
|
+
},
|
|
1399
|
+
logOperationEnd: () => {
|
|
1400
|
+
/* noop */
|
|
1401
|
+
},
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
this.registry = new ModuleRegistry({ baseUrl, profiler });
|
|
1405
|
+
// TODO: https://github.com/salesforce/lwr/issues/1087
|
|
1314
1406
|
this.services = Object.freeze({
|
|
1315
1407
|
addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
|
|
1316
1408
|
handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
|
|
1409
|
+
appMetadata: config.appMetadata,
|
|
1317
1410
|
});
|
|
1318
1411
|
}
|
|
1319
1412
|
/**
|