Skip to content

Commit 132e2e9

Browse files
committed
feat(ssr-compiler): change __load to use trusted identifier __lwcLoad
1 parent 861705a commit 132e2e9

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ const visitors: Visitors = {
7474
return;
7575
}
7676
const source = path.node!.source!;
77-
// 1. insert `import { load as __load } from '${loader}'` at top of program
78-
importManager.add({ load: '__load' }, loader);
79-
// 2. replace this import with `__load(${source})`
80-
path.replaceWith(b.callExpression(b.identifier('__load'), [structuredClone(source)]));
77+
// 1. insert `import { load as __lwcLoad } from '${loader}'` at top of program
78+
importManager.add({ load: '__lwcLoad' }, loader);
79+
// 2. replace this `import(source)` with `__lwcLoad(source)`
80+
const load = b.identifier('__lwcLoad');
81+
state.trustedLwcIdentifiers.add(load);
82+
path.replaceWith(b.callExpression(load, [structuredClone(source)]));
8183
},
8284
ClassDeclaration(path, state) {
8385
const { node } = path;
@@ -246,9 +248,9 @@ const visitors: Visitors = {
246248
}
247249
},
248250
},
249-
Identifier(path, _state) {
251+
Identifier(path, state) {
250252
const { node } = path;
251-
if (node?.name.startsWith('__lwc')) {
253+
if (node?.name.startsWith('__lwc') && !state.trustedLwcIdentifiers.has(node)) {
252254
throw generateError(node, SsrCompilerErrors.RESERVED_IDENTIFIER_PREFIX);
253255
}
254256
},
@@ -286,6 +288,7 @@ export default function compileJS(
286288
wireAdapters: [],
287289
experimentalDynamicComponent: options.experimentalDynamicComponent,
288290
importManager: new ImportManager(),
291+
trustedLwcIdentifiers: new WeakSet(),
289292
};
290293

291294
traverse(ast, visitors, state);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ export interface ComponentMetaState {
5858
experimentalDynamicComponent: ComponentTransformOptions['experimentalDynamicComponent'];
5959
/** imports to add to the top of the program after parsing */
6060
importManager: ImportManager;
61+
/** identifiers starting with __lwc that we added */
62+
trustedLwcIdentifiers: WeakSet<Identifier>;
6163
}

0 commit comments

Comments
 (0)