Skip to content

Commit

Permalink
refactor handling and logging for entry points and dependencies with …
Browse files Browse the repository at this point in the history
…no custom element export (#125)
  • Loading branch information
thescientist13 authored Dec 21, 2023
1 parent 17fb3a8 commit b29d981
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
34 changes: 19 additions & 15 deletions src/wcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ async function initializeCustomElement(elementURL, tagName, attrs = [], definiti
await elementInstance.connectedCallback();

return elementInstance;
} else {
console.debug('No custom element class found for this file');
return new HTMLElement();
}
}

Expand All @@ -169,18 +166,25 @@ async function renderToString(elementURL, wrappingEntryTag = true, props = {}) {
const elementTagName = wrappingEntryTag && await getTagName(elementURL);
const isEntry = !!elementTagName;
const elementInstance = await initializeCustomElement(elementURL, undefined, undefined, definitions, isEntry, props);

const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree, definitions);
const html = wrappingEntryTag && elementTagName ? `
<${elementTagName}>
${serialize(finalTree)}
</${elementTagName}>
`
: serialize(finalTree);
let html;

// in case the entry point isn't valid
if (elementInstance) {
const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree, definitions);

html = wrappingEntryTag && elementTagName ? `
<${elementTagName}>
${serialize(finalTree)}
</${elementTagName}>
`
: serialize(finalTree);
} else {
console.warn('WARNING: No custom element class found for this entry point.');
}

return {
html,
Expand Down
10 changes: 8 additions & 2 deletions test/cases/no-export/no-export.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ const expect = chai.expect;
describe('Run WCC For ', function() {
const LABEL = 'Single Custom Element with no default export';
let rawHtml;
let meta;

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

rawHtml = html;
meta = metadata;
});

describe(LABEL, function() {
it('should not throw an error', function() {
expect(rawHtml).to.equal('');
expect(rawHtml).to.equal(undefined);
});

it('should not have any definition', function() {
expect(meta.length).to.equal(0);
});
});

Expand Down

0 comments on commit b29d981

Please sign in to comment.