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 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<x-cmp>
<x-parent>
<template shadowrootmode="open">
<x-shadow>
<template shadowrootmode="open">
</template>
</x-shadow>
<x-shadow>
<template shadowrootmode="open">
</template>
<h1>
slotted content
</h1>
</x-shadow>
<x-light>
</x-light>
<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,6 @@
<template>
<x-shadow></x-shadow>
<x-shadow><h1>slotted content</h1></x-shadow>
<x-light></x-light>
<x-light><h1>slotted content</h1></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-when-colliding-prop-then-method/index.js',
]);
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@ const bYieldFromChildGenerator = esTemplateWithYield`
}

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

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

Expand All @@ -60,6 +63,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
3 changes: 3 additions & 0 deletions packages/@lwc/ssr-compiler/src/transmogrify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const visitors: Visitors = {
//
// - renderAttrs vs renderAttrsNoYield
// - fallbackTmpl vs fallbackTmplNoYield
// - unimplementedTmpl vs unimplementedTmplNoYield
//
// If this becomes too burdensome to maintain, we can officially deprecate the generator-based approach
// and switch the @lwc/ssr-runtime implementation wholesale over to the no-generator paradigm.
Expand All @@ -136,6 +137,8 @@ const visitors: Visitors = {
node.imported.name = 'fallbackTmplNoYield';
} else if (node.imported.name === 'renderAttrs') {
node.imported.name = 'renderAttrsNoYield';
} else if (node.imported.name === 'unimplementedTmpl') {
node.imported.name = 'unimplementedTmplNoYield';
}
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/@lwc/ssr-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export {
serverSideRenderComponent,
// renderComponent is an alias for serverSideRenderComponent
serverSideRenderComponent as renderComponent,
unimplementedTmpl,
unimplementedTmplNoYield,
} from './render';
export { normalizeTextContent, renderTextContent } from './render-text-content';
export { hasScopedStaticStylesheets, renderStylesheets } from './styles';
Expand Down
59 changes: 55 additions & 4 deletions packages/@lwc/ssr-runtime/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function* fallbackTmpl(
_lightSlottedContent: unknown,
_scopedSlottedContent: unknown,
Cmp: LightningElementConstructor,
instance: unknown
instance: LightningElement
) {
if (Cmp.renderMode !== 'light') {
yield `<template shadowrootmode="open"></template>`;
Expand All @@ -117,11 +117,11 @@ export function* fallbackTmpl(

export function fallbackTmplNoYield(
emit: (segment: string) => void,
shadowSlottedContent: AsyncGeneratorFunction,
shadowSlottedContent: AsyncGeneratorFunction | null,
_lightSlottedContent: unknown,
_scopedSlottedContent: unknown,
Cmp: LightningElementConstructor,
instance: unknown
instance: LightningElement | null
) {
if (Cmp.renderMode !== 'light') {
emit(`<template shadowrootmode="open"></template>`);
Expand All @@ -131,6 +131,49 @@ 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,
instance: LightningElement,
shadowSlottedContent: AsyncGeneratorFunction,
Cmp?: LightningElementConstructor
) {
yield `<${tagName}>`;
if (Cmp?.renderMode !== 'light') {
yield '<template shadowrootmode="open"></template>';
if (shadowSlottedContent) {
yield shadowSlottedContent(instance);
}
}
yield `</${tagName}>`;
}

/**
* 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 unimplementedTmplNoYield(
emit: (segment: string) => void,
tagName: string,
instance: LightningElement,
shadowSlottedContent: AsyncGeneratorFunction,
Cmp?: LightningElementConstructor
) {
emit(`<${tagName}>`);
if (Cmp?.renderMode !== 'light') {
emit('<template shadowrootmode="open"></template>');
if (shadowSlottedContent) {
shadowSlottedContent(emit, instance);
}
}
emit(`</${tagName}>`);
}

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

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

Expand All @@ -201,6 +244,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