@lwrjs/shared-utils 0.4.6 → 0.5.2

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.
@@ -0,0 +1,17 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {get: all[name], enumerable: true});
6
+ };
7
+
8
+ // packages/@lwrjs/shared-utils/src/env.ts
9
+ __markAsModule(exports);
10
+ __export(exports, {
11
+ getExperimentalFeatures: () => getExperimentalFeatures
12
+ });
13
+ function getExperimentalFeatures() {
14
+ return {
15
+ ENABLE_FINGERPRINTS: process.env.ENABLE_FINGERPRINTS !== void 0 && process.env.ENABLE_FINGERPRINTS === "true" ? true : false
16
+ };
17
+ }
@@ -30,3 +30,4 @@ __exportStar(exports, __toModule(require("./import-metadata.cjs")));
30
30
  __exportStar(exports, __toModule(require("./graph.cjs")));
31
31
  __exportStar(exports, __toModule(require("./mappings.cjs")));
32
32
  __exportStar(exports, __toModule(require("./metrics.cjs")));
33
+ __exportStar(exports, __toModule(require("./env.cjs")));
@@ -31,23 +31,21 @@ var import_graph = __toModule(require("./graph.cjs"));
31
31
  var import_identity = __toModule(require("./identity.cjs"));
32
32
  async function getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler) {
33
33
  const visitedCache = new Map();
34
- const importMetadata = {
35
- imports: {}
34
+ let importMetadata = {
35
+ imports: {},
36
+ index: {}
36
37
  };
37
38
  for (const moduleId of moduleIds) {
38
39
  const specifier = moduleId.specifier;
39
40
  if (!visitedCache.has(specifier)) {
40
41
  const depth = {static: import_graph.GraphDepth.ALL, dynamic: 0};
41
42
  const moduleGraph = await (0, import_graph.getModuleGraphs)(moduleId.specifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, runtimeEnvironment.bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
42
- Object.assign(importMetadata.imports, toImportMetadata(moduleGraph).imports);
43
+ importMetadata = await toImportMetadata(moduleGraph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
43
44
  }
44
45
  }
45
46
  return importMetadata;
46
47
  }
47
- function toImportMetadata(moduleGraph, existingImportMetadata) {
48
- const importMetadata = {
49
- imports: {}
50
- };
48
+ async function toImportMetadata(moduleGraph, existingImportMetadata = {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams = {}) {
51
49
  const specifier = moduleGraph.graphs[0].specifier;
52
50
  const uri = moduleGraph.uriMap[specifier];
53
51
  const definition = moduleGraph.linkedDefinitions[specifier];
@@ -57,9 +55,8 @@ function toImportMetadata(moduleGraph, existingImportMetadata) {
57
55
  if (!definition) {
58
56
  throw new Error("Linked module definition was not included in the graph: " + specifier);
59
57
  }
60
- if (!importMetadata.imports[uri]) {
61
- importMetadata.imports[uri] = normalizedIncludedModules(definition);
62
- }
58
+ const rootMetadata = await normalizeImportMetadata(specifier, uri, definition, moduleRegistry, runtimeEnvironment, runtimeParams);
59
+ let importMetadata = mergeImportMetadata(existingImportMetadata, rootMetadata);
63
60
  const depSpecifiers = moduleGraph.graphs[0].static;
64
61
  for (const depSpecifier of depSpecifiers) {
65
62
  const depUri = moduleGraph.uriMap[depSpecifier];
@@ -67,18 +64,58 @@ function toImportMetadata(moduleGraph, existingImportMetadata) {
67
64
  if (!depUri) {
68
65
  throw new Error("URI was not included in the graph for dependent: " + depSpecifier);
69
66
  }
70
- if (!depSpecifier) {
67
+ if (!depDef) {
71
68
  throw new Error("Linked dependent module definition was not included in the graph: " + depSpecifier);
72
69
  }
73
70
  if (!importMetadata.imports[depUri]) {
74
- importMetadata.imports[depUri] = normalizedIncludedModules(depDef);
71
+ const depMetadata = await normalizeImportMetadata((0, import_identity.getSpecifier)(depDef), depUri, depDef, moduleRegistry, runtimeEnvironment, runtimeParams);
72
+ importMetadata = mergeImportMetadata(importMetadata, depMetadata);
75
73
  }
76
74
  }
77
- if (existingImportMetadata?.imports) {
78
- importMetadata.imports = Object.assign(importMetadata.imports, existingImportMetadata.imports);
79
- }
80
75
  return importMetadata;
81
76
  }
77
+ function mergeImportMetadata(existing, newMetadata) {
78
+ return {
79
+ imports: {
80
+ ...existing.imports,
81
+ ...newMetadata.imports
82
+ },
83
+ index: {
84
+ ...existing.index,
85
+ ...newMetadata.index
86
+ }
87
+ };
88
+ }
89
+ async function normalizeImportMetadata(specifier, uri, definition, moduleRegistry, moduleRuntimeEnvironment, runtimeParams) {
90
+ const specifiers = normalizedIncludedModules(definition);
91
+ const imports = {};
92
+ imports[uri] = specifiers;
93
+ const index = await createIndex(specifiers, moduleRegistry, moduleRuntimeEnvironment, runtimeParams);
94
+ if (!(specifier in index)) {
95
+ index[specifier] = uri;
96
+ }
97
+ return {
98
+ imports,
99
+ index
100
+ };
101
+ }
82
102
  function normalizedIncludedModules(definition) {
83
103
  return (0, import_identity.isBundleDefinition)(definition) ? [(0, import_identity.getSpecifier)(definition), ...definition.bundleRecord.includedModules] : [(0, import_identity.getSpecifier)(definition)];
84
104
  }
105
+ async function createIndex(specifiers, moduleRegistry, runtimeEnvironment, runtimeParams) {
106
+ const index = {};
107
+ const moduleRuntimeEnvironment = {...runtimeEnvironment, bundle: false};
108
+ async function getUri(specifier) {
109
+ const moduleId = (0, import_identity.explodeSpecifier)(specifier);
110
+ if (!moduleId.version) {
111
+ throw new Error("Module specifier must include a version: " + specifier);
112
+ }
113
+ index[specifier] = await moduleRegistry.resolveModuleUri({...moduleId, version: moduleId.version}, moduleRuntimeEnvironment, runtimeParams);
114
+ }
115
+ const promises = [];
116
+ for (const specifier of specifiers) {
117
+ promises.push(getUri(specifier));
118
+ }
119
+ await Promise.all(promises);
120
+ return index;
121
+ }
@@ -0,0 +1,3 @@
1
+ import { ExperimentalFeatures } from '@lwrjs/types';
2
+ export declare function getExperimentalFeatures(): ExperimentalFeatures;
3
+ //# sourceMappingURL=env.d.ts.map
@@ -0,0 +1,9 @@
1
+ export function getExperimentalFeatures() {
2
+ return {
3
+ // DEFAULT ENABLE_FINGERPRINTS = false;
4
+ ENABLE_FINGERPRINTS: process.env.ENABLE_FINGERPRINTS !== undefined && process.env.ENABLE_FINGERPRINTS === 'true'
5
+ ? true
6
+ : false,
7
+ };
8
+ }
9
+ //# sourceMappingURL=env.js.map
package/build/es/graph.js CHANGED
@@ -49,12 +49,16 @@ defRegistry, runtimeEnvironment, runtimeParams) {
49
49
  }
50
50
  if (depth.static === GraphDepth.ALL) {
51
51
  if (visited.has(impSpecifier)) {
52
+ // eslint-disable-next-line no-await-in-loop
52
53
  await flatten(impSpecifier, flattened, graphIdx, visited);
53
54
  }
54
55
  else {
55
56
  const moduleDef = isBundler(defRegistry)
56
- ? await defRegistry.getModuleBundle(imp, runtimeEnvironment, runtimeParams)
57
- : await defRegistry.getModule(imp, runtimeParams);
57
+ ? // eslint-disable-next-line no-await-in-loop
58
+ await defRegistry.getModuleBundle(imp, runtimeEnvironment, runtimeParams)
59
+ : // eslint-disable-next-line no-await-in-loop
60
+ await defRegistry.getModule(imp, runtimeParams);
61
+ // eslint-disable-next-line no-await-in-loop
58
62
  await traverse(moduleDef, depth, flattened, graphIdx, visited, defRegistry, runtimeEnvironment, runtimeParams);
59
63
  }
60
64
  }
@@ -71,12 +75,16 @@ defRegistry, runtimeEnvironment, runtimeParams) {
71
75
  }
72
76
  if (visited.has(impSpecifier)) {
73
77
  // flatten already known sub graphs
78
+ // eslint-disable-next-line no-await-in-loop
74
79
  await flatten(impSpecifier, flattened, graphIdx + 1, visited);
75
80
  }
76
81
  else {
77
82
  const moduleDef = isBundler(defRegistry)
78
- ? await defRegistry.getModuleBundle(imp, runtimeEnvironment, runtimeParams)
79
- : await defRegistry.getModule(imp, runtimeParams);
83
+ ? // eslint-disable-next-line no-await-in-loop
84
+ await defRegistry.getModuleBundle(imp, runtimeEnvironment, runtimeParams)
85
+ : // eslint-disable-next-line no-await-in-loop
86
+ await defRegistry.getModule(imp, runtimeParams);
87
+ // eslint-disable-next-line no-await-in-loop
80
88
  await traverse(moduleDef, depth, flattened, graphIdx + 1, visited, defRegistry, runtimeEnvironment, runtimeParams);
81
89
  }
82
90
  }
@@ -115,7 +123,9 @@ options, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visited
115
123
  const uriMap = {};
116
124
  if (options.includeUris) {
117
125
  for (const visitedSpecifier of acc.keys()) {
126
+ // eslint-disable-next-line no-await-in-loop
118
127
  const moduleId = await getVersionedModuleId(visitedSpecifier, moduleRegistry);
128
+ // eslint-disable-next-line no-await-in-loop
119
129
  const uri = await moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams);
120
130
  uriMap[visitedSpecifier] = uri;
121
131
  }
@@ -123,10 +133,13 @@ options, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visited
123
133
  const linkedDefinitions = {};
124
134
  if (options.includeLinkedDefinitions) {
125
135
  for (const visitedSpecifier of acc.keys()) {
136
+ // eslint-disable-next-line no-await-in-loop
126
137
  const versionedModuleId = await getVersionedModuleId(visitedSpecifier, moduleRegistry);
127
138
  const module = isBundler(defRegistry)
128
- ? await defRegistry.getModuleBundle(versionedModuleId, runtimeEnvironment, runtimeParams)
129
- : await defRegistry.getLinkedModule(versionedModuleId, runtimeEnvironment, runtimeParams);
139
+ ? // eslint-disable-next-line no-await-in-loop
140
+ await defRegistry.getModuleBundle(versionedModuleId, runtimeEnvironment, runtimeParams)
141
+ : // eslint-disable-next-line no-await-in-loop
142
+ await defRegistry.getLinkedModule(versionedModuleId, runtimeEnvironment, runtimeParams);
130
143
  linkedDefinitions[visitedSpecifier] = module;
131
144
  }
132
145
  }
@@ -9,4 +9,5 @@ export * from './import-metadata.js';
9
9
  export * from './graph.js';
10
10
  export * from './mappings.js';
11
11
  export * from './metrics.js';
12
+ export * from './env.js';
12
13
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -9,4 +9,5 @@ export * from './import-metadata.js';
9
9
  export * from './graph.js';
10
10
  export * from './mappings.js';
11
11
  export * from './metrics.js';
12
+ export * from './env.js';
12
13
  //# sourceMappingURL=index.js.map
@@ -11,9 +11,9 @@ export declare function getImportMetadataMappings(moduleIds: AbstractModuleId[],
11
11
  * You can pass in an existing set of import metadata and the result will merge the imports.
12
12
  * If there is a duplicate uri the merge will keep the one from the existing metadata.
13
13
  *
14
- * @param moduleGraph FlattenedModuleGraphs created by the grpah shared util
14
+ * @param moduleGraph FlattenedModuleGraphs created by the graph shared util
15
15
  * @param existingImportMetadata Optional existing ImportMetadata. If provided the results will be a merge the two sets of URI mappings.
16
- * @returns Retruns ImportMetadata from a module graph in the format here -> https://rfcs.lwc.dev/rfcs/lws/0000-mapping-api#uri-mapping-resource-specification
16
+ * @returns Returns ImportMetadata from a module graph in the format here -> https://rfcs.lwc.dev/rfcs/lws/0000-mapping-api#uri-mapping-resource-specification
17
17
  */
18
- export declare function toImportMetadata(moduleGraph: FlattenedModuleGraphs, existingImportMetadata?: ImportMetadata): ImportMetadata;
18
+ export declare function toImportMetadata(moduleGraph: FlattenedModuleGraphs, existingImportMetadata: ImportMetadata | undefined, moduleRegistry: ModuleRegistry, runtimeEnvironment: RuntimeEnvironment, runtimeParams?: RuntimeParams): Promise<ImportMetadata>;
19
19
  //# sourceMappingURL=mappings.d.ts.map
@@ -1,12 +1,13 @@
1
1
  import { getModuleGraphs, GraphDepth } from './graph.js';
2
- import { getSpecifier, isBundleDefinition } from './identity.js';
2
+ import { explodeSpecifier, getSpecifier, isBundleDefinition } from './identity.js';
3
3
  /**
4
4
  * Get the Import Metadata for the LWR Mapping Api (https://rfcs.lwc.dev/rfcs/lws/0000-mapping-api)
5
5
  */
6
6
  export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler) {
7
7
  const visitedCache = new Map();
8
- const importMetadata = {
8
+ let importMetadata = {
9
9
  imports: {},
10
+ index: {},
10
11
  };
11
12
  for (const moduleId of moduleIds) {
12
13
  const specifier = moduleId.specifier;
@@ -14,11 +15,13 @@ export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, r
14
15
  if (!visitedCache.has(specifier)) {
15
16
  // Traversal of the Module Graph is done to get all the URLS for discoverable static dependencies.
16
17
  const depth = { static: GraphDepth.ALL, dynamic: 0 };
18
+ // eslint-disable-next-line no-await-in-loop
17
19
  const moduleGraph = await getModuleGraphs(moduleId.specifier,
18
20
  // include uris and linked definitions
19
21
  { includeUris: true, includeLinkedDefinitions: true, depth }, moduleRegistry, runtimeEnvironment.bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
20
22
  // Root module
21
- Object.assign(importMetadata.imports, toImportMetadata(moduleGraph).imports);
23
+ // eslint-disable-next-line no-await-in-loop
24
+ importMetadata = await toImportMetadata(moduleGraph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
22
25
  }
23
26
  }
24
27
  return importMetadata;
@@ -31,14 +34,12 @@ export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, r
31
34
  * You can pass in an existing set of import metadata and the result will merge the imports.
32
35
  * If there is a duplicate uri the merge will keep the one from the existing metadata.
33
36
  *
34
- * @param moduleGraph FlattenedModuleGraphs created by the grpah shared util
37
+ * @param moduleGraph FlattenedModuleGraphs created by the graph shared util
35
38
  * @param existingImportMetadata Optional existing ImportMetadata. If provided the results will be a merge the two sets of URI mappings.
36
- * @returns Retruns ImportMetadata from a module graph in the format here -> https://rfcs.lwc.dev/rfcs/lws/0000-mapping-api#uri-mapping-resource-specification
39
+ * @returns Returns ImportMetadata from a module graph in the format here -> https://rfcs.lwc.dev/rfcs/lws/0000-mapping-api#uri-mapping-resource-specification
37
40
  */
38
- export function toImportMetadata(moduleGraph, existingImportMetadata) {
39
- const importMetadata = {
40
- imports: {},
41
- };
41
+ export async function toImportMetadata(moduleGraph, existingImportMetadata = { imports: {}, index: {} }, moduleRegistry, runtimeEnvironment, runtimeParams = {}) {
42
+ // root module specifier
42
43
  const specifier = moduleGraph.graphs[0].specifier;
43
44
  const uri = moduleGraph.uriMap[specifier];
44
45
  const definition = moduleGraph.linkedDefinitions[specifier];
@@ -48,9 +49,9 @@ export function toImportMetadata(moduleGraph, existingImportMetadata) {
48
49
  if (!definition) {
49
50
  throw new Error('Linked module definition was not included in the graph: ' + specifier);
50
51
  }
51
- if (!importMetadata.imports[uri]) {
52
- importMetadata.imports[uri] = normalizedIncludedModules(definition);
53
- }
52
+ // Merge in the existing metadata with imports for the root specifier
53
+ const rootMetadata = await normalizeImportMetadata(specifier, uri, definition, moduleRegistry, runtimeEnvironment, runtimeParams);
54
+ let importMetadata = mergeImportMetadata(existingImportMetadata, rootMetadata);
54
55
  // root module dependencies
55
56
  const depSpecifiers = moduleGraph.graphs[0].static;
56
57
  for (const depSpecifier of depSpecifiers) {
@@ -59,22 +60,70 @@ export function toImportMetadata(moduleGraph, existingImportMetadata) {
59
60
  if (!depUri) {
60
61
  throw new Error('URI was not included in the graph for dependent: ' + depSpecifier);
61
62
  }
62
- if (!depSpecifier) {
63
+ if (!depDef) {
63
64
  throw new Error('Linked dependent module definition was not included in the graph: ' + depSpecifier);
64
65
  }
65
66
  if (!importMetadata.imports[depUri]) {
66
- importMetadata.imports[depUri] = normalizedIncludedModules(depDef);
67
+ // eslint-disable-next-line no-await-in-loop
68
+ const depMetadata = await normalizeImportMetadata(getSpecifier(depDef), depUri, depDef, moduleRegistry, runtimeEnvironment, runtimeParams);
69
+ importMetadata = mergeImportMetadata(importMetadata, depMetadata);
67
70
  }
68
71
  }
69
- if (existingImportMetadata?.imports) {
70
- importMetadata.imports = Object.assign(importMetadata.imports, existingImportMetadata.imports);
71
- }
72
72
  return importMetadata;
73
73
  }
74
+ function mergeImportMetadata(existing, newMetadata) {
75
+ // TODO should there be an error if the metadata conflicts?
76
+ return {
77
+ imports: {
78
+ ...existing.imports,
79
+ ...newMetadata.imports,
80
+ },
81
+ index: {
82
+ ...existing.index,
83
+ ...newMetadata.index,
84
+ },
85
+ };
86
+ }
87
+ async function normalizeImportMetadata(specifier, uri, definition, moduleRegistry, moduleRuntimeEnvironment, runtimeParams) {
88
+ const specifiers = normalizedIncludedModules(definition);
89
+ const imports = {};
90
+ imports[uri] = specifiers;
91
+ // The index info for the modules included in a bundle
92
+ const index = await createIndex(specifiers, moduleRegistry, moduleRuntimeEnvironment, runtimeParams);
93
+ // If root specifier was not included in createIndex add an entry where the uri is the index
94
+ if (!(specifier in index)) {
95
+ index[specifier] = uri;
96
+ }
97
+ // return imports and index objects created above
98
+ return {
99
+ imports,
100
+ index,
101
+ };
102
+ }
74
103
  // Normalized the included modules associated with either an individual module definition or Bundle definition
75
104
  function normalizedIncludedModules(definition) {
76
105
  return isBundleDefinition(definition)
77
106
  ? [getSpecifier(definition), ...definition.bundleRecord.includedModules]
78
107
  : [getSpecifier(definition)];
79
108
  }
109
+ async function createIndex(specifiers, moduleRegistry, runtimeEnvironment, runtimeParams) {
110
+ const index = {};
111
+ // We need a runtime environment for modules when building the index for a bundle
112
+ const moduleRuntimeEnvironment = { ...runtimeEnvironment, bundle: false };
113
+ async function getUri(specifier) {
114
+ const moduleId = explodeSpecifier(specifier);
115
+ if (!moduleId.version) {
116
+ throw new Error('Module specifier must include a version: ' + specifier);
117
+ }
118
+ index[specifier] = await moduleRegistry.resolveModuleUri({ ...moduleId, version: moduleId.version }, moduleRuntimeEnvironment, runtimeParams);
119
+ }
120
+ // Queue up uri requests
121
+ const promises = [];
122
+ for (const specifier of specifiers) {
123
+ promises.push(getUri(specifier));
124
+ }
125
+ // Wait for them to finish
126
+ await Promise.all(promises);
127
+ return index;
128
+ }
80
129
  //# sourceMappingURL=mappings.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.4.6",
7
+ "version": "0.5.2",
8
8
  "homepage": "https://lwr.dev/",
9
9
  "repository": {
10
10
  "type": "git",
@@ -41,11 +41,11 @@
41
41
  "slugify": "^1.4.5"
42
42
  },
43
43
  "devDependencies": {
44
- "@lwrjs/diagnostics": "0.4.6",
45
- "@lwrjs/types": "0.4.6"
44
+ "@lwrjs/diagnostics": "0.5.2",
45
+ "@lwrjs/types": "0.5.2"
46
46
  },
47
47
  "engines": {
48
48
  "node": ">=14.15.4 <15"
49
49
  },
50
- "gitHead": "71f599b20c35d612e55312e94804a97e7cc778e1"
50
+ "gitHead": "3186b126bf8bf95f88a31b2ea00768e8f42e362e"
51
51
  }