@lwrjs/loader 0.12.0-alpha.9 → 0.12.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 Legacy Module Loader v0.12.0-alpha.9 */
7
+ /* LWR Legacy Module Loader v0.12.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.21.0 */
380
+ /*! version: 0.21.4 */
381
381
 
382
382
  /* global console,process */
383
383
 
@@ -572,32 +572,19 @@ const MODULE_ERROR = `${LOADER_PREFIX}module.error`;
572
572
 
573
573
 
574
574
 
575
+
576
+
575
577
 
576
578
 
577
579
 
578
580
  class ModuleRegistry {
579
581
 
580
-
581
-
582
- // A registry for named AMD defines containing the *metadata* of AMD module
583
- __init() {this.namedDefineRegistry = new Map();}
584
- // The evaluated module registry where the module identifier (name or URL?) is the key
585
- __init2() {this.moduleRegistry = new Map();}
586
- // Aliases of modules in the registry
587
- __init3() {this.aliases = new Map();}
588
-
589
-
590
-
591
-
592
582
 
593
583
  constructor(config) {ModuleRegistry.prototype.__init.call(this);ModuleRegistry.prototype.__init2.call(this);ModuleRegistry.prototype.__init3.call(this);
594
584
  this.baseUrl = config.baseUrl || '';
595
585
  this.profiler = config.profiler;
596
586
  }
597
587
 
598
- /**
599
- * Module import
600
- */
601
588
  async load(id, importer) {
602
589
  const metadata = importer ? { importer } : {};
603
590
  this.profiler.logOperationStart({
@@ -606,21 +593,17 @@ class ModuleRegistry {
606
593
  metadata,
607
594
  });
608
595
  const resolvedId = await this.resolve(id, importer);
609
- const moduleRecord = this.getModuleRecord(resolvedId, id);
610
-
596
+ const moduleRecord = await this.getModuleRecord(resolvedId, id);
611
597
  if (moduleRecord.evaluated) {
612
598
  return moduleRecord.module;
613
599
  } else {
614
600
  if (!moduleRecord.evaluationPromise) {
615
- moduleRecord.evaluationPromise = this.evaluateModule(moduleRecord, {});
601
+ moduleRecord.evaluationPromise = this.topLevelEvaluation(moduleRecord);
616
602
  }
617
603
  return moduleRecord.evaluationPromise;
618
604
  }
619
605
  }
620
606
 
621
- /**
622
- * Resolve id for a module
623
- */
624
607
  async resolve(id, importer) {
625
608
  const parentUrl = this.baseUrl; // only support baseUrl for now
626
609
 
@@ -637,7 +620,7 @@ class ModuleRegistry {
637
620
  // eslint-disable-next-line no-await-in-loop
638
621
  result = isResponseAPromise(response) ? await response : response;
639
622
  }
640
- if (!isValidResolveResponse(result)) {
623
+ if (!this.isValidResolveResponse(result)) {
641
624
  throw new LoaderError(INVALID_LOADER_SERVICE_RESPONSE);
642
625
  }
643
626
 
@@ -718,9 +701,6 @@ class ModuleRegistry {
718
701
  return this.moduleRegistry.has(id);
719
702
  }
720
703
 
721
- /**
722
- * Module entry point LWR.define()
723
- */
724
704
  define(
725
705
  name,
726
706
  dependencies,
@@ -835,10 +815,24 @@ class ModuleRegistry {
835
815
  }
836
816
  }
837
817
 
818
+
838
819
  setImportResolver(resolver) {
839
820
  this.resolver = resolver;
840
821
  }
841
822
 
823
+
824
+
825
+ // A registry for named AMD defines containing the *metadata* of AMD module
826
+ __init() {this.namedDefineRegistry = new Map();}
827
+
828
+ // The evaluated module registry where the module identifier (name or URL?) is the key
829
+ __init2() {this.moduleRegistry = new Map();}
830
+
831
+ // Aliases of modules in the registry
832
+ __init3() {this.aliases = new Map();}
833
+
834
+
835
+
842
836
  // Returns an existing module record by the resolvedId or aliased id
843
837
  getExistingModuleRecord(resolvedId, aliasId) {
844
838
  const moduleRecord = this.moduleRegistry.get(resolvedId);
@@ -860,8 +854,7 @@ class ModuleRegistry {
860
854
  return moduleRecord;
861
855
  }
862
856
 
863
- // Module Records Inflight Cache
864
- getModuleRecord(resolvedId, id) {
857
+ async getModuleRecord(resolvedId, id) {
865
858
  // Look for an existing record
866
859
  const existingRecord = this.getExistingModuleRecord(resolvedId, id);
867
860
  if (existingRecord) {
@@ -869,17 +862,37 @@ class ModuleRegistry {
869
862
  return existingRecord;
870
863
  }
871
864
 
865
+ // Create a new Module Record
866
+ const instantiation = this.getModuleDef(resolvedId, id);
867
+ const dependencyRecords = instantiation.then((moduleDef) => {
868
+ const dependencies = (moduleDef && moduleDef.dependencies) || [];
869
+ // get dep and filter out exports
870
+ const filtered = dependencies
871
+ .map((dep) => {
872
+ if (dep === 'exports') {
873
+ return;
874
+ }
875
+ invariant(dep !== 'require', NO_AMD_REQUIRE);
876
+ return this.getModuleDependencyRecord.call(this, dep);
877
+ })
878
+ .filter((depRecord) => depRecord !== undefined) ;
879
+
880
+ return Promise.all(filtered);
881
+ });
882
+
872
883
  const newModuleRecord = {
873
884
  id: resolvedId,
874
885
  module: Object.create(null),
875
- instantiation: this.getModuleDef(resolvedId, id),
886
+ dependencyRecords,
887
+ instantiation,
876
888
  evaluated: false,
877
889
  evaluationPromise: null,
878
890
  };
879
891
  this.moduleRegistry.set(resolvedId, newModuleRecord);
880
892
  this.storeModuleAlias(id, resolvedId);
881
893
 
882
- return newModuleRecord;
894
+ // Wait for the dependencies to resolve the return the moduleRecord
895
+ return dependencyRecords.then(() => newModuleRecord);
883
896
  }
884
897
 
885
898
  storeModuleAlias(aliasId, resolvedId) {
@@ -899,85 +912,96 @@ class ModuleRegistry {
899
912
  }
900
913
  }
901
914
 
902
- /**
903
- * Evaluate all module dependencies
904
- */
905
- async evaluateDependencies(
906
- dependencies,
907
- evaluationMap,
908
- ) {
909
- const exports = {};
910
- const promiseArray = [];
911
- if (dependencies) {
912
- for (const dep of dependencies) {
913
- if (dep === 'exports') {
914
- promiseArray.push(Promise.resolve(exports));
915
- } else {
916
- invariant(dep !== 'require', NO_AMD_REQUIRE);
917
- promiseArray.push(this.evaluateDependent(dep, evaluationMap));
918
- }
919
- }
920
- }
921
-
922
- return Promise.all(promiseArray).then((results) => {
923
- const depsMapped = results.filter((result) => result !== undefined);
924
- return { depsMapped, exports };
925
- });
915
+ async getModuleDependencyRecord(dependency) {
916
+ const resolvedDepId = await this.resolve(dependency);
917
+ return this.getModuleRecord(resolvedDepId, dependency);
926
918
  }
927
919
 
928
- async evaluateDependent(dep, evaluationMap) {
929
- const resolvedDepId = await this.resolve(dep);
930
-
931
- const depModuleRecord = this.getModuleRecord(resolvedDepId, dep);
932
- let module = depModuleRecord.module;
933
- const handleReturn = (module) => {
934
- if (module) {
935
- return module.__defaultInterop ? module.default : module;
936
- }
937
- throw new LoaderError(FAILED_DEP, [resolvedDepId]);
938
- };
920
+ // execute the "top-level code" (the code outside of functions) of a module
921
+ async topLevelEvaluation(moduleRecord) {
922
+ await this.instantiateAll(moduleRecord, {});
923
+ return this.evaluateModule(moduleRecord, {});
924
+ }
939
925
 
940
- // If evaluated return the module
941
- if (depModuleRecord.evaluated) {
942
- return handleReturn(module);
943
- }
944
- /**
945
- * Circular dependencies are handled properly when named exports are used,
946
- * however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
947
- *
948
- * The workaround below applies for circular dependencies (!moduleRecord.evaluated)
949
- */
950
- if (!evaluationMap[depModuleRecord.id]) {
951
- // If we have not started dependency evaluation kick it off
952
- if (!depModuleRecord.evaluationPromise) {
953
- depModuleRecord.evaluationPromise = this.evaluateModule(depModuleRecord, evaluationMap);
926
+ // Returns a promise when a module and all of it's dependencies have finished instantiation
927
+ async instantiateAll(
928
+ moduleRecord,
929
+ instantiatedMap,
930
+ ) {
931
+ if (!instantiatedMap[moduleRecord.id]) {
932
+ instantiatedMap[moduleRecord.id] = true;
933
+ const dependencyModuleRecords = await moduleRecord.dependencyRecords;
934
+ if (dependencyModuleRecords) {
935
+ for (let i = 0; i < dependencyModuleRecords.length; i++) {
936
+ const depRecord = dependencyModuleRecords[i];
937
+ // eslint-disable-next-line no-await-in-loop
938
+ await this.instantiateAll(depRecord, instantiatedMap);
939
+ }
954
940
  }
955
- return depModuleRecord.evaluationPromise.then((module) => {
956
- return handleReturn(module);
957
- });
958
- } else {
959
- // Otherwise return a dummy circular module wrapper
960
- module = getCircularDependencyWrapper(module);
961
941
  }
962
- return handleReturn(module);
963
942
  }
964
943
 
965
944
  async evaluateModule(
966
945
  moduleRecord,
967
946
  evaluationMap,
968
947
  ) {
969
- // Create a evaluationMap to detect cycles in this dep chain
970
- const chainMap = { ...evaluationMap };
971
- chainMap[moduleRecord.id] = true;
948
+ const dependencyModuleRecords = await moduleRecord.dependencyRecords;
949
+ if (dependencyModuleRecords.length > 0) {
950
+ evaluationMap[moduleRecord.id] = true;
951
+ // evaluate dependencies first
952
+ await this.evaluateModuleDependencies(dependencyModuleRecords, evaluationMap);
953
+ }
972
954
 
973
- // Wait for load to finish
974
955
  const { exporter, dependencies } = await moduleRecord.instantiation;
975
-
976
- // Evaluate all it's dependents
977
- const { depsMapped, exports } = await this.evaluateDependencies(dependencies, chainMap);
956
+ // The exports object automatically gets filled in by the exporter evaluation
957
+ const exports = {};
958
+ const depsMapped = dependencies
959
+ ? await Promise.all(
960
+ dependencies.map(async (dep) => {
961
+ if (dep === 'exports') {
962
+ return exports;
963
+ }
964
+ const resolvedDepId = await this.resolve(dep);
965
+
966
+ const moduleRecord = this.moduleRegistry.get(resolvedDepId) ;
967
+ if (!moduleRecord) {
968
+ throw new LoaderError(FAILED_DEP, [resolvedDepId]);
969
+ }
970
+
971
+ const module = moduleRecord.module;
972
+
973
+ /**
974
+ * Circular dependencies are handled properly when named exports are used,
975
+ * however, for default exports there is a bug: https://github.com/rollup/rollup/issues/3384
976
+ *
977
+ * The workaround below applies for circular dependencies (!moduleRecord.evaluated)
978
+ */
979
+ if (!moduleRecord.evaluated) {
980
+ return this.getCircularDependencyWrapper(module);
981
+ }
982
+
983
+ if (module) {
984
+ return module.__defaultInterop ? module.default : module;
985
+ }
986
+
987
+ throw new LoaderError(FAILED_DEP, [resolvedDepId]);
988
+ }),
989
+ )
990
+ : [];
991
+
992
+ // W-10029836 - In the case where we could be instantiating multiple graphs at the same time lets make sure the module have not already been evaluated
993
+ if (moduleRecord.evaluated) {
994
+ return moduleRecord.module;
995
+ }
978
996
 
979
997
  // evaluates the module function
980
- let moduleDefault = this.evaluateModuleCode(exporter, depsMapped, moduleRecord);
998
+ let moduleDefault;
999
+ try {
1000
+ moduleDefault = exporter(...depsMapped);
1001
+ } catch (e) {
1002
+ throw new LoaderError(EXPORTER_ERROR, [moduleRecord.id, e.message || e]);
1003
+ }
1004
+
981
1005
  // value is returned from exporter, then we are not using named exports
982
1006
  if (moduleDefault !== undefined) {
983
1007
  moduleDefault = { default: moduleDefault };
@@ -989,7 +1013,7 @@ class ModuleRegistry {
989
1013
  // if no return value, then we are using the exports object
990
1014
  else {
991
1015
  // handle only default export with Rollup forced named exports
992
- if (isNamedExportDefaultOnly(exports)) {
1016
+ if (this.isNamedExportDefaultOnly(exports)) {
993
1017
  Object.defineProperty(exports, '__useDefault', { value: true });
994
1018
  }
995
1019
  }
@@ -1021,17 +1045,42 @@ class ModuleRegistry {
1021
1045
  Object.defineProperty(moduleRecord.module, '__esModule', { value: true });
1022
1046
  }
1023
1047
 
1024
- Object.freeze(moduleRecord.module);
1025
1048
  moduleRecord.evaluated = true;
1026
- moduleRecord.evaluationPromise = null;
1049
+ Object.freeze(moduleRecord.module);
1027
1050
  return moduleRecord.module;
1028
1051
  }
1029
1052
 
1030
- evaluateModuleCode(evaluatedExporter, depsMapped, moduleRecord) {
1031
- try {
1032
- return evaluatedExporter(...depsMapped);
1033
- } catch (e) {
1034
- throw new LoaderError(EXPORTER_ERROR, [moduleRecord.id, e.message || e]);
1053
+ // Determines if named exports module has only default export
1054
+ isNamedExportDefaultOnly(exports) {
1055
+ return (
1056
+ exports !== undefined &&
1057
+ Object.getOwnPropertyNames(exports).length === 2 &&
1058
+ Object.prototype.hasOwnProperty.call(exports, 'default') &&
1059
+ Object.prototype.hasOwnProperty.call(exports, '__esModule')
1060
+ );
1061
+ }
1062
+
1063
+ // Wrap the dependency in a function that can be called and detected by __circular__ property.
1064
+ // The LWC engine checks for __circular__ to detect circular dependencies.
1065
+ getCircularDependencyWrapper(module) {
1066
+ const tmp = () => {
1067
+ return module.__useDefault || module.__defaultInterop ? module.default : module;
1068
+ };
1069
+ tmp.__circular__ = true;
1070
+ return tmp;
1071
+ }
1072
+
1073
+ async evaluateModuleDependencies(
1074
+ dependencyModuleRecords,
1075
+ evaluationMap,
1076
+ ) {
1077
+ for (let i = 0; i < dependencyModuleRecords.length; i++) {
1078
+ const depRecord = dependencyModuleRecords[i];
1079
+ if (!depRecord.evaluated && !evaluationMap[depRecord.id]) {
1080
+ evaluationMap[depRecord.id] = true;
1081
+ // eslint-disable-next-line no-await-in-loop
1082
+ await this.evaluateModule(depRecord, evaluationMap);
1083
+ }
1035
1084
  }
1036
1085
  }
1037
1086
 
@@ -1043,8 +1092,8 @@ class ModuleRegistry {
1043
1092
  const moduleName = !isUrl(resolvedId)
1044
1093
  ? resolvedId
1045
1094
  : originalId !== resolvedId
1046
- ? originalId
1047
- : undefined;
1095
+ ? originalId
1096
+ : undefined;
1048
1097
  let moduleDef = moduleName && this.namedDefineRegistry.get(moduleName);
1049
1098
  if (moduleDef && moduleDef.external) {
1050
1099
  return moduleDef.external.moduleDefPromise;
@@ -1111,6 +1160,8 @@ class ModuleRegistry {
1111
1160
  });
1112
1161
  }
1113
1162
 
1163
+
1164
+
1114
1165
  addLoaderPlugin(hooks) {
1115
1166
  if (typeof hooks !== 'object') {
1116
1167
  throw new LoaderError(INVALID_HOOK);
@@ -1133,6 +1184,7 @@ class ModuleRegistry {
1133
1184
  }
1134
1185
  }
1135
1186
 
1187
+
1136
1188
  registerHandleStaleModuleHook(handleStaleModule) {
1137
1189
  if (this.handleStaleModuleHook) {
1138
1190
  this.handleStaleModuleHook.push(handleStaleModule);
@@ -1140,30 +1192,12 @@ class ModuleRegistry {
1140
1192
  this.handleStaleModuleHook = [handleStaleModule];
1141
1193
  }
1142
1194
  }
1143
- }
1144
-
1145
- function isValidResolveResponse(res) {
1146
- return res === null || typeof res === 'string' || (res && typeof (res ).url === 'string');
1147
- }
1148
-
1149
- // Determines if named exports module has only default export
1150
- function isNamedExportDefaultOnly(exports) {
1151
- return (
1152
- exports !== undefined &&
1153
- Object.getOwnPropertyNames(exports).length === 2 &&
1154
- Object.prototype.hasOwnProperty.call(exports, 'default') &&
1155
- Object.prototype.hasOwnProperty.call(exports, '__esModule')
1156
- );
1157
- }
1158
1195
 
1159
- // Wrap the dependency in a function that can be called and detected by __circular__ property.
1160
- // The LWC engine checks for __circular__ to detect circular dependencies.
1161
- function getCircularDependencyWrapper(module) {
1162
- const tmp = () => {
1163
- return module.__useDefault || module.__defaultInterop ? module.default : module;
1164
- };
1165
- tmp.__circular__ = true;
1166
- return tmp;
1196
+ isValidResolveResponse(res) {
1197
+ return (
1198
+ res === null || typeof res === 'string' || (res && typeof (res ).url === 'string')
1199
+ );
1200
+ }
1167
1201
  }
1168
1202
 
1169
1203
  // find the longest set of segments from path which are a key in matchObj
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
- "version": "0.12.0-alpha.9",
8
+ "version": "0.12.0",
9
9
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
10
10
  "repository": {
11
11
  "type": "git",
@@ -61,17 +61,17 @@
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
- "@locker/trusted-types": "0.21.0",
65
- "@lwrjs/diagnostics": "0.12.0-alpha.9",
66
- "@lwrjs/types": "0.12.0-alpha.9",
64
+ "@locker/trusted-types": "0.21.4",
65
+ "@lwrjs/diagnostics": "0.12.0",
66
+ "@lwrjs/types": "0.12.0",
67
67
  "@rollup/plugin-node-resolve": "^15.2.3",
68
68
  "@rollup/plugin-sucrase": "^5.0.2",
69
69
  "@rollup/plugin-terser": "^0.4.4",
70
70
  "rollup": "^2.78.0"
71
71
  },
72
72
  "dependencies": {
73
- "@lwrjs/client-modules": "0.12.0-alpha.9",
74
- "@lwrjs/shared-utils": "0.12.0-alpha.9"
73
+ "@lwrjs/client-modules": "0.12.0",
74
+ "@lwrjs/shared-utils": "0.12.0"
75
75
  },
76
76
  "lwc": {
77
77
  "modules": [
@@ -91,5 +91,5 @@
91
91
  "volta": {
92
92
  "extends": "../../../package.json"
93
93
  },
94
- "gitHead": "58b28fbc6300b704ac17f8878423120de0e376d7"
94
+ "gitHead": "90f93604b26003e1e1eb85bb0d1f34f4fc9e9ff9"
95
95
  }