@lwrjs/loader 0.22.9 → 0.22.11

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 (37) hide show
  1. package/README.md +33 -1
  2. package/build/assets/prod/lwr-error-shim.js +1 -1
  3. package/build/assets/prod/lwr-loader-shim-legacy.bundle.js +530 -25
  4. package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +3 -3
  5. package/build/assets/prod/lwr-loader-shim-legacy.js +317 -14
  6. package/build/assets/prod/lwr-loader-shim.bundle.js +123 -12
  7. package/build/assets/prod/lwr-loader-shim.bundle.min.js +3 -3
  8. package/build/assets/prod/lwr-loader-shim.js +105 -8
  9. package/build/cjs/modules/lwr/loader/validateLoadSpecifier.cjs +4 -1
  10. package/build/cjs/modules/lwr/loaderLegacy/importMap/importMap.cjs +1 -1
  11. package/build/cjs/modules/lwr/loaderLegacy/importMap/importMapResolver.cjs +12 -0
  12. package/build/cjs/modules/lwr/loaderLegacy/importMap/utils.cjs +13 -1
  13. package/build/cjs/modules/lwr/loaderLegacy/utils/validation.cjs +93 -0
  14. package/build/modules/lwr/esmLoader/esmLoader.js +1 -1
  15. package/build/modules/lwr/loader/loader.js +18 -4
  16. package/build/modules/lwr/loader/validateLoadSpecifier.d.ts +2 -1
  17. package/build/modules/lwr/loader/validateLoadSpecifier.js +11 -2
  18. package/build/modules/lwr/loaderLegacy/importMap/importMap.js +3 -2
  19. package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.d.ts +1 -0
  20. package/build/modules/lwr/loaderLegacy/importMap/importMapResolver.js +13 -0
  21. package/build/modules/lwr/loaderLegacy/importMap/utils.d.ts +12 -1
  22. package/build/modules/lwr/loaderLegacy/importMap/utils.js +24 -2
  23. package/build/modules/lwr/loaderLegacy/loaderLegacy.d.ts +11 -1
  24. package/build/modules/lwr/loaderLegacy/loaderLegacy.js +213 -11
  25. package/build/modules/lwr/loaderLegacy/utils/validation.d.ts +50 -0
  26. package/build/modules/lwr/loaderLegacy/utils/validation.js +114 -0
  27. package/build/shim/defineCacheResolver.d.ts +10 -0
  28. package/build/shim/defineCacheResolver.js +78 -0
  29. package/build/shim/loader.d.ts +5 -1
  30. package/build/shim/loader.js +14 -3
  31. package/build/shim/shim.js +3 -2
  32. package/build/shim-legacy/loaderLegacy.d.ts +7 -2
  33. package/build/shim-legacy/loaderLegacy.js +15 -4
  34. package/build/shim-legacy/shimLegacy.d.ts +14 -0
  35. package/build/shim-legacy/shimLegacy.js +53 -4
  36. package/build/types.d.ts +22 -0
  37. package/package.json +6 -6
