@lwrjs/shared-utils 0.7.0-alpha.1 → 0.7.0-alpha.12

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.
@@ -28,6 +28,7 @@ __export(exports, {
28
28
  getModuleGraphs: () => getModuleGraphs
29
29
  });
30
30
  var import_identity = __toModule(require("./identity.cjs"));
31
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
32
  var import_import_metadata = __toModule(require("./import-metadata.cjs"));
32
33
  var GraphDepth;
33
34
  (function(GraphDepth2) {
@@ -110,7 +111,9 @@ async function getModuleGraphs(specifier, options, moduleRegistry, defRegistry,
110
111
  };
111
112
  const moduleDef = isBundler(defRegistry) ? await defRegistry.getModuleBundle(versionedModuleId, runtimeEnvironment, runtimeParams) : await defRegistry.getModule(versionedModuleId, runtimeParams);
112
113
  if (!moduleDef) {
113
- throw new Error("Missing module Definition");
114
+ throw (0, import_diagnostics.createSingleDiagnosticError)({
115
+ description: import_diagnostics.descriptions.UNRESOLVABLE.MODULE_ENTRY(versionedModuleId.specifier)
116
+ }, import_diagnostics.LwrUnresolvableError);
114
117
  }
115
118
  const flattened = [];
116
119
  await traverse(moduleDef, depth, flattened, 0, acc, defRegistry, runtimeEnvironment, runtimeParams);
@@ -26,6 +26,7 @@ __markAsModule(exports);
26
26
  __export(exports, {
27
27
  extractMetadataFromHtml: () => extractMetadataFromHtml
28
28
  });
29
+ var import_identity = __toModule(require("./identity.cjs"));
29
30
  var import_parse5_sax_parser = __toModule(require("parse5-sax-parser"));
30
31
  var import_stream = __toModule(require("stream"));
31
32
  function parseAssetLocation(htmlSource, tagName, attrLocation) {
@@ -52,11 +53,19 @@ async function extractMetadataFromHtml(htmlSource) {
52
53
  const ceRefStack = [];
53
54
  parser.on("startTag", ({
54
55
  tagName,
56
+ attrs,
55
57
  sourceCodeLocation
56
58
  }) => {
57
59
  if (tagName.includes("-") && !openElements.has(tagName)) {
58
60
  const {startOffset, endOffset} = sourceCodeLocation;
59
- const ceRef = {tagName, location: {startOffset, endOffset}};
61
+ const ceRef = {
62
+ tagName,
63
+ location: {startOffset, endOffset},
64
+ props: attrs.length ? attrs.reduce((obj, {name, value}) => {
65
+ obj[(0, import_identity.getPropFromAttrName)(name)] = value === "" ? "true" : value;
66
+ return obj;
67
+ }, {}) : void 0
68
+ };
60
69
  openElements.add(tagName);
61
70
  if (ceRefStack.length) {
62
71
  const last = ceRefStack[ceRefStack.length - 1];
@@ -43,13 +43,14 @@ __export(exports, {
43
43
  getMappingUriPrefix: () => getMappingUriPrefix,
44
44
  getModuleIdentity: () => getModuleIdentity,
45
45
  getModuleUriPrefix: () => getModuleUriPrefix,
46
+ getPropFromAttrName: () => getPropFromAttrName,
46
47
  getResourceIdentity: () => getResourceIdentity,
47
48
  getSpecifier: () => getSpecifier,
48
49
  getVersionedModuleId: () => getVersionedModuleId,
49
50
  getVersionedSpecifier: () => getVersionedSpecifier,
50
51
  isBundleDefinition: () => isBundleDefinition,
51
52
  isExternalUrl: () => isExternalUrl,
52
- kebabcaseToCamelcase: () => kebabcaseToCamelcase,
53
+ kebabCaseToModuleSpecifer: () => kebabCaseToModuleSpecifer,
53
54
  moduleSpecifierToKebabCase: () => moduleSpecifierToKebabCase,
54
55
  normalizeVersionFromUri: () => normalizeVersionFromUri,
55
56
  normalizeVersionToUri: () => normalizeVersionToUri,
@@ -159,7 +160,7 @@ function parsePackageSpecifier(specifier) {
159
160
  subResource: matched[2] || ""
160
161
  };
161
162
  }
162
- function kebabcaseToCamelcase(name) {
163
+ function kebabCaseToModuleSpecifer(name) {
163
164
  const newName = [];
164
165
  let nsFound = false;
165
166
  let upper = false;
@@ -186,6 +187,9 @@ function slugify(name) {
186
187
  lower: true
187
188
  });
188
189
  }
190
+ function getPropFromAttrName(propName) {
191
+ return propName.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
192
+ }
189
193
  function getModuleUriPrefix({apiVersion, bundle, format, compat, basePath}, {locale, environment} = {}) {
190
194
  const localePart = locale ? `/${LOCALE_SIGIL}/${locale}` : "";
191
195
  const environmentPart = environment ? `/${ENVIRONMENT_SIGIL}/${environment}` : "";
@@ -218,7 +222,10 @@ function getModuleIdentity(req) {
218
222
  }
219
223
  function getMappingIdentity(req) {
220
224
  const {specifiers} = req.params;
221
- const moduleIds = explodeSpecifiers(specifiers);
225
+ const moduleIds = explodeSpecifiers(specifiers).map((obj) => ({
226
+ ...obj,
227
+ importer: req.query.importer
228
+ }));
222
229
  return {
223
230
  moduleIds
224
231
  };
@@ -33,3 +33,4 @@ __exportStar(exports, __toModule(require("./graph.cjs")));
33
33
  __exportStar(exports, __toModule(require("./mappings.cjs")));
34
34
  __exportStar(exports, __toModule(require("./urls.cjs")));
35
35
  __exportStar(exports, __toModule(require("./env.cjs")));
36
+ __exportStar(exports, __toModule(require("./logger.cjs")));
@@ -0,0 +1,36 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/shared-utils/src/logger.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ logger: () => logger
28
+ });
29
+ var import_winston = __toModule(require("winston"));
30
+ var LOG_LEVEL = process.env.LOG_LEVEL || "info";
31
+ console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
32
+ var logger = import_winston.default.createLogger({
33
+ level: LOG_LEVEL,
34
+ format: import_winston.default.format.json(),
35
+ transports: [new import_winston.default.transports.Console()]
36
+ });
@@ -36,7 +36,8 @@ async function getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeP
36
36
  index: {}
37
37
  };
38
38
  for (const moduleId of moduleIds) {
39
- const specifier = (0, import_identity.getSpecifier)(moduleId);
39
+ const requestedSpecifier = (0, import_identity.getSpecifier)(moduleId);
40
+ const specifier = await getVersionedSpecifier(moduleId, moduleRegistry);
40
41
  if (!visitedCache.has(specifier)) {
41
42
  const depth = {
42
43
  static: runtimeEnvironment.format === "esm" ? import_graph.GraphDepth.NONE : import_graph.GraphDepth.ALL,
@@ -45,6 +46,14 @@ async function getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeP
45
46
  const moduleGraph = await (0, import_graph.getModuleGraphs)(specifier, {includeUris: true, includeLinkedDefinitions: true, depth}, moduleRegistry, runtimeEnvironment.bundle ? moduleBundler : moduleRegistry, runtimeEnvironment, runtimeParams, visitedCache);
46
47
  importMetadata = await toImportMetadata(moduleGraph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
47
48
  }
49
+ if (requestedSpecifier !== specifier) {
50
+ const requestedSpecifierPlusImporter = `${requestedSpecifier}?importer=${moduleId.importer}`;
51
+ const specifiersArray = Object.values(importMetadata.imports).find((a) => a.includes(specifier));
52
+ if (!specifiersArray) {
53
+ throw new Error("Could not find: " + specifier + " in " + JSON.stringify(importMetadata.imports));
54
+ }
55
+ specifiersArray.push(requestedSpecifierPlusImporter);
56
+ }
48
57
  }
49
58
  return importMetadata;
50
59
  }
@@ -127,3 +136,12 @@ async function createIndex(specifiers, moduleRegistry, runtimeEnvironment, runti
127
136
  await Promise.all(promises);
128
137
  return index;
129
138
  }
139
+ async function getVersionedSpecifier(moduleId, moduleRegistry) {
140
+ if (!moduleId.importer || moduleId.version) {
141
+ return (0, import_identity.getSpecifier)(moduleId);
142
+ }
143
+ const versionedModuleEntry = await moduleRegistry.getModuleEntry({
144
+ ...moduleId
145
+ });
146
+ return (0, import_identity.getSpecifier)(versionedModuleEntry);
147
+ }
package/build/es/graph.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { getSpecifier, getVersionedModuleId, isBundleDefinition } from './identity.js';
2
+ import { LwrUnresolvableError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
2
3
  import { ModuleNameType } from './import-metadata.js';
3
4
  export var GraphDepth;
4
5
  (function (GraphDepth) {
@@ -116,7 +117,9 @@ options, moduleRegistry, defRegistry, runtimeEnvironment, runtimeParams, visited
116
117
  ? await defRegistry.getModuleBundle(versionedModuleId, runtimeEnvironment, runtimeParams)
117
118
  : await defRegistry.getModule(versionedModuleId, runtimeParams);
118
119
  if (!moduleDef) {
119
- throw new Error('Missing module Definition');
120
+ throw createSingleDiagnosticError({
121
+ description: descriptions.UNRESOLVABLE.MODULE_ENTRY(versionedModuleId.specifier),
122
+ }, LwrUnresolvableError);
120
123
  }
121
124
  const flattened = [];
122
125
  await traverse(moduleDef, depth, flattened, 0, acc, defRegistry, runtimeEnvironment, runtimeParams);
@@ -1,3 +1,4 @@
1
+ import { getPropFromAttrName } from './identity.js';
1
2
  import SAXParser from 'parse5-sax-parser';
2
3
  import { Readable } from 'stream';
3
4
  function parseAssetLocation(htmlSource, tagName, attrLocation) {
@@ -28,11 +29,24 @@ export async function extractMetadataFromHtml(htmlSource) {
28
29
  const assetReferences = [];
29
30
  const parser = new SAXParser({ sourceCodeLocationInfo: true }); // TODO: Would we need this in the future?
30
31
  const ceRefStack = [];
31
- parser.on('startTag', ({ tagName, sourceCodeLocation, }) => {
32
+ parser.on('startTag', ({ tagName, attrs, // attributes are passed into SSR
33
+ sourceCodeLocation, }) => {
32
34
  // custom elements
33
35
  if (tagName.includes('-') && !openElements.has(tagName)) {
34
36
  const { startOffset, endOffset } = sourceCodeLocation;
35
- const ceRef = { tagName, location: { startOffset, endOffset } };
37
+ const ceRef = {
38
+ tagName,
39
+ location: { startOffset, endOffset },
40
+ // transform attributes [{ name: 'some-attr', value: 'the value' }] into properties { someAttr: 'the value' }
41
+ // leave props as undefined if there are no attributes
42
+ // set boolean attribute values to "true", or lwc will see them as falsy
43
+ props: attrs.length
44
+ ? attrs.reduce((obj, { name, value }) => {
45
+ obj[getPropFromAttrName(name)] = value === '' ? 'true' : value;
46
+ return obj;
47
+ }, {})
48
+ : undefined,
49
+ };
36
50
  openElements.add(tagName);
37
51
  if (ceRefStack.length) {
38
52
  // nested CE
@@ -127,13 +127,13 @@ interface PackageIdentity {
127
127
  */
128
128
  export declare function parsePackageSpecifier(specifier: string): PackageIdentity;
129
129
  /**
130
- * Turn a string from kebab case to camel case
130
+ * Turn a string from kebab case to "specifier case": namespace/camelCaseName
131
131
  * @param name A string in kebab case
132
132
  * @example - 'name-of-something' => 'name/ofSomething'
133
133
  */
134
- export declare function kebabcaseToCamelcase(name: string): string;
134
+ export declare function kebabCaseToModuleSpecifer(name: string): string;
135
135
  /**
136
- * Clone of lwr/init, reverse of kebabcaseToCamelcase, strips off versions
136
+ * Clone of lwr/init, reverse of kebabCaseToModuleSpecifer, strips off versions
137
137
  * @param specifier
138
138
  * @example - 'name/ofSomething/v/1.0.0' => 'name-of-something'
139
139
  */
@@ -144,6 +144,12 @@ export declare function moduleSpecifierToKebabCase(specifier: string): string;
144
144
  * @example - 'This IS a sentence' => 'this-is-a-sentence'
145
145
  */
146
146
  export declare function slugify(name: string): string;
147
+ /**
148
+ * Turn an html attribute into a LWC property, eg: 'best-property-ever' => 'bestPropertyEver'
149
+ * @param propName - a string in kebab case (lowercase)
150
+ * @returns - a string in camel case
151
+ */
152
+ export declare function getPropFromAttrName(propName: string): string;
147
153
  /**
148
154
  * Create a module URI prefix, stopping where the specifier would be (at "mi/")
149
155
  * @param param0 - URI props from the Runtime Environment
@@ -190,11 +190,11 @@ export function parsePackageSpecifier(specifier) {
190
190
  };
191
191
  }
192
192
  /**
193
- * Turn a string from kebab case to camel case
193
+ * Turn a string from kebab case to "specifier case": namespace/camelCaseName
194
194
  * @param name A string in kebab case
195
195
  * @example - 'name-of-something' => 'name/ofSomething'
196
196
  */
197
- export function kebabcaseToCamelcase(name) {
197
+ export function kebabCaseToModuleSpecifer(name) {
198
198
  const newName = [];
199
199
  let nsFound = false;
200
200
  let upper = false;
@@ -216,7 +216,7 @@ export function kebabcaseToCamelcase(name) {
216
216
  return newName.join('');
217
217
  }
218
218
  /**
219
- * Clone of lwr/init, reverse of kebabcaseToCamelcase, strips off versions
219
+ * Clone of lwr/init, reverse of kebabCaseToModuleSpecifer, strips off versions
220
220
  * @param specifier
221
221
  * @example - 'name/ofSomething/v/1.0.0' => 'name-of-something'
222
222
  */
@@ -236,6 +236,14 @@ export function slugify(name) {
236
236
  lower: true,
237
237
  });
238
238
  }
239
+ /**
240
+ * Turn an html attribute into a LWC property, eg: 'best-property-ever' => 'bestPropertyEver'
241
+ * @param propName - a string in kebab case (lowercase)
242
+ * @returns - a string in camel case
243
+ */
244
+ export function getPropFromAttrName(propName) {
245
+ return propName.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
246
+ }
239
247
  /**
240
248
  * Create a module URI prefix, stopping where the specifier would be (at "mi/")
241
249
  * @param param0 - URI props from the Runtime Environment
@@ -281,7 +289,10 @@ export function getModuleIdentity(req) {
281
289
  }
282
290
  export function getMappingIdentity(req) {
283
291
  const { specifiers } = req.params;
284
- const moduleIds = explodeSpecifiers(specifiers);
292
+ const moduleIds = explodeSpecifiers(specifiers).map((obj) => ({
293
+ ...obj,
294
+ importer: req.query.importer,
295
+ }));
285
296
  return {
286
297
  moduleIds,
287
298
  };
@@ -12,4 +12,5 @@ export * from './graph.js';
12
12
  export * from './mappings.js';
13
13
  export * from './urls.js';
14
14
  export * from './env.js';
15
+ export * from './logger.js';
15
16
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -12,4 +12,5 @@ export * from './graph.js';
12
12
  export * from './mappings.js';
13
13
  export * from './urls.js';
14
14
  export * from './env.js';
15
+ export * from './logger.js';
15
16
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import winston from 'winston';
2
+ export declare const logger: winston.Logger;
3
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1,9 @@
1
+ import winston from 'winston';
2
+ const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
3
+ console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
4
+ export const logger = winston.createLogger({
5
+ level: LOG_LEVEL,
6
+ format: winston.format.json(),
7
+ transports: [new winston.transports.Console()],
8
+ });
9
+ //# sourceMappingURL=logger.js.map
@@ -10,7 +10,9 @@ export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, r
10
10
  index: {},
11
11
  };
12
12
  for (const moduleId of moduleIds) {
13
- const specifier = getSpecifier(moduleId);
13
+ const requestedSpecifier = getSpecifier(moduleId);
14
+ // eslint-disable-next-line no-await-in-loop
15
+ const specifier = await getVersionedSpecifier(moduleId, moduleRegistry);
14
16
  // Check if we have already visited
15
17
  if (!visitedCache.has(specifier)) {
16
18
  // Traversal of the Module Graph is done to get all the URLs for discoverable dependencies.
@@ -27,6 +29,15 @@ export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, r
27
29
  // eslint-disable-next-line no-await-in-loop
28
30
  importMetadata = await toImportMetadata(moduleGraph, importMetadata, moduleRegistry, runtimeEnvironment, runtimeParams);
29
31
  }
32
+ // If the requested specifier is not the same as the versioned specifier, include the requested specifier's importer.
33
+ if (requestedSpecifier !== specifier) {
34
+ const requestedSpecifierPlusImporter = `${requestedSpecifier}?importer=${moduleId.importer}`;
35
+ const specifiersArray = Object.values(importMetadata.imports).find((a) => a.includes(specifier));
36
+ if (!specifiersArray) {
37
+ throw new Error('Could not find: ' + specifier + ' in ' + JSON.stringify(importMetadata.imports));
38
+ }
39
+ specifiersArray.push(requestedSpecifierPlusImporter);
40
+ }
30
41
  }
31
42
  return importMetadata;
32
43
  }
@@ -141,4 +152,13 @@ async function createIndex(specifiers, moduleRegistry, runtimeEnvironment, runti
141
152
  await Promise.all(promises);
142
153
  return index;
143
154
  }
155
+ async function getVersionedSpecifier(moduleId, moduleRegistry) {
156
+ if (!moduleId.importer || moduleId.version) {
157
+ return getSpecifier(moduleId);
158
+ }
159
+ const versionedModuleEntry = await moduleRegistry.getModuleEntry({
160
+ ...moduleId,
161
+ });
162
+ return getSpecifier(versionedModuleEntry);
163
+ }
144
164
  //# sourceMappingURL=mappings.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.7.0-alpha.1",
7
+ "version": "0.7.0-alpha.12",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -40,16 +40,17 @@
40
40
  "magic-string": "^0.25.7",
41
41
  "mime-types": "^2.1.33",
42
42
  "parse5-sax-parser": "^6.0.1",
43
- "slugify": "^1.4.5"
43
+ "slugify": "^1.4.5",
44
+ "winston": "^3.7.2"
44
45
  },
45
46
  "devDependencies": {
46
- "@lwrjs/diagnostics": "0.7.0-alpha.1",
47
- "@lwrjs/types": "0.7.0-alpha.1",
47
+ "@lwrjs/diagnostics": "0.7.0-alpha.12",
48
+ "@lwrjs/types": "0.7.0-alpha.12",
48
49
  "@types/mime-types": "2.1.1",
49
50
  "@types/path-to-regexp": "^1.7.0"
50
51
  },
51
52
  "engines": {
52
53
  "node": ">=14.15.4 <17"
53
54
  },
54
- "gitHead": "24245670d6908d37e465f2316f92385df63d22aa"
55
+ "gitHead": "785b8380a821b43b7320783dddaf20d1433216e2"
55
56
  }