Skip to content

Commit 8cb3174

Browse files
committed
chore: comments and some light cleanup
1 parent 580fc4c commit 8cb3174

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

packages/@lwc/ssr-compiler/src/compile-template/context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function createNewContext(templateOptions: TemplateOpts): {
4343
const previouslyHoistedStatementKeysTmpl = new Set<unknown>();
4444

4545
const hoist = {
46+
// Anything added here will be inserted at the top of the compiled template's
47+
// JS module.
4648
module(stmt: EsStatement, optionalDedupeKey?: unknown) {
4749
if (optionalDedupeKey) {
4850
if (previouslyHoistedStatementKeysMod.has(optionalDedupeKey)) {
@@ -52,6 +54,8 @@ export function createNewContext(templateOptions: TemplateOpts): {
5254
}
5355
hoistedStatements.module.push(stmt);
5456
},
57+
// Anything added here will be inserted at the top of the JavaScript function
58+
// corresponding to the template (typically named `__lwcTmpl`).
5559
templateFn(stmt: EsStatement, optionalDedupeKey?: unknown) {
5660
if (optionalDedupeKey) {
5761
if (previouslyHoistedStatementKeysTmpl.has(optionalDedupeKey)) {
@@ -66,6 +70,10 @@ export function createNewContext(templateOptions: TemplateOpts): {
6670
const shadowSlotToFnName = new Map<string, string>();
6771
let fnNameUniqueId = 0;
6872

73+
// At present, we only track shadow-slotted content. This is because the functions
74+
// corresponding to shadow-slotted content are deduped and hoisted to the top of
75+
// the template function, whereas light-dom-slotted content is inlined. It may be
76+
// desirable to also track light-dom-slotted content at some future point in time.
6977
const slots = {
7078
shadow: {
7179
isDuplicate(uniqueNodeId: string) {

packages/@lwc/ssr-compiler/src/compile-template/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ export default function compileTemplate(
126126
}
127127

128128
let tmplDecl = bExportTemplate(
129-
optimizeAdjacentYieldStmts([...cxt.hoistedStatements.templateFn, ...statements])
129+
optimizeAdjacentYieldStmts([
130+
// Deep in the compiler, we may choose to hoist statements and declarations
131+
// to the top of the template function. After `templateIrToEsTree`, these
132+
// hoisted statements/declarations are prepended to the template function's
133+
// body.
134+
...cxt.hoistedStatements.templateFn,
135+
...statements,
136+
])
130137
);
131138
// Ideally, we'd just do ${LWC_VERSION_COMMENT} in the code template,
132139
// but placeholders have a special meaning for `esTemplate`.
@@ -139,7 +146,18 @@ export default function compileTemplate(
139146
];
140147
});
141148

142-
let program = b.program([...getImports(), ...cxt.hoistedStatements.module, tmplDecl], 'module');
149+
let program = b.program(
150+
[
151+
// All import declarations come first...
152+
...getImports(),
153+
// ... followed by any statements or declarations that need to be hoisted
154+
// to the top of the module scope...
155+
...cxt.hoistedStatements.module,
156+
// ... followed by the template function declaration itself.
157+
tmplDecl,
158+
],
159+
'module'
160+
);
143161

144162
addScopeTokenDeclarations(program, filename, options.namespace, options.name);
145163

packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ import type {
3939
} from '@lwc/template-compiler';
4040
import type { TransformerContext } from '../../types';
4141

42+
// This function will be defined once and hoisted to the top of the template function. It'll be
43+
// referenced deeper in the call stack where the function is called or passed as a parameter.
44+
// It is a higher-order function that curries local variables that may be referenced by the
45+
// shadow slot content.
4246
const bGenerateShadowSlottedContent = esTemplateWithYield`
4347
const ${/* function name */ is.identifier} = (${/* local vars */ is.identifier}) => async function* ${/* function name */ 0}(contextfulParent) {
4448
// The 'contextfulParent' variable is shadowed here so that a contextful relationship
@@ -47,17 +51,20 @@ const bGenerateShadowSlottedContent = esTemplateWithYield`
4751
${/* shadow slot content */ is.statement}
4852
};
4953
`<EsVariableDeclaration>;
54+
// By passing in the set of local variables (which correspond 1:1 to the variables expected by
55+
// the referenced function), `shadowSlottedContent` will be curried function that can generate
56+
// shadow-slotted content.
5057
const bGenerateShadowSlottedContentRef = esTemplateWithYield`
5158
const shadowSlottedContent = ${/* reference to hoisted fn */ is.identifier}(${/* local vars */ is.identifier});
5259
`<EsVariableDeclaration>;
5360
const bNullishGenerateShadowSlottedContent = esTemplateWithYield`
5461
const shadowSlottedContent = null;
5562
`<EsVariableDeclaration>;
5663

57-
const bContentMap = esTemplateWithYield`
64+
const blightSlottedContentMap = esTemplateWithYield`
5865
const ${/* name of the content map */ is.identifier} = Object.create(null);
5966
`<EsVariableDeclaration>;
60-
const bNullishContentMap = esTemplateWithYield`
67+
const bNullishLightSlottedContentMap = esTemplateWithYield`
6168
const ${/* name of the content map */ is.identifier} = null;
6269
`<EsVariableDeclaration>;
6370

@@ -298,11 +305,11 @@ export function getSlottedContent(
298305
)
299306
: bNullishGenerateShadowSlottedContent();
300307
const lightSlottedContentMap = hasLightSlottedContent
301-
? bContentMap(b.identifier('lightSlottedContentMap'))
302-
: bNullishContentMap(b.identifier('lightSlottedContentMap'));
308+
? blightSlottedContentMap(b.identifier('lightSlottedContentMap'))
309+
: bNullishLightSlottedContentMap(b.identifier('lightSlottedContentMap'));
303310
const scopedSlottedContentMap = hasScopedSlottedContent
304-
? bContentMap(b.identifier('scopedSlottedContentMap'))
305-
: bNullishContentMap(b.identifier('scopedSlottedContentMap'));
311+
? blightSlottedContentMap(b.identifier('scopedSlottedContentMap'))
312+
: bNullishLightSlottedContentMap(b.identifier('scopedSlottedContentMap'));
306313

307314
return bGenerateSlottedContent(
308315
shadowSlottedContentFn,

0 commit comments

Comments
 (0)