@lwrjs/client-modules 0.10.0-alpha.2 → 0.10.0-alpha.20

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 +1,2 @@
1
1
  const o=[];let e;const t=globalThis.LWR;globalThis.LWR.define?globalThis.LWR=Object.freeze({define:globalThis.LWR.define}):delete globalThis.LWR;const n={addLoaderPlugin:()=>{console.warn("API is not supported in ESM format")},handleStaleModule:function(t){o.push(t),e||(e=new WebSocket(`ws://${location.host}`),e.addEventListener("message",(async({data:e})=>{const t=JSON.parse(e);if("moduleUpdate"===t.eventType){const{oldHash:e,newHash:n,module:{specifier:a}}=t.payload;for(let t=0;t<o.length;t++){if(null!==(0,o[t])({name:a,oldHash:e,newHash:n}))break}}})))},appMetadata:function(){const{appId:o,bootstrapModule:e,rootComponent:n,rootComponents:a}=t;return{appId:o,bootstrapModule:e,rootComponent:n,rootComponents:a}}()};export{n as services};
2
+ //# sourceMappingURL=servicesESM.js.map
@@ -0,0 +1,22 @@
1
+ // Polyfill the Declarative ShadowDOM spec - call this function AFTER the DOM has been parsed and BEFORE hydrateComponent() is invoked
2
+ // See https://github.com/salesforce/lwc-rfcs/blob/master/text/0129-declarative-shadow-dom-polyfill.md#single-loop-script-shadow-root-attachment-reference
3
+
4
+ function applyShadowRoots(node) {
5
+ // if this browser DOES NOT support Declarative ShadowDOM
6
+ node.querySelectorAll('template[shadowroot]').forEach(template => {
7
+ const mode = template.getAttribute('shadowroot') || 'open';
8
+ const shadowRoot = template.parentNode?.attachShadow({
9
+ mode
10
+ });
11
+ shadowRoot.appendChild(template.content);
12
+ template.remove();
13
+ applyShadowRoots(shadowRoot);
14
+ });
15
+ }
16
+ export function polyfillDeclarativeShadowDom(node = document) {
17
+ // eslint-disable-next-line no-prototype-builtins
18
+ if (!HTMLTemplateElement.prototype.hasOwnProperty('shadowRoot')) {
19
+ // if this browser DOES NOT support Declarative ShadowDOM
20
+ applyShadowRoots(node);
21
+ }
22
+ }
@@ -0,0 +1 @@
1
+ export const isServer = process && process.env && (process.env.SSR === 'true' || process.env.SSR);
@@ -1,10 +1,20 @@
1
- import { BOOTSTRAP_END } from 'lwr/metrics';
2
- import { logOperationStart } from 'lwr/profiler';
3
-
4
- // TODO: This is a temporal workaround until https://github.com/salesforce/lwc/pull/2083 is sorted
5
1
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
2
  // @ts-ignore
3
+ import { BOOTSTRAP_END, INIT, INIT_MODULE } from 'lwr/metrics';
4
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
+ // @ts-ignore
6
+ import { logOperationStart, logOperationEnd } from 'lwr/profiler';
7
+
8
+ // TODO: This is a temporal workaround until https://github.com/salesforce/lwc/pull/2083 is sorted - tmp
7
9
  import { createElement } from 'lwc';
