@lwrjs/client-modules
Advanced tools
Comparing version 0.0.2-alpha2 to 0.0.2-alpha3
@@ -15,4 +15,4 @@ /** | ||
/** | ||
* Import any requested static application dependencies and | ||
* define the root application component into the CustomElement registry. | ||
* Import any requested static application dependencies, define the root | ||
* application component into the CustomElement registry, and inject it. | ||
* @param rootAppSpecifier - The bare specifier for the component, eg: 'x/appRoot' | ||
@@ -24,6 +24,22 @@ * @param rootApp - A reference to the Constructor (extended from LightningElement) | ||
export function init(rootAppSpecifier, rootApp) { | ||
if (typeof customElements !== 'undefined') { | ||
const elementName = toKebabCase(rootAppSpecifier); | ||
customElements.define(elementName, rootApp.CustomElementConstructor); | ||
if (typeof customElements !== 'undefined' && typeof document !== 'undefined') { | ||
const elementName = toKebabCase(rootAppSpecifier); // Add the root to the CustomElementRegistry | ||
customElements.define(elementName, rootApp.CustomElementConstructor); // Append the root element to the DOM, if it does not exist | ||
const rootEl = document.body.querySelector(elementName); | ||
if (!rootEl) { | ||
const el = document.createElement(elementName); | ||
const container = document.querySelector('[lwr-root]'); | ||
if (container) { | ||
// Append to a node with the "lwr-root" attribute | ||
container.appendChild(el); | ||
} else { | ||
// Otherwise, add the root to the <body> | ||
document.body.appendChild(el); | ||
} | ||
} | ||
} | ||
} |
import { init } from '../init'; | ||
const LauraCmp = { | ||
CustomElementConstructor: class Fake {}, | ||
const TestCmp = { | ||
CustomElementConstructor: class Fake extends HTMLElement {}, | ||
}; | ||
const KhangCmp = { | ||
CustomElementConstructor: class Fake {}, | ||
const TestCmp2 = { | ||
CustomElementConstructor: class Fake extends HTMLElement {}, | ||
}; | ||
const TestCmp3 = { | ||
CustomElementConstructor: class Fake extends HTMLElement {}, | ||
}; | ||
describe('LWR init', () => { | ||
it('init()', () => { | ||
init('generated/lauraHomePage', LauraCmp); | ||
it('initializes and append root element to <body>', () => { | ||
init('generated/lauraHomePage', TestCmp); | ||
expect(customElements.get('generated-laura-home-page')).toBeDefined(); | ||
}); | ||
it('init() headless', () => { | ||
it('initializes and append root element to lwr-root node', () => { | ||
const container = document.createElement('div'); | ||
container.setAttribute('lwr-root', ''); | ||
document.body.appendChild(container); | ||
init('c/darrell', TestCmp2); | ||
expect(customElements.get('c-darrell')).toBeDefined(); | ||
}); | ||
it('initializes and skips appending the root element', () => { | ||
document.body.appendChild(document.createElement('c-diego')); | ||
init('c/diego', TestCmp3); | ||
expect(customElements.get('c-diego')).toBeDefined(); | ||
}); | ||
it('initializes in a headless environment', () => { | ||
const oldCustomElements = customElements; | ||
delete globalThis.customElements; | ||
init('c/khang', KhangCmp); | ||
init('c/khang', TestCmp); | ||
globalThis.customElements = oldCustomElements; | ||
@@ -20,0 +35,0 @@ expect(customElements.get('c-khang')).toBeUndefined(); |
@@ -19,4 +19,4 @@ /** | ||
/** | ||
* Import any requested static application dependencies and | ||
* define the root application component into the CustomElement registry. | ||
* Import any requested static application dependencies, define the root | ||
* application component into the CustomElement registry, and inject it. | ||
* @param rootAppSpecifier - The bare specifier for the component, eg: 'x/appRoot' | ||
@@ -26,6 +26,22 @@ * @param rootApp - A reference to the Constructor (extended from LightningElement) | ||
export function init(rootAppSpecifier: string, rootApp): void { | ||
if (typeof customElements !== 'undefined') { | ||
if (typeof customElements !== 'undefined' && typeof document !== 'undefined') { | ||
const elementName = toKebabCase(rootAppSpecifier); | ||
// Add the root to the CustomElementRegistry | ||
customElements.define(elementName, rootApp.CustomElementConstructor); | ||
// Append the root element to the DOM, if it does not exist | ||
const rootEl = document.body.querySelector(elementName); | ||
if (!rootEl) { | ||
const el = document.createElement(elementName); | ||
const container = document.querySelector('[lwr-root]'); | ||
if (container) { | ||
// Append to a node with the "lwr-root" attribute | ||
container.appendChild(el); | ||
} else { | ||
// Otherwise, add the root to the <body> | ||
document.body.appendChild(el); | ||
} | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.0.2-alpha2", | ||
"version": "0.0.2-alpha3", | ||
"homepage": "https://lwr.dev/", | ||
@@ -46,3 +46,3 @@ "repository": { | ||
}, | ||
"gitHead": "38b630d6f3c0b9c554de34bd6303a5fdc975a581" | ||
"gitHead": "0a3ec38ad674e502f5dae4d232de8975fb0e4d76" | ||
} |
247337
6268