-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathgenerate-markup.ts
More file actions
155 lines (140 loc) · 6.07 KB
/
generate-markup.ts
File metadata and controls
155 lines (140 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { parse as pathParse } from 'node:path';
import { is, builders as b } from 'estree-toolkit';
import { esTemplate } from '../estemplate';
import { bImportDeclaration } from '../estree/builders';
import { bWireAdaptersPlumbing } from './wire';
import type { Program, Statement, IfStatement } from 'estree';
import type { ComponentMetaState } from './types';
const bGenerateMarkup = esTemplate`
// These variables may mix with component-authored variables, so should be reasonably unique
const __lwcPublicFields__ = new Set(${/*public fields*/ is.arrayExpression});
const __lwcPrivateFields__ = new Set(${/*private fields*/ is.arrayExpression});
async function* generateMarkup(
tagName,
props,
attrs,
shadowSlottedContent,
lightSlottedContent,
scopedSlottedContent,
parent,
scopeToken,
contextfulParent
) {
tagName = tagName ?? ${/*component tag name*/ is.literal};
attrs = attrs ?? Object.create(null);
props = props ?? Object.create(null);
const instance = new ${/* Component class */ is.identifier}({
tagName: tagName.toUpperCase(),
});
__establishContextfulRelationship(contextfulParent, instance);
${/*connect wire*/ is.statement}
instance[__SYMBOL__SET_INTERNALS](
props,
attrs,
__lwcPublicFields__,
__lwcPrivateFields__,
);
instance.isConnected = true;
if (instance.connectedCallback) {
__mutationTracker.enable(instance);
instance.connectedCallback();
__mutationTracker.disable(instance);
}
// If a render() function is defined on the class or any of its superclasses, then that takes priority.
// Next, if the class or any of its superclasses has an implicitly-associated template, then that takes
// second priority (e.g. a foo.html file alongside a foo.js file). Finally, there is a fallback empty template.
const tmplFn = instance.render?.() ?? ${/*component class*/ 3}[__SYMBOL__DEFAULT_TEMPLATE] ?? __fallbackTmpl;
yield \`<\${tagName}\`;
const hostHasScopedStylesheets =
tmplFn.hasScopedStylesheets ||
hasScopedStaticStylesheets(${/*component class*/ 3});
const hostScopeToken = hostHasScopedStylesheets ? tmplFn.stylesheetScopeToken + "-host" : undefined;
yield* __renderAttrs(instance, attrs, hostScopeToken, scopeToken);
yield '>';
yield* tmplFn(
shadowSlottedContent,
lightSlottedContent,
scopedSlottedContent,
${/*component class*/ 3},
instance
);
yield \`</\${tagName}>\`;
}
${/* component class */ 3}[__SYMBOL__GENERATE_MARKUP] = generateMarkup;
`<[Statement]>;
const bExposeTemplate = esTemplate`
if (${/*template*/ is.identifier}) {
${/* component class */ is.identifier}[__SYMBOL__DEFAULT_TEMPLATE] = ${/*template*/ 0}
}
`<IfStatement>;
/**
* This builds a generator function `generateMarkup` and adds it to the component JS's
* compilation output. `generateMarkup` acts as the glue between component JS and its
* template(s), including:
*
* - managing reflection of attrs & props
* - instantiating the component instance
* - setting the internal state of that component instance
* - invoking component lifecycle methods
* - yielding the tag name & attributes
* - deferring to the template function for yielding child content
*/
export function addGenerateMarkupFunction(
program: Program,
state: ComponentMetaState,
tagName: string,
filename: string
) {
const { privateFields, publicFields, tmplExplicitImports } = state;
// The default tag name represents the component name that's passed to the transformer.
// This is needed to generate markup for dynamic components which are invoked through
// the generateMarkup function on the constructor.
// At the time of generation, the invoker does not have reference to its tag name to pass as an argument.
const defaultTagName = b.literal(tagName);
const classIdentifier = b.identifier(state.lwcClassName!);
let exposeTemplateBlock: IfStatement | null = null;
if (!tmplExplicitImports) {
const defaultTmplPath = `./${pathParse(filename).name}.html`;
const tmplVar = b.identifier('tmpl');
program.body.unshift(bImportDeclaration({ default: tmplVar.name }, defaultTmplPath));
program.body.unshift(
bImportDeclaration({ SYMBOL__DEFAULT_TEMPLATE: '__SYMBOL__DEFAULT_TEMPLATE' })
);
exposeTemplateBlock = bExposeTemplate(tmplVar, classIdentifier);
}
// If no wire adapters are detected on the component, we don't bother injecting the wire-related code.
let connectWireAdapterCode: Statement[] = [];
if (state.wireAdapters.length) {
connectWireAdapterCode = bWireAdaptersPlumbing(state.wireAdapters);
program.body.unshift(bImportDeclaration({ connectContext: '__connectContext' }));
}
program.body.unshift(
bImportDeclaration({
fallbackTmpl: '__fallbackTmpl',
hasScopedStaticStylesheets: undefined,
mutationTracker: '__mutationTracker',
renderAttrs: '__renderAttrs',
SYMBOL__GENERATE_MARKUP: '__SYMBOL__GENERATE_MARKUP',
SYMBOL__SET_INTERNALS: '__SYMBOL__SET_INTERNALS',
establishContextfulRelationship: '__establishContextfulRelationship',
})
);
program.body.push(
...bGenerateMarkup(
b.arrayExpression(publicFields.map(b.literal)),
b.arrayExpression(privateFields.map(b.literal)),
defaultTagName,
classIdentifier,
connectWireAdapterCode
)
);
if (exposeTemplateBlock) {
program.body.push(exposeTemplateBlock);
}
}