@lwrjs/loader 0.9.0-alpha.8 → 0.9.0

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.
@@ -4,7 +4,7 @@
4
4
  * SPDX-License-Identifier: MIT
5
5
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
6
  */
7
- /* LWR ESM Module Loader v0.9.0-alpha.8 */
7
+ /* LWR ESM Module Loader v0.9.0 */
8
8
  /**
9
9
  * Simplified version of the AMD Import Metadata Resolver.
10
10
  * Just reads the ImportMetadata at construction time.
@@ -87,37 +87,41 @@ async function load(specifier, importer) {
87
87
  return import(uri);
88
88
  }
89
89
  async function resolveUrl(specifier, importer) {
90
- let uri;
90
+ var _a;
91
+ // HMR imports complete URIs when swapping out modules
92
+ if (specifier.includes('://') || specifier.startsWith('/')) {
93
+ return specifier;
94
+ }
91
95
  if (!resolver || !resolverLegacy) {
92
96
  throw new Error('The ESM Loader was not initialized');
93
97
  }
94
98
  // Check if the URI is in the import metadata
95
- uri = await resolver.resolve(specifier);
96
- if (uri) {
97
- return uri;
99
+ if (resolver) {
100
+ const uri = await resolver.resolve(specifier);
101
+ if (uri) {
102
+ return uri;
103
+ }
98
104
  }
99
105
  // Check if the URI is in the legacy import metadata
100
- uri = resolverLegacy.legacyResolve(specifier);
101
- if (uri) {
102
- return uri;
103
- }
104
- // Else fall back to the module endpoint
105
- uri = specifier;
106
- // do not alter the specifier if it is already a URL
107
- if (uri.indexOf('://') < 0 && !uri.startsWith('/')) {
108
- // add the specifier and importer to the default URI
109
- const { endpoints } = esmLoaderConfig;
110
- if (endpoints && endpoints.uris && endpoints.uris.module) {
111
- uri = endpoints.uris.module + encodeURIComponent(specifier);
112
- if (importer) {
113
- uri += `?importer=${encodeURIComponent(importer)}`;
114
- }
115
- if (endpoints.modifiers) {
116
- // Add URI modifiers to query
117
- uri += Object.entries(endpoints.modifiers).reduce((q, [k, v]) => (q += `${k}=${v}&`), importer ? '&' : '?');
118
- }
106
+ if (resolverLegacy) {
107
+ const uri = resolverLegacy.legacyResolve(specifier);
108
+ if (uri) {
109
+ return uri;
119
110
  }
120
111
  }
112
+ const { endpoints } = esmLoaderConfig;
113
+ if (!((_a = endpoints === null || endpoints === void 0 ? void 0 : endpoints.uris) === null || _a === void 0 ? void 0 : _a.module)) {
114
+ throw new Error(`Unable to resolve the URL for "${specifier}"`);
115
+ }
116
+ // add the specifier and importer to the default URI
117
+ let uri = endpoints.uris.module + encodeURIComponent(specifier);
118
+ if (importer) {
119
+ uri += `?importer=${encodeURIComponent(importer)}`;
120
+ }
121
+ if (endpoints.modifiers) {
122
+ // Add URI modifiers to query
123
+ uri += Object.entries(endpoints.modifiers).reduce((q, [k, v]) => (q += `${k}=${v}&`), importer ? '&' : '?');
124
+ }
121
125
  return uri;
122
126
  }
123
127
 
@@ -1,9 +1,8 @@
1
- import { LwrCompiler } from '@lwrjs/compiler'; // eslint-disable-line lwr/only-allowed-imports
2
1
  import { readFile } from '@lwrjs/shared-utils'; // eslint-disable-line lwr/only-allowed-imports
3
- const compiler = new LwrCompiler();
2
+ import { convertToAmd } from '@lwrjs/shared-utils/compiler'; // eslint-disable-line lwr/only-allowed-imports
4
3
  export async function getAMDModule(specifier, filename, autoExports) {
5
4
  const code = readFile(filename);
6
- const { code: amdModule } = await compiler.convertToAmd(code, { id: specifier }, autoExports !== true);
5
+ const { code: amdModule } = await convertToAmd(code, { id: specifier }, autoExports !== true);
7
6
  return amdModule;
8
7
  }
9
8
  export function evaluateDefine(moduleString) {
@@ -4,7 +4,7 @@
4
4
  * SPDX-License-Identifier: MIT
5
5
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
6
  */
