Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ async function init() {
const distRoot = './dist';
const pagesRoot = './docs/pages';
const pages = await fs.readdir(new URL(pagesRoot, import.meta.url));
const { html } = await renderToString(new URL('./docs/layout.js', import.meta.url), {
lightMode: true
});
const { html } = await renderToString(new URL('./docs/layout.js', import.meta.url));

await fs.rm(distRoot, { recursive: true, force: true });
await fs.mkdir(distRoot, { recursive: true });
Expand Down
5 changes: 1 addition & 4 deletions docs/components/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ template.innerHTML = `

class Footer extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
this.innerHTML = template.content.textContent;
}
}

Expand Down
5 changes: 1 addition & 4 deletions docs/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ template.innerHTML = `

class Header extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
this.innerHTML = template.content.textContent;
}
}

Expand Down
5 changes: 1 addition & 4 deletions docs/components/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ template.innerHTML = `

class Navigation extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
this.innerHTML = template.content.textContent;
}
}

Expand Down
6 changes: 1 addition & 5 deletions docs/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ template.innerHTML = `
`;

class Layout extends HTMLElement {

connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
this.innerHTML = template.content.textContent;
}
}

Expand Down
15 changes: 0 additions & 15 deletions docs/pages/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@ const { html } = await renderFromHTML(`

For example, even if `Header` or `Footer` use `import` to pull in additional custom elements, only the `Header` and `Footer custom elements used in the "entry" HTML are needed in the array.

### Options

`renderToString` and `renderFromHTML` also supports a second and third parameter respectively, that is an object, called `options`
```js
// default values
{
lightMode: false
}
```

It supports the following configuration(s):

- `lightMode`: For more static outcomes (e.g. no declarative shadow DOM), this option will omit all wrapping `<template shadowroot="...">` tags when rendering out custom elements. Useful for static sites or working with global CSS libraries.


## Metadata

`renderToString` and `renderFromHTML` return not only HTML, but also metadata about all the custom elements registered as part of rendering the entry file.
Expand Down
29 changes: 12 additions & 17 deletions src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ function isCustomElementDefinitionNode(node) {
&& expression.callee.property.name === 'define';
}

async function renderComponentRoots(tree, includeShadowRoots = true) {
async function renderComponentRoots(tree) {
for (const node of tree.childNodes) {
if (node.tagName && node.tagName.indexOf('-') > 0) {
const { tagName } = node;
const { moduleURL } = definitions[tagName];
const elementInstance = await initializeCustomElement(moduleURL, tagName, node.attrs);
const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots })
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
const elementTree = parseFragment(elementHtml);
node.childNodes = node.childNodes.length === 0

node.childNodes = node.childNodes.length === 0
? elementTree.childNodes
: [...elementTree.childNodes, ...node.childNodes];
}

if (node.childNodes && node.childNodes.length > 0) {
await renderComponentRoots(node, includeShadowRoots);
await renderComponentRoots(node);
}

// does this only apply to `<template>` tags?
if (node.content && node.content.childNodes && node.content.childNodes.length > 0) {
await renderComponentRoots(node.content, includeShadowRoots);
await renderComponentRoots(node.content);
}
}

Expand Down Expand Up @@ -125,20 +125,18 @@ async function initializeCustomElement(elementURL, tagName, attrs = []) {
return elementInstance;
}

async function renderToString(elementURL, options = {}) {
async function renderToString(elementURL) {
definitions = [];

const { lightMode = false } = options;
const includeShadowRoots = !lightMode;
const elementTagName = await getTagName(elementURL);
const elementInstance = await initializeCustomElement(elementURL);

const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots })
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);
const html = !lightMode && elementTagName ? `
const finalTree = await renderComponentRoots(elementTree);
const html = elementTagName ? `
<${elementTagName}>
${serialize(finalTree)}
</${elementTagName}>
Expand All @@ -151,18 +149,15 @@ async function renderToString(elementURL, options = {}) {
};
}

async function renderFromHTML(html, elements = [], options = {}) {
async function renderFromHTML(html, elements = []) {
definitions = [];

const { lightMode = false } = options;
const includeShadowRoots = !lightMode;

for (const url of elements) {
await initializeCustomElement(url);
}

const elementTree = getParse(html)(html);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);
const finalTree = await renderComponentRoots(elementTree);

return {
html: serialize(finalTree),
Expand Down
88 changes: 0 additions & 88 deletions test/cases/config-light-mode/config-light-mode.spec.js

This file was deleted.

43 changes: 0 additions & 43 deletions test/cases/config-light-mode/src/components/header.js

This file was deleted.

27 changes: 0 additions & 27 deletions test/cases/config-light-mode/src/components/navigation.js

This file was deleted.

25 changes: 0 additions & 25 deletions test/cases/config-light-mode/src/pages/index.js

This file was deleted.