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

fix(ssr): handle export { Cmp as default } @W-17655297 #5176

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<x-cmp>
<x-parent>
<template shadowrootmode="open">
<x-shadow>
<template shadowrootmode="open">
</template>
</x-shadow>
<x-light>
</x-light>
</template>
</x-cmp>
</x-parent>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const tagName = 'x-cmp';
export { default } from 'x/cmp';
export * from 'x/cmp';
export const tagName = 'x-parent';
export { default } from 'x/parent';
export * from 'x/parent';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:render-mode="light">
This template isn't actually used because `export {Component as default}` isn't recognized as an LWC component.
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

class Light extends LightningElement {
static renderMode = 'light';
}

export { Light as default };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<x-shadow></x-shadow>
<x-light></x-light>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class Parent extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement } from 'lwc';

class Shadow extends LightningElement {}

export { Shadow as default };
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
export const expectedFailures = new Set([
'attribute-global-html/as-component-prop/undeclared/index.js',
'attribute-global-html/as-component-prop/without-@api/index.js',
'exports/component-as-default/index.js',
'known-boolean-attributes/default-def-html-attributes/static-on-component/index.js',
'wire/errors/throws-on-computed-key/index.js',
'wire/errors/throws-when-colliding-prop-then-method/index.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ const bYieldFromChildGenerator = esTemplateWithYield`
}

const scopeToken = hasScopedStylesheets ? stylesheetScopeToken : undefined;
const Ctor = ${/* Component */ is.identifier};
const generateMarkup = ${/* Component */ is.identifier}[__SYMBOL__GENERATE_MARKUP];

yield* Ctor[__SYMBOL__GENERATE_MARKUP](
${/* tag name */ is.literal},
childProps,
childAttrs,
shadowSlottedContent,
lightSlottedContentMap,
scopedSlottedContentMap,
instance,
scopeToken,
contextfulParent
);
if (!generateMarkup) {
yield __unimplementedTmpl(${/* tag name */ is.literal}, ${/* Component */ 3});
} else {
yield* generateMarkup(
${/* tag name */ 4},
childProps,
childAttrs,
shadowSlottedContent,
lightSlottedContentMap,
scopedSlottedContentMap,
instance,
scopeToken,
contextfulParent
);
}
}
`<EsBlockStatement>;

Expand All @@ -60,6 +64,7 @@ export const Component: Transformer<IrComponent> = function Component(node, cxt)
cxt.import({ default: childComponentLocalName }, importPath);
cxt.import({
SYMBOL__GENERATE_MARKUP: '__SYMBOL__GENERATE_MARKUP',
unimplementedTmpl: '__unimplementedTmpl',
});
const childTagName = node.name;

Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/ssr-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
serverSideRenderComponent,
// renderComponent is an alias for serverSideRenderComponent
serverSideRenderComponent as renderComponent,
unimplementedTmpl,
} from './render';
export { normalizeTextContent, renderTextContent } from './render-text-content';
export { hasScopedStaticStylesheets, renderStylesheets } from './styles';
Expand Down
24 changes: 23 additions & 1 deletion packages/@lwc/ssr-runtime/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ export function fallbackTmplNoYield(
}
}

/**
* If a component is incorrectly implemented, and is missing a `generateMarkup` function,
* then use this template as a fallback so the world doesn't explode.
* @example export { Cmp as default }
*/
export function unimplementedTmpl(tagName: string, Cmp?: LightningElementConstructor): string {
let html = `<${tagName}>`;
if (Cmp?.renderMode !== 'light') {
html += '<template shadowrootmode="open"></template>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be rendering the slotted content here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support

}
html += `</${tagName}>`;
return html;
}

export type GenerateMarkupFn = (
tagName: string,
props: Properties | null,
Expand Down Expand Up @@ -174,7 +188,7 @@ type GenerateMarkupFnVariants =
| GenerateMarkupFnAsyncNoGen
| GenerateMarkupFnSyncNoGen;

interface ComponentWithGenerateMarkup {
interface ComponentWithGenerateMarkup extends LightningElementConstructor {
[SYMBOL__GENERATE_MARKUP]: GenerateMarkupFnVariants;
}

Expand All @@ -195,6 +209,14 @@ export async function serverSideRenderComponent(
markup += segment;
};

if (!generateMarkup) {
// If a non-component is accidentally provided, render an empty template
emit(`<${tagName}>`);
fallbackTmplNoYield(emit, null, null, null, Component, null);
emit(`</${tagName}>`);
return markup;
}

if (mode === 'asyncYield') {
for await (const segment of (generateMarkup as GenerateMarkupFn)(
tagName,
Expand Down