@lwrjs/loader 0.13.0-alpha.9 → 0.13.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.
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 +39 -28
  3. package/build/assets/prod/lwr-loader-shim-legacy.bundle.min.js +3 -3
  4. package/build/assets/prod/lwr-loader-shim-legacy.js +17 -15
  5. package/build/assets/prod/lwr-loader-shim.bundle.js +41 -22
  6. package/build/assets/prod/lwr-loader-shim.bundle.min.js +3 -3
  7. package/build/assets/prod/lwr-loader-shim.js +22 -11
  8. package/build/bundle/prod/lwr/esmLoader/esmLoader.js +1 -1
  9. package/build/cjs/index.cjs +27 -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 +9 -11
  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 +11 -11
  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 +27 -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 +19 -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 +58 -0
  92. package/build/modules/lwr/loaderLegacy/loaderLegacy.js +22 -13
  93. package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.d.ts +54 -0
  94. package/build/modules/lwr/loaderLegacy/moduleRegistry/moduleRegistry.js +547 -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 +30 -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 +57 -0
  118. package/build/types.js +2 -0
  119. package/package.json +10 -11
@@ -0,0 +1,4 @@
1
+ declare function getMatch(path: string, matchObj: Record<string, unknown>): string | undefined;
2
+ declare function targetWarning(match: string, target: string, msg: string): void;
3
+ export { getMatch, targetWarning };
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { hasConsole } from '../utils/dom.js';
2
+ // find the longest set of segments from path which are a key in matchObj
3
+ // eg: getMatch('/a/b/c', { '/a/b': ..., '/a': ..., '/d/e/f': ...}) => '/a/b'
4
+ function getMatch(path, matchObj) {
5
+ if (matchObj[path]) {
6
+ return path;
7
+ }
8
+ let sepIndex = path.length;
9
+ do {
10
+ const segment = path.slice(0, sepIndex + 1);
11
+ if (segment in matchObj) {
12
+ return segment;
13
+ }
14
+ } while (path.length > 1 && (sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
15
+ }
16
+ function targetWarning(match, target, msg) {
17
+ if (hasConsole) {
18
+ // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
19
+ console.warn('Package target ' + msg + ", resolving target '" + target + "' for " + match);
20
+ }
21
+ }
22
+ export { getMatch, targetWarning };
23
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,6 @@
1
+ import type { ImportDefinition, ImportMap } from '../importMap/importMap.js';
2
+ export interface ImportResolver {
3
+ importMap: ImportMap;
4
+ resolve(resolvedOrPlain: string, parentUrl: string): ImportDefinition | undefined;
5
+ }
6
+ //# sourceMappingURL=importResolver.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=importResolver.js.map
@@ -0,0 +1,58 @@
1
+ import { Module, ModuleDefinitionSignatures } from './moduleRegistry/moduleRegistry.js';
2
+ import { ImportMap } from './importMap/importMap.js';
3
+ import type { ServiceAPI } from '@lwrjs/types';
4
+ import type { LoaderConfig } from '../../../types.js';
5
+ /**
6
+ * The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
7
+ */
8
+ export declare class Loader {
9
+ private registry;
10
+ private baseUrl;
11
+ readonly services: Readonly<Pick<ServiceAPI, 'addLoaderPlugin' | 'handleStaleModule' | 'appMetadata'>>;
12
+ constructor(config?: LoaderConfig);
13
+ /**
14
+ * Defines/registers a single named AMD module definition.
15
+ *
16
+ * @param {string} name The module name
17
+ * @param {string[]} dependencies A list of module dependencies (module imports)
18
+ * @param {Function} execute The function containing the module code. AKA exporter as it also returns the modules exports when executed
19
+ * @param {ModuleDefinitionSignatures} signatures Object containing the module signature and the signatures of its dependencies
20
+ * @return {void}
21
+ */
22
+ define(name: string, dependencies: string[], execute: Function, signatures: ModuleDefinitionSignatures): void;
23
+ /**
24
+ * Retrieves/loads a module, returning it from the registry if it exists and fetching it if it doesn't.
25
+ *
26
+ * @param {string} id - A module identifier or URL
27
+ * @param {string} importer - The versioned specifier of the module importer
28
+ * Used when the ID is not versioned (eg: variable dynamic imports)
29
+ * @return {Promise<Module>}
30
+ */
31
+ load(id: string, importer?: string): Promise<Module>;
32
+ clearRegistry(): void;
33
+ /**
34
+ * Checks if a Module exists in the registry. Note, returns false even if the ModuleDefinition exists but the Module has not been instantiated yet (executed).
35
+ *
36
+ * @param {string} id - A module identifier or URL
37
+ * @return {boolean}
38
+ */
39
+ has(id: string): boolean;
40
+ /**
41
+ * Resolves the module identifier or URL. Returns the module identifier if the moduleDefinition exists, or the full resolved URL if a URL is given.
42
+ *
43
+ * @param {string} id - A module identifier or URL
44
+ * @param {string} importer - The versioned specifier of the module importer
45
+ * Used when the ID is not versioned (eg: variable dynamic imports)
46
+ * @return {string}
47
+ */
48
+ resolve(id: string, importer?: string): Promise<string>;
49
+ private parentImportMap?;
50
+ registerImportMappings(mappings?: ImportMap): Promise<void>;
51
+ /**
52
+ * Marks modules as "externally" loaded/provided (e.g. preloaded), so that the loader does not attempt to load them.
53
+ *
54
+ * @param modules - list of module identifiers
55
+ */
56
+ registerExternalModules(modules: string[]): void;
57
+ }
58
+ //# sourceMappingURL=loaderLegacy.d.ts.map
@@ -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.13.0-alpha.9 */
7
+ /* LWR Legacy Module Loader v0.13.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) {
@@ -377,7 +377,7 @@ try {
377
377
  // swallow
378
378
  }
379
379
  const trusted = createPolicy('trusted', policyOptions);
380
- /*! version: 0.22.2 */
380
+ /*! version: 0.22.4 */
381
381
 
382
382
  /* global console,process */
383
383
 
@@ -519,7 +519,7 @@ function evaluateHandleStaleModuleHooks(
519
519
  // Loader: modules
520
520
  const LOADER_PREFIX = 'lwr.loader.';
521
521
  const MODULE_DEFINE = `${LOADER_PREFIX}module.define`;
522
- const MODULE_DYNAMIC_LOAD = `${LOADER_PREFIX}moduleRegistry.dynamicLoad`;
522
+ const MODULE_DYNAMIC_LOAD = `${LOADER_PREFIX}module.dynamicLoad`;
523
523
  const MODULE_FETCH = `${LOADER_PREFIX}module.fetch`;
524
524
  const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
525
525
 
@@ -572,8 +572,6 @@ const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
572
572
 
573
573
 
574
574
 
575
-
576
-
577
575
 
578
576
 
579
577
 
@@ -585,6 +583,10 @@ class ModuleRegistry {
585
583
  this.profiler = config.profiler;
586
584
  }
587
585
 
586
+ clearRegistry() {
587
+ this.moduleRegistry = new Map();
588
+ }
589
+
588
590
  async load(id, importer) {
589
591
  const metadata = importer ? { importer } : {};
590
592
  this.profiler.logOperationStart({
@@ -620,7 +622,7 @@ class ModuleRegistry {
620
622
  // eslint-disable-next-line no-await-in-loop
621
623
  result = isResponseAPromise(response) ? await response : response;
622
624
  }
623
- if (!this.isValidResolveResponse(result)) {
625
+ if (!this.isValidResolveResponse(result )) {
624
626
  throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
625
627
  }
626
628
 
@@ -674,7 +676,7 @@ class ModuleRegistry {
674
676
  resolvedOrPlain,
675
677
  ) ;
676
678
  if (namedDefineRecord.external || namedDefineRecord.defined) {
677
- const record = this.moduleRegistry.get(resolved);
679
+ const record = this.moduleRegistry.get(resolved );
678
680
  if (!record || !this.aliases.has(resolvedOrPlain)) {
679
681
  return resolvedOrPlain;
680
682
  }
@@ -710,7 +712,7 @@ class ModuleRegistry {
710
712
  const mod = this.namedDefineRegistry.get(name);
711
713
  // Don't allow redefining a module.
712
714
  if (mod && mod.defined) {
713
- if (process.env.NODE_ENV !== 'production' && hasConsole) {
715
+ if (process.env.NODE_ENV !== 'production' && process.env.MRT_HMR !== 'true' && hasConsole) {
714
716
  // eslint-disable-next-line lwr/no-unguarded-apis
715
717
  console.warn(`Module redefine attempted: ${name}`);
716
718
  }
@@ -759,7 +761,7 @@ class ModuleRegistry {
759
761
  // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
760
762
  timer = setTimeout(() => {
761
763
  reject(new LoaderError(MODULE_LOAD_TIMEOUT, [id]));
762
- }, MODULE_LOAD_TIMEOUT_TIMER);
764
+ }, MODULE_LOAD_TIMEOUT_TIMER) ;
763
765
  }).finally(() => {
764
766
  // eslint-disable-next-line lwr/no-unguarded-apis, no-undef
765
767
  clearTimeout(timer);
@@ -1464,6 +1466,9 @@ async function evaluateImportMaps(baseUrl) {
1464
1466
  return importMapPromise;
1465
1467
  }
1466
1468
 
1469
+ function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1470
+
1471
+
1467
1472
  /**
1468
1473
  * The LWR loader is inspired and borrows from the algorithms and native browser principles of https://github.com/systemjs/systemjs
1469
1474
  */
@@ -1473,9 +1478,9 @@ class Loader {
1473
1478
 
1474
1479
 
1475
1480
  constructor(config) {
1476
- config = config || {};
1477
- let baseUrl = config.baseUrl;
1478
- let profiler = config.profiler;
1481
+ const loaderConfig = config || {};
1482
+ let baseUrl = loaderConfig.baseUrl;
1483
+ let profiler = loaderConfig.profiler;
1479
1484
  if (baseUrl) {
1480
1485
  // add a trailing slash, if it does not exist
1481
1486
  baseUrl = baseUrl.replace(/\/?$/, '/');
@@ -1506,7 +1511,7 @@ class Loader {
1506
1511
  this.services = Object.freeze({
1507
1512
  addLoaderPlugin: this.registry.addLoaderPlugin.bind(this.registry),
1508
1513
  handleStaleModule: this.registry.registerHandleStaleModuleHook.bind(this.registry),
1509
- appMetadata: config.appMetadata,
1514
+ appMetadata: _optionalChain([config, 'optionalAccess', _ => _.appMetadata]) ,
1510
1515
  });
1511
1516
  }
1512
1517
 
@@ -1556,6 +1561,10 @@ class Loader {
1556
1561
  return this.registry.load(id, importer);
1557
1562
  }
1558
1563
 
1564
+ clearRegistry() {
1565
+ this.registry.clearRegistry();
1566
+ }
1567
+
1559
1568
  /**
1560
1569
  * Checks if a Module exists in the registry. Note, returns false even if the ModuleDefinition exists but the Module has not been instantiated yet (executed).
1561
1570
  *
@@ -0,0 +1,54 @@
1
+ import type { ImportMapResolver } from '../importMap/importMapResolver.js';
2
+ import type { HandleStaleModuleHook, LoaderHooks, ResolveHookResponse } from '@lwrjs/types';
3
+ import type { LoaderConfig } from '../../../../types.js';
4
+ export type ModuleDefinitionSignatures = {
5
+ ownHash: string;
6
+ hashes: {
7
+ [key: string]: string;
8
+ };
9
+ };
10
+ export type Module = {
11
+ [key: string]: any;
12
+ __useDefault?: boolean;
13
+ };
14
+ export declare class ModuleRegistry {
15
+ private profiler;
16
+ constructor(config: LoaderConfig);
17
+ clearRegistry(): void;
18
+ load(id: string, importer?: string): Promise<Module>;
19
+ resolve(id: string, importer?: string): Promise<string>;
20
+ has(id: string): boolean;
21
+ define(name: string, dependencies: string[], exporter: Function, signatures: ModuleDefinitionSignatures): void;
22
+ /**
23
+ * Marks modules as "externally" loaded/provided, so that the loader does not attempt to fetch them.
24
+ *
25
+ * @param modules - list of module identifiers
26
+ */
27
+ registerExternalModules(modules: string[]): void;
28
+ private checkModuleSignature;
29
+ private resolver?;
30
+ setImportResolver(resolver: ImportMapResolver): void;
31
+ private baseUrl;
32
+ private namedDefineRegistry;
33
+ private moduleRegistry;
34
+ aliases: Map<string, string>;
35
+ private lastDefine;
36
+ private getExistingModuleRecord;
37
+ private getModuleRecord;
38
+ private storeModuleAlias;
39
+ private getModuleDependencyRecord;
40
+ private topLevelEvaluation;
41
+ private instantiateAll;
42
+ private evaluateModule;
43
+ private isNamedExportDefaultOnly;
44
+ private getCircularDependencyWrapper;
45
+ private evaluateModuleDependencies;
46
+ private getModuleDef;
47
+ private resolveHook?;
48
+ private loadHook?;
49
+ addLoaderPlugin(hooks: LoaderHooks): void;
50
+ private handleStaleModuleHook?;
51
+ registerHandleStaleModuleHook(handleStaleModule: HandleStaleModuleHook): void;
52
+ isValidResolveResponse(res: ResolveHookResponse): boolean;
53
+ }
54
+ //# sourceMappingURL=moduleRegistry.d.ts.map