Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/issue 24 refactor definitions sharing #70

Merged
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
143 changes: 141 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"http-server": "^14.1.0",
"jsdom": "^19.0.0",
"mocha": "^9.2.2",
"node-fetch": "^3.2.6",
"nodemon": "^2.0.15",
"prismjs": "^1.28.0",
"rehype-autolink-headings": "^6.1.1",
Expand Down
31 changes: 14 additions & 17 deletions src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import * as walk from 'acorn-walk';
import { parse, parseFragment, serialize } from 'parse5';
import fs from 'fs/promises';

let definitions;

function getParse(html) {
return html.indexOf('<html>') >= 0 || html.indexOf('<body>') >= 0 || html.indexOf('<head>') >= 0
? parse
Expand All @@ -22,12 +20,12 @@ function isCustomElementDefinitionNode(node) {
&& expression.callee.property.name === 'define';
}

async function renderComponentRoots(tree) {
async function renderComponentRoots(tree, definitions) {
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 elementInstance = await initializeCustomElement(moduleURL, tagName, node.attrs, definitions);
const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
Expand All @@ -39,19 +37,19 @@ async function renderComponentRoots(tree) {
}

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

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

return tree;
}

async function registerDependencies(moduleURL) {
async function registerDependencies(moduleURL, definitions) {
const moduleContents = await fs.readFile(moduleURL, 'utf-8');

walk.simple(acorn.parse(moduleContents, {
Expand All @@ -65,7 +63,7 @@ async function registerDependencies(moduleURL) {
if (!isBareSpecifier) {
const dependencyModuleURL = new URL(node.source.value, moduleURL);

await registerDependencies(dependencyModuleURL);
await registerDependencies(dependencyModuleURL, definitions);
}
},
async ExpressionStatement(node) {
Expand Down Expand Up @@ -101,8 +99,8 @@ async function getTagName(moduleURL) {
return tagName;
}

async function initializeCustomElement(elementURL, tagName, attrs = []) {
await registerDependencies(elementURL);
async function initializeCustomElement(elementURL, tagName, attrs = [], definitions = []) {
await registerDependencies(elementURL, definitions);

// https://github.com/ProjectEvergreen/wcc/pull/67/files#r902061804
const { pathname } = elementURL;
Expand All @@ -127,16 +125,15 @@ async function initializeCustomElement(elementURL, tagName, attrs = []) {
}

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

const definitions = [];
const elementTagName = await getTagName(elementURL);
const elementInstance = await initializeCustomElement(elementURL);
const elementInstance = await initializeCustomElement(elementURL, undefined, undefined, definitions);

const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree);
const finalTree = await renderComponentRoots(elementTree, definitions);
const html = elementTagName ? `
<${elementTagName}>
${serialize(finalTree)}
Expand All @@ -151,14 +148,14 @@ async function renderToString(elementURL) {
}

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

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

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

return {
html: serialize(finalTree),
Expand Down
33 changes: 33 additions & 0 deletions test/cases/node-modules/node-modules.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Use Case
* Run wcc against a custom element using a dependency from node-modules
*
* User Result
* Should run without error.
*
* User Workspace
* src/
* components/
* events-list.js
* index.js
*/
import { expect } from 'chai';
import { JSDOM } from 'jsdom';
import { renderToString } from '../../../src/wcc.js';

describe('Run WCC For ', function() {
const LABEL = 'Custom Element w/ a node modules dependency';
let dom;

before(async function() {
const { html } = await renderToString(new URL('./src/index.js', import.meta.url));

dom = new JSDOM(html);
});

describe(LABEL, function() {
it('should not fail when a node module is imported in a custom element', function() {
expect(dom).to.not.be.undefined;
});
});
});
18 changes: 18 additions & 0 deletions test/cases/node-modules/src/components/events-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fetch from 'node-fetch';

class EventsList extends HTMLElement {
async connectedCallback() {
if (!this.shadowRoot) {
const events = await fetch('http://www.analogstudios.net/api/v2/events').then(resp => resp.json());

this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `<h1>Events List (${events.length})</h1>`;
}
}
}

export {
EventsList
};

customElements.define('wc-events-list', EventsList);
Loading