7
- /* LWR Module Loader v0.9.0-alpha.8 */
7
+ /* LWR Module Loader v0.9.0 */
8
8
  const templateRegex = /\{([0-9]+)\}/g;
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  function templateString(template, args) {
@@ -102,7 +102,7 @@ const STALE_HOOK_ERROR = Object.freeze({
102
102
  const MODULE_ALREADY_LOADED = Object.freeze({
103
103
  code: 3017,
104
104
  level: 0,
105
- message: 'Marking module(s) as externally loaded, but they are already loaded: {0}',
105
+ message: 'Marking module(s) as externally loaded, but they are already loaded:',
106
106
  });
107
107
  const FAIL_HOOK_LOAD = Object.freeze({
108
108
  code: 3018,
@@ -301,11 +301,13 @@ if (hasDocument) {
301
301
 
302
302
  // Bootstrap / shim
303
303
 
304
+ // Loader: modules
304
305
  const LOADER_PREFIX = 'lwr.loader.';
305
306
  const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
306
307
  const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
307
308
  const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
308
309
 
310
+ // Loader: mappings
309
311
  const MAPPINGS_FETCH = `${LOADER_PREFIX}mappings.fetch`;
310
312
  const MAPPINGS_ERROR = `${LOADER_PREFIX}mappings.error`;
311
313
 
@@ -766,12 +768,8 @@ class ModuleRegistry {
766
768
  * @param modules - list of module identifiers
767
769
  */
768
770
  registerExternalModules(modules) {
769
- const alreadyRegistered = [];
770
771
  modules.map((id) => {
771
- if (this.namedDefineRegistry.has(id)) {
772
- alreadyRegistered.push(id);
773
- }
774
- else {
772
+ if (!this.namedDefineRegistry.has(id)) {
775
773
  let resolveExternal;
776
774
  let timer;
777
775
  const moduleDefPromise = new Promise((resolve, reject) => {
@@ -795,11 +793,11 @@ class ModuleRegistry {
795
793
  };
796
794
  this.namedDefineRegistry.set(id, moduleDef);
797
795
  }
796
+ else if (process.env.NODE_ENV !== 'production' && hasConsole) {
797
+ // eslint-disable-next-line lwr/no-unguarded-apis
798
+ console.warn(MODULE_ALREADY_LOADED.message, id);
799
+ }
798
800
  });
799
- // throw error for modules that were already registered
800
- if (alreadyRegistered.length) {
801
- throw new LoaderError(MODULE_ALREADY_LOADED, [alreadyRegistered.join(', ')]);
802
- }
803
801
  }
804
802
  getImportMetadataResolver() {
805
803
  return this.resolver;
@@ -941,7 +939,7 @@ class ModuleRegistry {
941
939
  moduleDefault = { default: moduleDefault };
942
940
  // __defaultInterop is ONLY used to support backwards compatibility
943
941
  // of importing default exports the "wrong" way (when not using named exports).
944
- // See https://github.com/salesforce/lwr/pull/816
942
+ // See https://github.com/salesforce-experience-platform-emu/lwr/pull/816
945
943
  Object.defineProperty(moduleDefault, '__defaultInterop', { value: true });
946
944
  }
947
945
  // if no return value, then we are using the exports object
@@ -1163,7 +1161,7 @@ class Loader {
1163
1161
  const appId = match && match[1];
1164
1162
  config.appMetadata.appId = appId;
1165
1163
  }
1166
- // TODO: https://github.com/salesforce/lwr/issues/1087
1164
+ // TODO: https://github.com/salesforce-experience-platform-emu/lwr/issues/1087
1167
1165
  this.services = Object.freeze({
1168
1166
  addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
1169
1167
  handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
@@ -1,9 +1,8 @@
1
- import { LwrCompiler } from '@lwrjs/compiler'; // eslint-disable-line lwr/only-allowed-imports
2
- import { readFile } from '@lwrjs/shared-utils'; // eslint-disable-line lwr/only-allowed-imports
3
- const compiler = new LwrCompiler();
1
+ import { getImportMetadata, readFile } from '@lwrjs/shared-utils'; // eslint-disable-line lwr/only-allowed-imports
2
+ import { convertToAmd } from '@lwrjs/shared-utils/compiler'; // eslint-disable-line lwr/only-allowed-imports
4
3
  export async function getAMDModule(specifier, filename, fixtureHashes, autoExports) {
5
4
  const code = readFile(filename);
6
- const metadata = await compiler.getModuleMetadata(code);
5
+ const metadata = await getImportMetadata(code);
7
6
  const depHashes = {};
8
7
  if (metadata && metadata.imports && metadata.imports.length > 0) {
9
8
  metadata.imports.forEach((dependency) => {
@@ -11,7 +10,7 @@ export async function getAMDModule(specifier, filename, fixtureHashes, autoExpor
11
10
  depHashes[specifier] = fixtureHashes[specifier];
12
11
  });
13
12
  }
14
- const { code: amdModule } = await compiler.convertToAmd(code, { id: specifier }, autoExports !== true);
13
+ const { code: amdModule } = await convertToAmd(code, { id: specifier }, autoExports !== true);
15
14
  const openAmdCodeString = amdModule.trim().slice(0, -2);
16
15
  const signedCode = `${openAmdCodeString}, ${JSON.stringify({
17
16
  ownHash: fixtureHashes[specifier],
@@ -4,7 +4,7 @@
4
4
  * SPDX-License-Identifier: MIT
5
5
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6
6
  */
7
- /* LWR Legacy Module Loader v0.9.0-alpha.8 */
7
+ /* LWR Legacy Module Loader v0.9.0 */
8
8
  const templateRegex = /\{([0-9]+)\}/g;
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  function templateString(template, args) {
@@ -402,6 +402,7 @@ function evaluateHandleStaleModuleHooks(handleStaleModuleHooks, hookArgs) {
402
402
 
403
403
  // Bootstrap / shim
404
404
 
405
+ // Loader: modules
405
406
  const LOADER_PREFIX = 'lwr.loader.';
406
407
  const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
407
408
  const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
@@ -548,12 +549,8 @@ class ModuleRegistry {
548
549
  * @param modules - list of module identifiers
549
550
  */
550
551
  registerExternalModules(modules) {
551
- const alreadyRegistered = [];
552
552
  modules.map((id) => {
553
- if (this.namedDefineRegistry.has(id)) {
554
- alreadyRegistered.push(id);
555
- }
556
- else {
553
+ if (!this.namedDefineRegistry.has(id)) {
557
554
  let resolveExternal;
558
555
  let timer;
559
556
  const moduleDefPromise = new Promise((resolve, reject) => {
@@ -577,11 +574,11 @@ class ModuleRegistry {
577
574
  };
578
575
  this.namedDefineRegistry.set(id, moduleDef);
579
576
  }
577
+ else if (process.env.NODE_ENV !== 'production' && hasConsole) {
578
+ // eslint-disable-next-line lwr/no-unguarded-apis
579
+ console.warn(MODULE_ALREADY_LOADED.message, id);
580
+ }
580
581
  });
581
- // throw error for modules that were already registered
582
- if (alreadyRegistered.length) {
583
- throw new LoaderError(MODULE_ALREADY_LOADED, [alreadyRegistered.join(', ')]);
584
- }
585
582
  }
586
583
  checkModuleSignature(name, signature) {
587
584
  const moduleDef = this.namedDefineRegistry.get(name);
@@ -755,7 +752,7 @@ class ModuleRegistry {
755
752
  moduleDefault = { default: moduleDefault };
756
753
  // __defaultInterop is ONLY used to support backwards compatibility
757
754
  // of importing default exports the "wrong" way (when not using named exports).
758
- // See https://github.com/salesforce/lwr/pull/816
755
+ // See https://github.com/salesforce-experience-platform-emu/lwr/pull/816
759
756
  Object.defineProperty(moduleDefault, '__defaultInterop', { value: true });
760
757
  }
761
758
  // if no return value, then we are using the exports object
@@ -967,7 +964,7 @@ function applyPackages(id, packages, defaultUri) {
967
964
  // When a specifier's URI cannot be resolved via the imports, fallback to "default".
968
965
  // -> https://rfcs.lwc.dev/rfcs/lwr/0000-import-metadata#json-schema
969
966
  // However, if `id` is already a fully resolved url,
970
- // we cannot prepend the defaultUri -> https://github.com/salesforce/lwr/issues/378.
967
+ // we cannot prepend the defaultUri -> https://github.com/salesforce-experience-platform-emu/lwr/issues/378.
971
968
  // In this case we do not apply any package mappings and allow the caller (resolveImportMapEntry) to handle it.
972
969
  if (!isUrl(id)) {
973
970
  return defaultUri + encodeURIComponent(id);
@@ -1134,7 +1131,7 @@ class Loader {
1134
1131
  };
1135
1132
  }
1136
1133
  this.registry = new ModuleRegistry({ baseUrl, profiler });
1137
- // TODO: https://github.com/salesforce/lwr/issues/1087
1134
+ // TODO: https://github.com/salesforce-experience-platform-emu/lwr/issues/1087
1138
1135
  this.services = Object.freeze({
1139
1136
  addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
1140
1137
  handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
package/package.json CHANGED
@@ -5,15 +5,15 @@
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
- "version": "0.9.0-alpha.8",
8
+ "version": "0.9.0",
9
9
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/salesforce/lwr.git",
12
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr.git",
13
13
  "directory": "packages/@lwrjs/loader"
14
14
  },
15
15
  "bugs": {
16
- "url": "https://github.com/salesforce/lwr/issues"
16
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr/issues"
17
17
  },
18
18
  "type": "module",
19
19
  "types": "build/index.d.ts",
@@ -61,13 +61,14 @@
61
61
  "build": "yarn build:ts && yarn build:error-shim && yarn build:shim && yarn build:loader && yarn build:shim:bundle && yarn build:shim:bundle:minify"
62
62
  },
63
63
  "devDependencies": {
64
- "@lwrjs/compiler": "0.9.0-alpha.8",
65
- "@lwrjs/diagnostics": "0.9.0-alpha.8",
66
- "@lwrjs/types": "0.9.0-alpha.8",
64
+ "@locker/compiler": "0.18.14",
65
+ "@lwrjs/diagnostics": "0.9.0",
66
+ "@lwrjs/types": "0.9.0",
67
+ "rollup": "~2.45.2",
67
68
  "rollup-plugin-terser": "^7.0.2"
68
69
  },
69
70
  "dependencies": {
70
- "@lwrjs/shared-utils": "0.9.0-alpha.8"
71
+ "@lwrjs/shared-utils": "0.9.0"
71
72
  },
72
73
  "lwc": {
73
74
  "modules": [
@@ -82,7 +83,7 @@
82
83
  ]
83
84
  },
84
85
  "engines": {
85
- "node": ">=14.15.4 <19"
86
+ "node": ">=16.0.0 <20"
86
87
  },
87
- "gitHead": "013fcee240ff2e1c23ef304e70fc39d6ef13fb8b"
88
+ "gitHead": "4e42b0dc5453f92b36b42aa8132c5bc281e616b7"
88
89
  }