@lwrjs/client-modules 0.9.0-alpha.2 → 0.9.0-alpha.21

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.
@@ -1,15 +1,16 @@
1
1
  import { BOOTSTRAP_END } from 'lwr/metrics';
2
- import { logOperationStart } from 'lwr/profiler'; // TODO: This is a temporal workaround until https://github.com/salesforce/lwc/pull/2083 is sorted
2
+ import { logOperationStart } from 'lwr/profiler';
3
+
4
+ // TODO: This is a temporal workaround until https://github.com/salesforce/lwc/pull/2083 is sorted
3
5
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
6
  // @ts-ignore
5
-
6
7
  import { createElement } from 'lwc';
7
-
8
8
  function initializeWebComponent(elementName, Ctor) {
9
9
  return createElement(elementName, {
10
10
  is: Ctor
11
11
  });
12
12
  }
13
+
13
14
  /**
14
15
  * Convert a module specifier into a valid CustomElement registry name:
15
16
  * - remove any version linking
@@ -20,20 +21,19 @@ function initializeWebComponent(elementName, Ctor) {
20
21
  * eg: "lwr/example/v/1.0.0" => "lwr-example"
21
22
  * @param specifier The bare specifier to convert
22
23
  */
23
-
24
-
25
24
  export function toKebabCase(specifier) {
26
25
  return specifier.replace(/\/v\/[a-zA-Z0-9-_.]+$/, '').replace('/', '-').replace(/([A-Z])/g, c => `-${c.toLowerCase()}`);
27
26
  }
27
+
28
28
  /**
29
29
  * This method maps between attribute names
30
30
  * and the corresponding props name.
31
31
  */
32
-
33
32
  const CAMEL_REGEX = /-([a-z])/g;
34
33
  export function getPropFromAttrName(propName) {
35
34
  return propName.replace(CAMEL_REGEX, g => g[1].toUpperCase());
36
35
  }
36
+
37
37
  /**
38
38
  * Import any requested static application dependencies, define the root
39
39
  * application component(s) into the CustomElement registry, and inject them.
@@ -41,20 +41,18 @@ export function getPropFromAttrName(propName) {
41
41
  * bare specifier and corresponding LightningElement constructor
42
42
  * @example - [['x/appRoot', appCtor], ['x/nav', navCtor]]
43
43
  */
44
-
45
44
  export function init(rootModules) {
46
45
  if (typeof customElements !== 'undefined' && typeof document !== 'undefined') {
47
46
  const container = document.querySelector('[lwr-root]');
48
47
  rootModules.forEach(([moduleSpecifier, ctor]) => {
49
48
  // Kebab-case the specifier
50
- const elementName = toKebabCase(moduleSpecifier); // Append the root element to the DOM, if it does not exist
51
- // this is for SPA like routes (one component at the root level) utilizing the lwr-root directive
49
+ const elementName = toKebabCase(moduleSpecifier);
52
50
 
51
+ // Append the root element to the DOM, if it does not exist
52
+ // this is for SPA like routes (one component at the root level) utilizing the lwr-root directive
53
53
  let el = document.body.querySelector(elementName);
54
-
55
54
  if (!el) {
56
55
  el = initializeWebComponent(elementName, ctor);
57
-
58
56
  if (container) {
59
57
  // Append to a node with the "lwr-root" attribute
60
58
  container.appendChild(el);
@@ -69,31 +67,27 @@ export function init(rootModules) {
69
67
  const customElements = document.querySelectorAll(elementName);
70
68
  customElements.forEach(customElement => {
71
69
  const newElement = initializeWebComponent(elementName, ctor);
72
-
73
70
  for (const {
74
71
  name,
75
72
  value
76
73
  } of customElement.attributes) {
77
74
  newElement.setAttribute(name, value);
78
75
  const prop = getPropFromAttrName(name);
79
-
80
76
  if (prop in newElement) {
81
77
  // Set attributes as properties too for reactivity
82
78
  newElement[prop] = value;
83
79
  }
84
- } // Move the children
85
-
80
+ }
86
81
 
82
+ // Move the children
87
83
  while (customElement.childNodes.length > 0) {
88
84
  newElement.appendChild(customElement.childNodes[0]);
89
85
  }
90
-
91
86
  customElement.parentElement.replaceChild(newElement, customElement);
92
87
  });
93
88
  }
94
89
  });
95
90
  }
96
-
97
91
  logOperationStart({
98
92
  id: BOOTSTRAP_END
99
93
  });
@@ -5,6 +5,7 @@ import { hydrateComponent } from 'lwc';
5
5
  export function toKebabCase(specifier) {
6
6
  return specifier.replace(/\/v\/[a-zA-Z0-9-_.]+$/, '').replace('/', '-').replace(/([A-Z])/g, c => `-${c.toLowerCase()}`);
7
7
  }
8
+
8
9
  /**
9
10
  * Hydrate the given component(s) which were server-side rendered (see the lwc-ssr/viewTransformer).
10
11
  * @param components - An array of arrays, each one holding:
@@ -13,13 +14,12 @@ export function toKebabCase(specifier) {
13
14
  * @param ssrProps - map of properties to set on each component
14
15
  * @example - [['x/appRoot', appCtor], ['x/nav', navCtor]]
15
16
  */
16
-
17
17
  export function init(components, ssrProps = {}) {
18
18
  if (typeof document !== 'undefined') {
19
19
  components.forEach(([moduleSpecifier, ctor]) => {
20
20
  // Kebab-case the specifier
21
- const elementName = toKebabCase(moduleSpecifier); // Find all instances of the component in the document, and hydrate them all
22
-
21
+ const elementName = toKebabCase(moduleSpecifier);
22
+ // Find all instances of the component in the document, and hydrate them all
23
23
  const customElements = document.querySelectorAll(elementName);
24
24
  customElements.forEach(customElement => {
25
25
  const propsId = customElement.dataset.lwrPropsId;