@@ -6,6 +6,7 @@ export default class LoaderShim {
6
6
  private loaderModule;
7
7
  private defineCache;
8
8
  private orderedDefs;
9
+ private importMapUpdatesCache;
9
10
  private errorHandler?;
10
11
  private watchdogTimerId?;
11
12
  constructor(global: GlobalThis);
@@ -20,6 +21,19 @@ export default class LoaderShim {
20
21
  * the order in which the modules were defined
21
22
  */
22
23
  private tempDefine;
24
+ /**
25
+ * Create a temporary LWR.importMap() function which captures all
26
+ * import map updates that occur BEFORE the full loader module is available
27
+ *
28
+ * Each import map update is validated, converted to moduleName -> URL mapping,
29
+ * and merged into the importMapUpdatesCache with write-once protection
30
+ */
31
+ private tempImportMap;
32
+ /**
33
+ * Apply all cached import map updates and merge with bootstrap import map
34
+ * Returns merged import map
35
+ */
36
+ private getImportMappingsWithUpdates;
23
37
  private postCustomInit;
24
38
  private initApp;
25
39
  private waitForBody;
@@ -4,6 +4,8 @@ import { logOperationStart, logOperationEnd } from 'lwr/profiler';
4
4
  import { createLoader } from './loaderLegacy.js';
5
5
  import { REQUIRED_MODULES_TIMEOUT } from '../shim/constants.js';
6
6
  import { customInit } from '../shim/customInit.js';
7
+ import { validateAndConvertImportMapUpdate } from '../modules/lwr/loaderLegacy/utils/validation.js';
8
+ import { mergeImportMapEntry } from '../modules/lwr/loaderLegacy/importMap/utils.js';
7
9
  /* eslint-disable lwr/no-unguarded-apis */
8
10
  const hasSetTimeout = typeof setTimeout === 'function';
9
11
  const hasConsole = typeof console !== 'undefined';
@@ -13,6 +15,7 @@ export default class LoaderShim {
13
15
  constructor(global) {
14
16
  this.defineCache = {};
15
17
  this.orderedDefs = [];
18
+ this.importMapUpdatesCache = {};
16
19
  // Start watchdog timer
17
20
  if (hasSetTimeout) {
18
21
  this.watchdogTimerId = this.startWatchdogTimer();
@@ -23,9 +26,11 @@ export default class LoaderShim {
23
26
  this.loaderModule = 'lwr/loaderLegacy/v/__VERSION__';
24
27
  // Set up error handler
25
28
  this.errorHandler = this.config.onError;
26
- // Set up the temporary LWR.define function and customInit hook
29
+ // Set up the temporary LWR.define and LWR.importMap functions
27
30
  const tempDefine = this.tempDefine.bind(this);
28
31
  global.LWR.define = tempDefine;
32
+ const tempImportMapMethod = this.tempImportMap.bind(this);
33
+ global.LWR.importMap = tempImportMapMethod;
29
34
  this.bootReady = this.config.autoBoot;
30
35
  try {
31
36
  this.createProfilerModule(global.LWR.define);
@@ -73,6 +78,48 @@ export default class LoaderShim {
73
78
  this.initApp();
74
79
  }
75
80
  }
81
+ /**
82
+ * Create a temporary LWR.importMap() function which captures all
83
+ * import map updates that occur BEFORE the full loader module is available
84
+ *
85
+ * Each import map update is validated, converted to moduleName -> URL mapping,
86
+ * and merged into the importMapUpdatesCache with write-once protection
87
+ */
88
+ tempImportMap(importMapUpdate) {
89
+ try {
90
+ // Validate and convert the import map update to { imports: { moduleName: moduleScriptURL } }
91
+ const importMap = validateAndConvertImportMapUpdate(importMapUpdate);
92
+ // Early return if update was empty
93
+ if (!importMap) {
94
+ return;
95
+ }
96
+ // Merge into cache with write-once protection
97
+ for (const [moduleName, moduleScriptURL] of Object.entries(importMap.imports)) {
98
+ mergeImportMapEntry(moduleName, moduleScriptURL, this.importMapUpdatesCache);
99
+ }
100
+ }
101
+ catch (e) {
102
+ this.enterErrorState(e);
103
+ }
104
+ }
105
+ /**
106
+ * Apply all cached import map updates and merge with bootstrap import map
107
+ * Returns merged import map
108
+ */
109
+ getImportMappingsWithUpdates() {
110
+ // Start with bootstrap import map
111
+ // Cast importMappings from object to ImportMap to access properties
112
+ const bootstrapMappings = this.config.importMappings;
113
+ // Merge with write-once protection: bootstrap mappings take precedence
114
+ const mergedImports = { ...(bootstrapMappings?.imports || {}) };
115
+ for (const [specifier, uri] of Object.entries(this.importMapUpdatesCache)) {
116
+ mergeImportMapEntry(specifier, uri, mergedImports);
117
+ }
118
+ return {
119
+ ...(bootstrapMappings || {}),
120
+ imports: mergedImports,
121
+ };
122
+ }
76
123
  // Called by the customInit hook via lwr.initializeApp()
77
124
  postCustomInit() {
78
125
  this.bootReady = true;
@@ -99,7 +146,7 @@ export default class LoaderShim {
99
146
  rootComponents: this.config.rootComponents,
100
147
  },
101
148
  };
102
- const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], loaderConfig, this.config.preloadModules);
149
+ const loader = createLoader(this.loaderModule, this.defineCache[this.loaderModule], loaderConfig, this.config.preloadModules, this.defineCache);
103
150
  this.mountApp(loader);
104
151
  if (loader &&
105
152
  typeof loader.getModuleWarnings === 'function' &&
@@ -155,10 +202,12 @@ export default class LoaderShim {
155
202
  }
156
203
  // Set up the application globals, import map, root custom element...
157
204
  mountApp(loader) {
158
- const { bootstrapModule, rootComponent, importMappings, rootComponents, serverData, endpoints } = this.config;
205
+ const { bootstrapModule, rootComponent, rootComponents, serverData, endpoints } = this.config;
206
+ const importMappings = this.getImportMappingsWithUpdates();
159
207
  // Set global LWR.define to loader.define
160
208
  this.global.LWR = Object.freeze({
161
209
  define: loader.define.bind(loader),
210
+ importMap: loader.importMap.bind(loader),
162
211
  rootComponent,
163
212
  rootComponents,
164
213
  serverData: serverData || {},
@@ -180,7 +229,7 @@ export default class LoaderShim {
180
229
  .registerImportMappings(importMappings)
181
230
  .then(() => {
182
231
  // eslint-disable-next-line lwr/no-unguarded-apis
183
- if (typeof window === 'undefined' || typeof document === undefined) {
232
+ if (typeof window === 'undefined' || typeof document === 'undefined') {
184
233
  return Promise.resolve();
185
234
  }
186
235
  if (initDeferDOM) {
package/build/types.d.ts CHANGED
@@ -4,14 +4,35 @@ import type { ProfilerAPI, LogDispatcher } from 'lwr/profiler';
4
4
  export type GlobalThis = {
5
5
  LWR: Partial<ClientBootstrapConfig> & {
6
6
  define: LoaderDefine;
7
+ importMap?: LwrImportMapUpdateMethod;
7
8
  };
8
9
  [key: string]: unknown;
9
10
  };
10
11
  export type LoaderDefine = (id: string, deps: string[], exporter: Function, hash?: object) => void;
12
+ /** define(id, deps, factory, hash?) - full form. Same shape as LoaderDefine args. (from main) */
11
13
  export type DefineArguments = [string, string[], Function, object?];
14
+ /** Raw args from define() - at least [id, depsOrFactory]; may include factory, hash. Used for parsing. */
15
+ export type DefineArgsInput = readonly [id: string, depsOrFactory: unknown, factory?: unknown, hash?: object];
16
+ export interface ParseDefineResult {
17
+ deps: string[];
18
+ /** Same as DefineArguments[2] / LoaderDefine exporter */
19
+ factory: Function;
20
+ }
21
+ /** Normalized loader factory: receives (exports, ...resolvedDeps). Used after resolveLoaderDepsFromDefineCache. */
22
+ export type LoaderFactory = (exports: object, ...deps: unknown[]) => void;
23
+ /** Args passed to the loader factory: [exports, ...resolvedDeps]. First element is always the exports bag. */
24
+ export type LoaderFactoryArgs = [exports: object, ...deps: unknown[]];
25
+ /** Result of resolveLoaderDepsFromDefineCache. Typed for createLoader consumption. */
26
+ export interface ResolvedLoader {
27
+ factory: LoaderFactory;
28
+ args: LoaderFactoryArgs;
29
+ exportsObj: object;
30
+ }
12
31
  export type LoaderClass = {
13
32
  new (config?: LoaderConfig): LoaderAPI;
14
33
  };
34
+ export type ImportMapUpdate = Record<string, string[]>;
35
+ export type LwrImportMapUpdateMethod = (update: ImportMapUpdate) => void;
15
36
  export interface BaseLoaderAPI {
16
37
  define: LoaderDefine;
17
38
  load(id: string): Promise<unknown>;
@@ -22,6 +43,7 @@ export interface BaseLoaderAPI {
22
43
  }
23
44
  export interface LoaderAPI extends BaseLoaderAPI {
24
45
  registerImportMappings(mappings?: ImportMap): Promise<void>;
46
+ importMap: LwrImportMapUpdateMethod;
25
47
  }
26
48
  export type LoaderConfig = {
27
49
  baseUrl?: string;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
- "version": "0.22.9",
8
+ "version": "0.22.11",
9
9
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
10
10
  "repository": {
11
11
  "type": "git",
@@ -61,16 +61,16 @@
61
61
  },
62
62
  "devDependencies": {
63
63
  "@locker/trusted-types": "0.26.4",
64
- "@lwrjs/diagnostics": "0.22.9",
65
- "@lwrjs/types": "0.22.9",
64
+ "@lwrjs/diagnostics": "0.22.11",
65
+ "@lwrjs/types": "0.22.11",
66
66
  "@rollup/plugin-node-resolve": "^15.2.3",
67
67
  "@rollup/plugin-sucrase": "^5.0.2",
68
68
  "@rollup/plugin-terser": "^0.4.4",
69
69
  "rollup": "^2.80.0"
70
70
  },
71
71
  "dependencies": {
72
- "@lwrjs/client-modules": "0.22.9",
73
- "@lwrjs/shared-utils": "0.22.9"
72
+ "@lwrjs/client-modules": "0.22.11",
73
+ "@lwrjs/shared-utils": "0.22.11"
74
74
  },
75
75
  "lwc": {
76
76
  "modules": [
@@ -90,5 +90,5 @@
90
90
  "volta": {
91
91
  "extends": "../../../package.json"
92
92
  },
93
- "gitHead": "d0c112cd7b6eb940d6e554d0bbd93fb535465fdc"
93
+ "gitHead": "85710371aa359747e08a064d4b4a2b3dfed1d30a"
94
94
  }