@lwrjs/shared-utils 0.8.0-alpha.9 → 0.9.0-alpha.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.
package/build/cjs/env.cjs CHANGED
@@ -11,7 +11,14 @@ __export(exports, {
11
11
  getFeatureFlags: () => getFeatureFlags
12
12
  });
13
13
  function getFeatureFlags() {
14
+ let legacyLoader = process.env.LEGACY_LOADER !== void 0 && process.env.LEGACY_LOADER === "true" ? true : false;
15
+ try {
16
+ if (LWR) {
17
+ legacyLoader = LWR.LEGACY_LOADER;
18
+ }
19
+ } catch (e) {
20
+ }
14
21
  return {
15
- LEGACY_LOADER: process.env.LEGACY_LOADER !== void 0 && process.env.LEGACY_LOADER === "true" ? true : false
22
+ LEGACY_LOADER: legacyLoader
16
23
  };
17
24
  }
package/build/cjs/fs.cjs CHANGED
@@ -43,6 +43,7 @@ var import_object = __toModule(require("./object.cjs"));
43
43
  var import_chokidar = __toModule(require("chokidar"));
44
44
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
45
45
  var import_mime_types = __toModule(require("mime-types"));
46
+ var import_logger = __toModule(require("./logger.cjs"));
46
47
  function hashContent(source) {
47
48
  return import_crypto.default.createHash("md5").update(source).digest("hex");
48
49
  }
@@ -77,7 +78,7 @@ function setupWatcher(onModuleChange) {
77
78
  const fileWatcher = createFileWatcher();
78
79
  fileWatcher.on("change", (0, import_object.debounce)((file) => onModuleChange(file), 500));
79
80
  fileWatcher.on("unlink", (0, import_object.debounce)((file) => onModuleChange(file), 500));
80
- fileWatcher.on("add", (file) => console.log("Watching: ", file));
81
+ fileWatcher.on("add", (file) => import_logger.logger.info(`Watching: ${file}`));
81
82
  return fileWatcher;
82
83
  }
83
84
  function canResolveView(source, type) {
@@ -24,7 +24,9 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/shared-utils/src/html-meta.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
- extractMetadataFromHtml: () => extractMetadataFromHtml
27
+ extractMetadataFromHtml: () => extractMetadataFromHtml,
28
+ isRelative: () => isRelative,
29
+ isSelfUrl: () => isSelfUrl
28
30
  });
29
31
  var import_identity = __toModule(require("./identity.cjs"));
30
32
  var import_parse5_sax_parser = __toModule(require("parse5-sax-parser"));
@@ -38,12 +40,21 @@ function parseAssetLocation(htmlSource, tagName, attrLocation) {
38
40
  return {
39
41
  url,
40
42
  tagName,
43
+ relative: isRelative(url),
41
44
  location: {
42
45
  startOffset: startOffset + keyAttr.length + 2,
43
46
  endOffset: endOffset - 1
44
47
  }
45
48
  };
46
49
  }