10
+
11
+ // <hydrateComponentProxy> - This code is removed in core
12
+ import { hydrateComponent } from 'lwc';
13
+ function hydrateComponentProxy(customElement, Ctor, props) {
14
+ hydrateComponent(customElement, Ctor, props);
15
+ }
16
+ // </hydrateComponentProxy>
17
+
8
18
  function initializeWebComponent(elementName, Ctor) {
9
19
  return createElement(elementName, {
10
20
  is: Ctor
@@ -41,53 +51,110 @@ export function getPropFromAttrName(propName) {
41
51
  * bare specifier and corresponding LightningElement constructor
42
52
  * @example - [['x/appRoot', appCtor], ['x/nav', navCtor]]
43
53
  */
44
- export function init(rootModules) {
45
- if (typeof customElements !== 'undefined' && typeof document !== 'undefined') {
46
- const container = document.querySelector('[lwr-root]');
47
- rootModules.forEach(([moduleSpecifier, ctor]) => {
48
- // Kebab-case the specifier
49
- const elementName = toKebabCase(moduleSpecifier);
54
+ export function init(rootModules, ssrProps = {}) {
55
+ if (typeof customElements === 'undefined' || typeof document === 'undefined') {
56
+ logOperationStart({
57
+ id: BOOTSTRAP_END
58
+ });
59
+ return;
60
+ }
61
+ logOperationStart({
62
+ id: INIT
63
+ });
64
+ let index = 0;
65
+ for (const [specifier, ctor] of rootModules) {
66
+ const elementName = toKebabCase(specifier);
67
+
68
+ // initialize and inject the root module into the LWR Root or DOM if it is missing
69
+ if (!document.body.querySelector(elementName)) {
70
+ logOperationStart({
71
+ id: INIT_MODULE,
72
+ specifier,
73
+ specifierIndex: ++index
74
+ });
50
75
 
51
- // Append the root element to the DOM, if it does not exist
52
76
  // this is for SPA like routes (one component at the root level) utilizing the lwr-root directive
53
- let el = document.body.querySelector(elementName);
54
- if (!el) {
55
- el = initializeWebComponent(elementName, ctor);
56
- if (container) {
57
- // Append to a node with the "lwr-root" attribute
58
- container.appendChild(el);
59
- } else {
60
- // Otherwise, add the root to the <body>
61
- document.body.appendChild(el);
77
+ const component = initializeWebComponent(elementName, ctor);
78
+ const container = document.querySelector('[lwr-root]');
79
+ container ? container.appendChild(component) : document.body.appendChild(component);
80
+ logOperationEnd({
81
+ id: INIT_MODULE,
82
+ specifier,
83
+ specifierIndex: index,
84
+ metadata: {
85
+ renderMode: 'spa'
62
86
  }
63
- } else {
64
- // We have rendered/ssred an HTML page and we need to reify the components
65
- // Due to the bug described on the header, for each custom element
66
- // we collect the attributes and we replace the element with the new synthetic contructor
67
- const customElements = document.querySelectorAll(elementName);
68
- customElements.forEach(customElement => {
69
- const newElement = initializeWebComponent(elementName, ctor);
70
- for (const {
71
- name,
72
- value
73
- } of customElement.attributes) {
74
- newElement.setAttribute(name, value);
75
- const prop = getPropFromAttrName(name);
76
- if (prop in newElement) {
77
- // Set attributes as properties too for reactivity
78
- newElement[prop] = value;
79
- }
80
- }
87
+ });
88
+ continue;
89
+ }
81
90
 
82
- // Move the children
83
- while (customElement.childNodes.length > 0) {
84
- newElement.appendChild(customElement.childNodes[0]);
91
+ // the page has been rendered or SSR'd, and each component needs to initialized(or hydrated)
92
+ const elements = document.querySelectorAll(elementName);
93
+ for (const element of elements) {
94
+ logOperationStart({
95
+ id: INIT_MODULE,
96
+ specifier,
97
+ specifierIndex: ++index
98
+ });
99
+ const propsId = element.dataset.lwrPropsId;
100
+
101
+ // hydrate SSR'd components
102
+ if (propsId) {
103
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
104
+ // @ts-ignore
105
+ hydrateComponentProxy(element, ctor, ssrProps[propsId] || {});
106
+ logOperationEnd({
107
+ id: INIT_MODULE,
108
+ specifier,
109
+ specifierIndex: index,
110
+ metadata: {
111
+ renderMode: 'ssr'
85
112
  }
86
- customElement.parentElement.replaceChild(newElement, customElement);
87
113
  });
114
+ continue;
88
115
  }
89
- });
116
+
117
+ // Note: due to the bug described at the top of this file, each CSR'd custom element
118
+ // must be replaced with the new synthetic constructor. Attributes and children are
119
+ // copied over to the new component.
120
+ const component = initializeWebComponent(elementName, ctor);
121
+
122
+ // copy the attributes
123
+ for (const {
124
+ name,
125
+ value
126
+ } of element.attributes) {
127
+ component.setAttribute(name, value);
128
+ const prop = getPropFromAttrName(name);
129
+ if (prop in component) {
130
+ // set attributes as properties for reactivity
131
+ component[prop] = value;
132
+ }
133
+ }
134
+
135
+ // save the children
136
+ while (element.childNodes.length > 0) {
137
+ component.appendChild(element.childNodes[0]);
138
+ }
139
+
140
+ // swap the element out with the initialized component
141
+ const parent = element.parentElement;
142
+ if (parent) {
143
+ parent.replaceChild(component, element);
144
+ }
145
+ logOperationEnd({
146
+ id: INIT_MODULE,
147
+ specifier,
148
+ specifierIndex: index,
149
+ metadata: {
150
+ renderMode: 'csr'
151
+ }
152
+ });
153
+ }
90
154
  }
155
+ logOperationEnd({
156
+ id: INIT
157
+ });
91
158
  logOperationStart({
92
159
  id: BOOTSTRAP_END
93
160
  });