Skip to content

Commit 9a232ea

Browse files
authored
fix(dist-custom-elements): add ssr checks (#3131)
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
1 parent 5bfbf40 commit 9a232ea

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/compiler/transformers/component-native/add-define-custom-element-function.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ const createCustomElementsDefineCase = (tagName: string, actionExpression: ts.Ex
143143
* Add the main `defineCustomElement` function e.g.
144144
* ```javascript
145145
* function defineCustomElement() {
146+
* if (typeof customElements === 'undefined') {
147+
* return;
148+
* }
146149
* const components = ['my-component'];
147150
* components.forEach(tagName => {
148151
* switch (tagName) {
@@ -176,6 +179,13 @@ const addDefineCustomElementFunction = (
176179
undefined,
177180
ts.factory.createBlock(
178181
[
182+
ts.factory.createIfStatement(
183+
ts.factory.createStrictEquality(
184+
ts.factory.createTypeOfExpression(ts.factory.createIdentifier('customElements')),
185+
ts.factory.createStringLiteral('undefined')
186+
),
187+
ts.factory.createBlock([ts.factory.createReturnStatement()])
188+
),
179189
ts.factory.createVariableStatement(
180190
undefined,
181191
ts.factory.createVariableDeclarationList(

src/compiler/transformers/component-native/native-component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ const updateNativeHostComponentHeritageClauses = (classNode: ts.ClassDeclaration
2727
return classNode.heritageClauses;
2828
}
2929

30-
if (moduleFile.cmps.length > 1) {
30+
if (moduleFile.cmps.length >= 1) {
3131
addCoreRuntimeApi(moduleFile, RUNTIME_APIS.HTMLElement);
3232
}
3333

34-
const heritageClause = ts.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
35-
ts.createExpressionWithTypeArguments([], ts.createIdentifier(HTML_ELEMENT)),
34+
const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
35+
ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier(HTML_ELEMENT), []),
3636
]);
3737

3838
return [heritageClause];

src/compiler/transformers/test/native-constructor.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('nativeComponentTransform', () => {
3737
const transpiledModule = transpileModule(code, null, compilerCtx, null, [], [transformer]);
3838

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

6464
expect(transpiledModule.outputText).toContain(
65-
`import { attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
65+
`import { HTMLElement, attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";`
6666
);
6767
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`);
6868
});

0 commit comments

Comments
 (0)