50
+ function isRelative(url) {
51
+ return !url?.match(isNotRelativeRegex);
52
+ }
53
+ var isNotRelativeRegex = /^(http(s)?:\/\/|\/)/i;
54
+ function isSelfUrl(url) {
55
+ return !url || !!url.match(isSelfUrlRegex);
56
+ }
57
+ var isSelfUrlRegex = /^\s*(data:|#)/i;
47
58
  async function extractMetadataFromHtml(htmlSource) {
48
59
  return new Promise((resolve, reject) => {
49
60
  const customElements = [];
@@ -34,3 +34,4 @@ __exportStar(exports, __toModule(require("./mappings.cjs")));
34
34
  __exportStar(exports, __toModule(require("./urls.cjs")));
35
35
  __exportStar(exports, __toModule(require("./env.cjs")));
36
36
  __exportStar(exports, __toModule(require("./logger.cjs")));
37
+ __exportStar(exports, __toModule(require("./lwr-app-observer.cjs")));
@@ -38,8 +38,10 @@ var DEBUG = "debug";
38
38
  var INFO = "info";
39
39
  var WARN = "warn";
40
40
  var ERROR = "error";
41
+ var options = {};
42
+ var DUPES = new Set();
41
43
  var currentLevel = process.env.LOG_LEVEL || INFO;
42
- var log = (level, message, additionalInfo) => {
44
+ function log(level, message, additionalInfo) {
43
45
  const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
44
46
  if (currentLevel !== LOG_LEVEL) {
45
47
  currentLevel = LOG_LEVEL;
@@ -63,21 +65,31 @@ var log = (level, message, additionalInfo) => {
63
65
  shouldLog = true;
64
66
  break;
65
67
  }
68
+ if (shouldLog && options.dedupe && options.dedupe.has(level)) {
69
+ const key = `[${level}] : ${message}`;
70
+ if (DUPES.has(key)) {
71
+ shouldLog = false;
72
+ } else {
73
+ DUPES.add(key);
74
+ }
75
+ }
66
76
  if (shouldLog) {
67
77
  let logMethod;
68
78
  if (level == ERROR) {
69
79
  logMethod = console.error;
80
+ } else if (level == WARN) {
81
+ logMethod = console.warn;
70
82
  } else {
71
83
  logMethod = console.log;
72
84
  }
73
85
  if (additionalInfo) {
74
- logMethod(`[${level}] : ${message}
86
+ logMethod(`[${level}] ${message}
75
87
  Additional Info: ${JSON.stringify(additionalInfo)}`);
76
88
  } else {
77
- logMethod(`[${level}] : ${message}`);
89
+ logMethod(`[${level}] ${message}`);
78
90
  }
79
91
  }
80
- };
92
+ }
81
93
  var stringifyError = (error) => {
82
94
  if (error instanceof import_diagnostics.DiagnosticsError) {
83
95
  return JSON.stringify({
@@ -104,5 +116,9 @@ var logger = {
104
116
  info: (message, additionalInfo) => log(INFO, message, additionalInfo),
105
117
  warn: (message, additionalInfo) => log(WARN, message, additionalInfo),
106
118
  error: (error, additionalInfo) => log(ERROR, stringifyError(error), additionalInfo),
107
- log
119
+ log,
120
+ setOptions: (opts) => {
121
+ options = opts;
122
+ },
123
+ currentLevel
108
124
  };
@@ -0,0 +1,82 @@
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/lwr-app-observer.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ LwrApplicationObserver: () => LwrApplicationObserver
28
+ });
29
+ var import_events = __toModule(require("events"));
30
+ var MODULE_DEF_CHANGED_EVENT = "module_definition_changed";
31
+ var MODULE_SOURCE_CHANGED_EVENT = "module_source_changed";
32
+ var VIEW_SOURCE_CHANGED_EVENT = "view_source_changed";
33
+ var ASSET_SOURCE_CHANGED_EVENT = "asset_source_changed";
34
+ var LwrEmitter = class {
35
+ constructor(observer) {
36
+ this.observer = observer;
37
+ }
38
+ notifyAssetSourceChanged(payload) {
39
+ this.observer.emit(ASSET_SOURCE_CHANGED_EVENT, {
40
+ eventType: ASSET_SOURCE_CHANGED_EVENT,
41
+ payload
42
+ });
43
+ }
44
+ notifyViewSourceChanged(payload) {
45
+ this.observer.emit(VIEW_SOURCE_CHANGED_EVENT, {
46
+ eventType: VIEW_SOURCE_CHANGED_EVENT,
47
+ payload
48
+ });
49
+ }
50
+ notifyModuleDefinitionChanged(payload) {
51
+ this.observer.emit(MODULE_DEF_CHANGED_EVENT, {
52
+ eventType: MODULE_DEF_CHANGED_EVENT,
53
+ payload
54
+ });
55
+ }
56
+ notifyModuleSourceChanged(payload) {
57
+ this.observer.emit(MODULE_SOURCE_CHANGED_EVENT, {
58
+ eventType: MODULE_SOURCE_CHANGED_EVENT,
59
+ payload
60
+ });
61
+ }
62
+ };
63
+ var LwrApplicationObserver = class extends import_events.EventEmitter {
64
+ constructor() {
65
+ super();
66
+ }
67
+ onModuleDefinitionChange(listener) {
68
+ this.on(MODULE_DEF_CHANGED_EVENT, listener);
69
+ }
70
+ onModuleSourceChange(listener) {
71
+ this.on(MODULE_SOURCE_CHANGED_EVENT, listener);
72
+ }
73
+ onViewSourceChange(listener) {
74
+ this.on(VIEW_SOURCE_CHANGED_EVENT, listener);
75
+ }
76
+ onAssetSourceChange(listener) {
77
+ this.on(ASSET_SOURCE_CHANGED_EVENT, listener);
78
+ }
79
+ createLwrEmitter() {
80
+ return new LwrEmitter(this);
81
+ }
82
+ };
package/build/es/env.js CHANGED
@@ -1,7 +1,17 @@
1
1
  export function getFeatureFlags() {
2
+ let legacyLoader = process.env.LEGACY_LOADER !== undefined && process.env.LEGACY_LOADER === 'true' ? true : false;
3
+ try {
4
+ // Use the Shim for MRT since we don't have access to managing environment variables
5
+ if (LWR) {
6
+ legacyLoader = LWR.LEGACY_LOADER;
7
+ }
8
+ }
9
+ catch (e) {
10
+ // No shim, ignore error
11
+ }
2
12
  return {
3
13
  // DEFAULT LEGACY_LOADER = false;
4
- LEGACY_LOADER: process.env.LEGACY_LOADER !== undefined && process.env.LEGACY_LOADER === 'true' ? true : false,
14
+ LEGACY_LOADER: legacyLoader,
5
15
  };
6
16
  }
7
17
  //# sourceMappingURL=env.js.map
package/build/es/fs.js CHANGED
@@ -6,6 +6,7 @@ import { debounce } from './object.js';
6
6
  import chokidar from 'chokidar';
7
7
  import { LwrUnresolvableError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
8
8
  import { lookup } from 'mime-types';
9
+ import { logger } from './logger.js';
9
10
  /**
10
11
  * Create a hash string for a source
11
12
  * @param source
@@ -67,7 +68,7 @@ export function setupWatcher(onModuleChange) {
67
68
  const fileWatcher = createFileWatcher();
68
69
  fileWatcher.on('change', debounce((file) => onModuleChange(file), 500));
69
70
  fileWatcher.on('unlink', debounce((file) => onModuleChange(file), 500));
70
- fileWatcher.on('add', (file) => console.log('Watching: ', file));
71
+ fileWatcher.on('add', (file) => logger.info(`Watching: ${file}`));
71
72
  return fileWatcher;
72
73
  }
73
74
  /**
@@ -1,4 +1,6 @@
1
1
  import { RenderedViewMetadata } from '@lwrjs/types';
2
+ export declare function isRelative(url: string): boolean;
3
+ export declare function isSelfUrl(url: string): boolean;
2
4
  /**
3
5
  * Pull the custom elements and img tags out of an HTML string, to use as metadata
4
6
  * @param htmlSource - An HTML string to parse
@@ -12,12 +12,23 @@ function parseAssetLocation(htmlSource, tagName, attrLocation) {
12
12
  return {
13
13
  url,
14
14
  tagName,
15
+ relative: isRelative(url),
15
16
  location: {
16
17
  startOffset: startOffset + keyAttr.length + 2 /* =" */,
17
18
  endOffset: endOffset - 1,
18
19
  },
19
20
  };
20
21
  }
22
+ // Detect if this is a relative URL
23
+ export function isRelative(url) {
24
+ return !url?.match(isNotRelativeRegex);
25
+ }
26
+ const isNotRelativeRegex = /^(http(s)?:\/\/|\/)/i;
27
+ // Detect if this is just a self referential URL
28
+ export function isSelfUrl(url) {
29
+ return !url || !!url.match(isSelfUrlRegex);
30
+ }
31
+ const isSelfUrlRegex = /^\s*(data:|#)/i;
21
32
  /**
22
33
  * Pull the custom elements and img tags out of an HTML string, to use as metadata
23
34
  * @param htmlSource - An HTML string to parse
@@ -13,4 +13,5 @@ export * from './mappings.js';
13
13
  export * from './urls.js';
14
14
  export * from './env.js';
15
15
  export * from './logger.js';
16
+ export * from './lwr-app-observer.js';
16
17
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -13,4 +13,5 @@ export * from './mappings.js';
13
13
  export * from './urls.js';
14
14
  export * from './env.js';
15
15
  export * from './logger.js';
16
+ export * from './lwr-app-observer.js';
16
17
  //# sourceMappingURL=index.js.map
@@ -4,6 +4,10 @@ export declare const DEBUG: LEVEL;
4
4
  export declare const INFO: LEVEL;
5
5
  export declare const WARN: LEVEL;
6
6
  export declare const ERROR: LEVEL;
7
+ declare type LoggerOptions = {
8
+ dedupe?: Set<string>;
9
+ };
10
+ declare function log(level: string, message: string, additionalInfo?: any): void;
7
11
  export declare const stringifyError: (error: any) => string;
8
12
  export declare const logger: {
9
13
  verbose: (message: string, additionalInfo?: any) => void;
@@ -11,7 +15,9 @@ export declare const logger: {
11
15
  info: (message: string, additionalInfo?: any) => void;
12
16
  warn: (message: string, additionalInfo?: any) => void;
13
17
  error: (error: any, additionalInfo?: any) => void;
14
- log: (level: string, message: string, additionalInfo?: any) => void;
18
+ log: typeof log;
19
+ setOptions: (opts: LoggerOptions) => void;
20
+ currentLevel: string;
15
21
  };
16
22
  export {};
17
23
  //# sourceMappingURL=logger.d.ts.map
@@ -4,8 +4,10 @@ export const DEBUG = 'debug';
4
4
  export const INFO = 'info';
5
5
  export const WARN = 'warn';
6
6
  export const ERROR = 'error';
7
+ let options = {};
8
+ const DUPES = new Set();
7
9
  let currentLevel = process.env.LOG_LEVEL || INFO;
8
- const log = (level, message, additionalInfo) => {
10
+ function log(level, message, additionalInfo) {
9
11
  const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
10
12
  if (currentLevel !== LOG_LEVEL) {
11
13
  currentLevel = LOG_LEVEL;
@@ -29,22 +31,36 @@ const log = (level, message, additionalInfo) => {
29
31
  shouldLog = true;
30
32
  break;
31
33
  }
34
+ // Check if we should suppress dupes and we have already logged this message
35
+ if (shouldLog && options.dedupe && options.dedupe.has(level)) {
36
+ const key = `[${level}] : ${message}`;
37
+ if (DUPES.has(key)) {
38
+ shouldLog = false;
39
+ }
40
+ else {
41
+ // add key to de-duplicate cache
42
+ DUPES.add(key);
43
+ }
44
+ }
32
45
  if (shouldLog) {
33
46
  let logMethod;
34
47
  if (level == ERROR) {
35
48
  logMethod = console.error;
36
49
  }
50
+ else if (level == WARN) {
51
+ logMethod = console.warn;
52
+ }
37
53
  else {
38
54
  logMethod = console.log;
39
55
  }
40
56
  if (additionalInfo) {
41
- logMethod(`[${level}] : ${message} \nAdditional Info: ${JSON.stringify(additionalInfo)}`);
57
+ logMethod(`[${level}] ${message} \nAdditional Info: ${JSON.stringify(additionalInfo)}`);
42
58
  }
43
59
  else {
44
- logMethod(`[${level}] : ${message}`);
60
+ logMethod(`[${level}] ${message}`);
45
61
  }
46
62
  }
47
- };
63
+ }
48
64
  export const stringifyError = (error) => {
49
65
  if (error instanceof DiagnosticsError) {
50
66
  return JSON.stringify({
@@ -74,5 +90,9 @@ export const logger = {
74
90
  warn: (message, additionalInfo) => log(WARN, message, additionalInfo),
75
91
  error: (error, additionalInfo) => log(ERROR, stringifyError(error), additionalInfo),
76
92
  log,
93
+ setOptions: (opts) => {
94
+ options = opts;
95
+ },
96
+ currentLevel,
77
97
  };
78
98
  //# sourceMappingURL=logger.js.map
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import { ModuleCompiled, LwrAppEmitter, LwrAppObserver, ModuleDefinitionChangedEvent, ModuleSourceChangedEvent, CompiledView, AssetSource, AssetSourceChangedEvent, ViewSourceChangedEvent } from '@lwrjs/types';
4
+ declare class LwrEmitter implements LwrAppEmitter {
5
+ observer: LwrApplicationObserver;
6
+ constructor(observer: LwrApplicationObserver);
7
+ notifyAssetSourceChanged(payload: AssetSource): void;
8
+ notifyViewSourceChanged(payload: CompiledView): void;
9
+ notifyModuleDefinitionChanged(payload: ModuleCompiled): void;
10
+ notifyModuleSourceChanged(payload: ModuleCompiled): void;
11
+ }
12
+ export declare class LwrApplicationObserver extends EventEmitter implements LwrAppObserver {
13
+ constructor();
14
+ onModuleDefinitionChange(listener: (event: ModuleDefinitionChangedEvent) => void): void;
15
+ onModuleSourceChange(listener: (event: ModuleSourceChangedEvent) => void): void;
16
+ onViewSourceChange(listener: (event: ViewSourceChangedEvent) => void): void;
17
+ onAssetSourceChange(listener: (event: AssetSourceChangedEvent) => void): void;
18
+ createLwrEmitter(): LwrEmitter;
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=lwr-app-observer.d.ts.map
@@ -0,0 +1,55 @@
1
+ import { EventEmitter } from 'events';
2
+ const MODULE_DEF_CHANGED_EVENT = 'module_definition_changed';
3
+ const MODULE_SOURCE_CHANGED_EVENT = 'module_source_changed';
4
+ const VIEW_SOURCE_CHANGED_EVENT = 'view_source_changed';
5
+ const ASSET_SOURCE_CHANGED_EVENT = 'asset_source_changed';
6
+ class LwrEmitter {
7
+ constructor(observer) {
8
+ this.observer = observer;
9
+ }
10
+ notifyAssetSourceChanged(payload) {
11
+ this.observer.emit(ASSET_SOURCE_CHANGED_EVENT, {
12
+ eventType: ASSET_SOURCE_CHANGED_EVENT,
13
+ payload,
14
+ });
15
+ }
16
+ notifyViewSourceChanged(payload) {
17
+ this.observer.emit(VIEW_SOURCE_CHANGED_EVENT, {
18
+ eventType: VIEW_SOURCE_CHANGED_EVENT,
19
+ payload,
20
+ });
21
+ }
22
+ notifyModuleDefinitionChanged(payload) {
23
+ this.observer.emit(MODULE_DEF_CHANGED_EVENT, {
24
+ eventType: MODULE_DEF_CHANGED_EVENT,
25
+ payload,
26
+ });
27
+ }
28
+ notifyModuleSourceChanged(payload) {
29
+ this.observer.emit(MODULE_SOURCE_CHANGED_EVENT, {
30
+ eventType: MODULE_SOURCE_CHANGED_EVENT,
31
+ payload,
32
+ });
33
+ }
34
+ }
35
+ export class LwrApplicationObserver extends EventEmitter {
36
+ constructor() {
37
+ super();
38
+ }
39
+ onModuleDefinitionChange(listener) {
40
+ this.on(MODULE_DEF_CHANGED_EVENT, listener);
41
+ }
42
+ onModuleSourceChange(listener) {
43
+ this.on(MODULE_SOURCE_CHANGED_EVENT, listener);
44
+ }
45
+ onViewSourceChange(listener) {
46
+ this.on(VIEW_SOURCE_CHANGED_EVENT, listener);
47
+ }
48
+ onAssetSourceChange(listener) {
49
+ this.on(ASSET_SOURCE_CHANGED_EVENT, listener);
50
+ }
51
+ createLwrEmitter() {
52
+ return new LwrEmitter(this);
53
+ }
54
+ }
55
+ //# sourceMappingURL=lwr-app-observer.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.8.0-alpha.9",
7
+ "version": "0.9.0-alpha.0",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -45,13 +45,13 @@
45
45
  "winston": "^3.7.2"
46
46
  },
47
47
  "devDependencies": {
48
- "@lwrjs/diagnostics": "0.8.0-alpha.9",
49
- "@lwrjs/types": "0.8.0-alpha.9",
48
+ "@lwrjs/diagnostics": "0.9.0-alpha.0",
49
+ "@lwrjs/types": "0.9.0-alpha.0",
50
50
  "@types/mime-types": "2.1.1",
51
51
  "@types/path-to-regexp": "^1.7.0"
52
52
  },
53
53
  "engines": {
54
54
  "node": ">=14.15.4 <19"
55
55
  },
56
- "gitHead": "037c471903c6753fc6866ef598571b0df83e58f9"
56
+ "gitHead": "6890d8619b295a49ee1ed8253a372337d83863be"
57
57
  }