Skip to content

Commit

Permalink
fix(dist-custom-elements): add ssr checks (#3131)
Browse files Browse the repository at this point in the history
the generated defineCustomElement function now checks if customElements is defined,
and exits if it is not.

HTMLElement is now imported from stencil if one or more components are found in a
module
  • Loading branch information
willmartian committed Nov 9, 2021
1 parent 5bfbf40 commit 9a232ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ const createCustomElementsDefineCase = (tagName: string, actionExpression: ts.Ex
* Add the main `defineCustomElement` function e.g.
* ```javascript
* function defineCustomElement() {
* if (typeof customElements === 'undefined') {
* return;
* }
* const components = ['my-component'];
* components.forEach(tagName => {
* switch (tagName) {
Expand Down Expand Up @@ -176,6 +179,13 @@ const addDefineCustomElementFunction = (
undefined,
ts.factory.createBlock(
[
ts.factory.createIfStatement(
ts.factory.createStrictEquality(
ts.factory.createTypeOfExpression(ts.factory.createIdentifier('customElements')),
ts.factory.createStringLiteral('undefined')
),
ts.factory.createBlock([ts.factory.createReturnStatement()])
),
ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const updateNativeHostComponentHeritageClauses = (classNode: ts.ClassDeclaration
return classNode.heritageClauses;
}

if (moduleFile.cmps.length > 1) {
if (moduleFile.cmps.length >= 1) {
addCoreRuntimeApi(moduleFile, RUNTIME_APIS.HTMLElement);
}

const heritageClause = ts.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.createExpressionWithTypeArguments([], ts.createIdentifier(HTML_ELEMENT)),
const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier(HTML_ELEMENT), []),
]);

return [heritageClause];
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/test/native-constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('nativeComponentTransform', () => {
const transpiledModule = transpileModule(code, null, compilerCtx, null, [], [transformer]);

expect(transpiledModule.outputText).toContain(
`import { attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
`import { HTMLElement, attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
);
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
});
Expand All @@ -62,7 +62,7 @@ describe('nativeComponentTransform', () => {
const transpiledModule = transpileModule(code, null, compilerCtx, null, [], [transformer]);

expect(transpiledModule.outputText).toContain(
`import { attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
`import { HTMLElement, attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
);
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
});
Expand Down

0 comments on commit 9a232ea

Please sign in to comment.