Skip to content

Commit b164da7

Browse files
committed
feat(ssr-compiler): only do stuff to components
1 parent 132e2e9 commit b164da7

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

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

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,42 +81,56 @@ const visitors: Visitors = {
8181
state.trustedLwcIdentifiers.add(load);
8282
path.replaceWith(b.callExpression(load, [structuredClone(source)]));
8383
},
84-
ClassDeclaration(path, state) {
85-
const { node } = path;
86-
if (
87-
node?.superClass &&
88-
// export default class extends LightningElement {}
89-
(is.exportDefaultDeclaration(path.parentPath) ||
90-
// class Cmp extends LightningElement {}; export default Cmp
91-
path.scope
92-
?.getBinding(node.id.name)
93-
?.references.some((ref) => is.exportDefaultDeclaration(ref.parent)))
94-
) {
95-
// If it's a default-exported class with a superclass, then it's an LWC component!
96-
state.isLWC = true;
97-
if (node.id) {
98-
state.lwcClassName = node.id.name;
99-
} else {
100-
node.id = b.identifier('DefaultComponentName');
101-
state.lwcClassName = 'DefaultComponentName';
102-
}
84+
ClassDeclaration: {
85+
enter(path, state) {
86+
const { node } = path;
87+
if (
88+
node?.superClass &&
89+
// export default class extends LightningElement {}
90+
(is.exportDefaultDeclaration(path.parentPath) ||
91+
// class Cmp extends LightningElement {}; export default Cmp
92+
path.scope
93+
?.getBinding(node.id.name)
94+
?.references.some((ref) => is.exportDefaultDeclaration(ref.parent)))
95+
) {
96+
// If it's a default-exported class with a superclass, then it's an LWC component!
97+
state.isLWC = true;
98+
state.currentComponent = node;
99+
if (node.id) {
100+
state.lwcClassName = node.id.name;
101+
} else {
102+
node.id = b.identifier('DefaultComponentName');
103+
state.lwcClassName = 'DefaultComponentName';
104+
}
103105

104-
// There's no builder for comment nodes :\
105-
const lwcVersionComment: EsComment = {
106-
type: 'Block',
107-
value: LWC_VERSION_COMMENT,
108-
};
106+
// There's no builder for comment nodes :\
107+
const lwcVersionComment: EsComment = {
108+
type: 'Block',
109+
value: LWC_VERSION_COMMENT,
110+
};
109111

110-
// Add LWC version comment to end of class body
111-
const { body } = node;
112-
if (body.trailingComments) {
113-
body.trailingComments.push(lwcVersionComment);
114-
} else {
115-
body.trailingComments = [lwcVersionComment];
112+
// Add LWC version comment to end of class body
113+
const { body } = node;
114+
if (body.trailingComments) {
115+
body.trailingComments.push(lwcVersionComment);
116+
} else {
117+
body.trailingComments = [lwcVersionComment];
118+
}
116119
}
117-
}
120+
},
121+
leave(path, state) {
122+
// Indicate that we're no longer traversing an LWC component
123+
if (state.currentComponent && path.node === state.currentComponent) {
124+
state.currentComponent = null;
125+
}
126+
},
118127
},
119128
PropertyDefinition(path, state) {
129+
// Don't do anything unless we're in a component
130+
if (!state.currentComponent) {
131+
return;
132+
}
133+
120134
const node = path.node;
121135
if (!node?.key) {
122136
// Seems to occur for `@wire() [symbol];` -- not sure why
@@ -157,7 +171,7 @@ const visitors: Visitors = {
157171
}
158172
// If we mutate any class-methods that are piped through this compiler, then we'll be
159173
// inadvertently mutating things like Wire adapters.
160-
if (!state.isLWC) {
174+
if (!state.currentComponent) {
161175
return;
162176
}
163177

@@ -220,7 +234,7 @@ const visitors: Visitors = {
220234
Super(path, state) {
221235
// If we mutate any super calls that are piped through this compiler, then we'll be
222236
// inadvertently mutating things like Wire adapters.
223-
if (!state.isLWC) {
237+
if (!state.currentComponent) {
224238
return;
225239
}
226240

@@ -273,6 +287,7 @@ export default function compileJS(
273287

274288
const state: ComponentMetaState = {
275289
isLWC: false,
290+
currentComponent: null,
276291
hasConstructor: false,
277292
hasConnectedCallback: false,
278293
hadRenderedCallback: false,

packages/@lwc/ssr-compiler/src/compile-js/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { type traverse } from 'estree-toolkit';
99
import type { ImportManager } from '../imports';
1010
import type { ComponentTransformOptions } from '../shared';
1111
import type {
12+
ClassDeclaration,
1213
Identifier,
1314
MemberExpression,
1415
MethodDefinition,
@@ -26,8 +27,10 @@ export interface WireAdapter {
2627
}
2728

2829
export interface ComponentMetaState {
29-
/** indicates whether the LightningElement subclass is found in the JS being traversed */
30+
/** indicates whether a subclass of LightningElement is found in the JS being traversed */
3031
isLWC: boolean;
32+
/** the class declaration currently being traversed, if it is an LWC component */
33+
currentComponent: ClassDeclaration | null;
3134
/** indicates whether the LightningElement subclass includes a constructor method */
3235
hasConstructor: boolean;
3336
/** indicates whether the subclass has a connectedCallback method */

0 commit comments

Comments
 (0)