From 40f58f05ca1e368050decd8fb4209f791f307768 Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Tue, 10 Dec 2024 15:50:19 -0700 Subject: [PATCH 1/8] fix: replace barrel from `lwc` package with '@lwc/ssr-runtime' --- .../fixtures/barrel-export-lwc/error.txt | 0 .../fixtures/barrel-export-lwc/expected.html | 4 +++ .../fixtures/barrel-export-lwc/imports.js | 14 +++++++++ .../fixtures/barrel-export-lwc/index.js | 3 ++ .../barrel-export-lwc/modules/x/cmp/cmp.html | 2 ++ .../barrel-export-lwc/modules/x/cmp/cmp.js | 31 +++++++++++++++++++ .../@lwc/ssr-compiler/src/compile-js/index.ts | 5 ++- .../ssr-compiler/src/compile-js/lwc-import.ts | 19 +++++++++++- 8 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/error.txt create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/error.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html new file mode 100644 index 0000000000..ce996e78e1 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js new file mode 100644 index 0000000000..41c93b8535 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js @@ -0,0 +1,14 @@ +// Barrel exporting from 'lwc' should work +// These imports are the ones that are allowed by the no-disallowed-lwc-imports eslint rule +// Ref: https://github.com/salesforce/eslint-plugin-lwc/blob/34911de749e20cabbf48f5585c92a4b62d082a41/lib/rules/no-disallowed-lwc-imports.js#L11 +export { + LightningElement, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, +} from 'lwc'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js new file mode 100644 index 0000000000..55d4302e11 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js @@ -0,0 +1,3 @@ +export const tagName = 'x-cmp'; +export { default } from 'x/cmp'; +export * from 'x/cmp'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html new file mode 100644 index 0000000000..6beff5199f --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html @@ -0,0 +1,2 @@ + diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js new file mode 100644 index 0000000000..2ab54b700a --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js @@ -0,0 +1,31 @@ +// Everything else can be imported in a helper, so we must check in the helper +import { + LightningElement, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, +} from '../../../imports.js'; + +// "Using" the imports so they don't get removed by the compiler +console.log( + LightningElement, + // The LWC compiler doesn't let us use decorators like this, so we don't need to check them here + // api, + // track, + // wire, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent +); + +export default class extends LightningElement {} diff --git a/packages/@lwc/ssr-compiler/src/compile-js/index.ts b/packages/@lwc/ssr-compiler/src/compile-js/index.ts index a48b2246e0..adaa8394cb 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/index.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/index.ts @@ -10,7 +10,7 @@ import { traverse, builders as b, is } from 'estree-toolkit'; import { parseModule } from 'meriyah'; import { transmogrify } from '../transmogrify'; -import { replaceLwcImport } from './lwc-import'; +import { replaceLwcImport, replaceNamedLwcImport } from './lwc-import'; import { catalogTmplImport } from './catalog-tmpls'; import { catalogStaticStylesheets, catalogAndReplaceStyleImports } from './stylesheets'; import { addGenerateMarkupFunction } from './generate-markup'; @@ -23,6 +23,9 @@ import type { CompilationMode } from '@lwc/shared'; const visitors: Visitors = { $: { scope: true }, + ExportNamedDeclaration(path) { + replaceNamedLwcImport(path); + }, ImportDeclaration(path, state) { if (!path.node || !path.node.source.value || typeof path.node.source.value !== 'string') { return; diff --git a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts index 5306559ff2..e8dd767b7d 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts @@ -7,7 +7,7 @@ import { builders as b } from 'estree-toolkit'; -import type { ImportDeclaration } from 'estree'; +import type { ImportDeclaration, ExportNamedDeclaration } from 'estree'; import type { NodePath } from 'estree-toolkit'; import type { ComponentMetaState } from './types'; @@ -35,3 +35,20 @@ export function replaceLwcImport(path: NodePath, state: Compo path.replaceWith(b.importDeclaration(path.node.specifiers, b.literal('@lwc/ssr-runtime'))); } + +/** + * This handles lwc barrel exports by replacing "lwc" with "@lwc/ssr-runtime" + */ +export function replaceNamedLwcImport(path: NodePath) { + if (!path.node || path.node.source?.value !== 'lwc') { + return; + } + + path.replaceWith( + b.exportNamedDeclaration( + path.node.declaration, + path.node.specifiers, + b.literal('@lwc/ssr-runtime') + ) + ); +} From 8777a1d1259fa4b96fbde4dc3df16801e8521fb1 Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Tue, 10 Dec 2024 17:45:57 -0700 Subject: [PATCH 2/8] fix: handle * barrel case and corresponding tests --- .../fixtures/barrel-export-lwc-all/barrel.js | 4 +++ .../error.txt | 0 .../expected.html | 0 .../index.js | 0 .../modules/x/cmp/cmp.html | 0 .../modules/x/cmp/cmp.js | 4 +++ .../barrel.js} | 0 .../barrel-export-lwc-named/error.txt | 0 .../barrel-export-lwc-named/expected.html | 4 +++ .../fixtures/barrel-export-lwc-named/index.js | 3 +++ .../modules/x/cmp/cmp.html | 2 ++ .../modules/x/cmp/cmp.js | 2 +- .../@lwc/ssr-compiler/src/compile-js/index.ts | 5 +++- .../ssr-compiler/src/compile-js/lwc-import.ts | 26 ++++++++++++++++--- 14 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc => barrel-export-lwc-all}/error.txt (100%) rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc => barrel-export-lwc-all}/expected.html (100%) rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc => barrel-export-lwc-all}/index.js (100%) rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc => barrel-export-lwc-all}/modules/x/cmp/cmp.html (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc/imports.js => barrel-export-lwc-named/barrel.js} (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/error.txt create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc => barrel-export-lwc-named}/modules/x/cmp/cmp.js (96%) diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js new file mode 100644 index 0000000000..29b8bfc209 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js @@ -0,0 +1,4 @@ +// Barrel exporting from 'lwc' should work +// These imports are the ones that are allowed by the no-disallowed-lwc-imports eslint rule +// Ref: https://github.com/salesforce/eslint-plugin-lwc/blob/34911de749e20cabbf48f5585c92a4b62d082a41/lib/rules/no-disallowed-lwc-imports.js#L11 +export * from 'lwc'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/error.txt similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/error.txt rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/error.txt diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/expected.html rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/index.js similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/index.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/index.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.html rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js new file mode 100644 index 0000000000..b06c287855 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js @@ -0,0 +1,4 @@ +// Everything else can be imported in a helper, so we must check in the helper +import { LightningElement } from '../../../barrel.js'; + +export default class extends LightningElement {} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/barrel.js similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/imports.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/barrel.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/error.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html new file mode 100644 index 0000000000..ce996e78e1 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js new file mode 100644 index 0000000000..55d4302e11 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js @@ -0,0 +1,3 @@ +export const tagName = 'x-cmp'; +export { default } from 'x/cmp'; +export * from 'x/cmp'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html new file mode 100644 index 0000000000..6beff5199f --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html @@ -0,0 +1,2 @@ + diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js similarity index 96% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js index 2ab54b700a..cc94d2a3a9 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc/modules/x/cmp/cmp.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js @@ -9,7 +9,7 @@ import { unwrap, createElement, renderComponent, -} from '../../../imports.js'; +} from '../../../barrel.js'; // "Using" the imports so they don't get removed by the compiler console.log( diff --git a/packages/@lwc/ssr-compiler/src/compile-js/index.ts b/packages/@lwc/ssr-compiler/src/compile-js/index.ts index adaa8394cb..63f70e1167 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/index.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/index.ts @@ -10,7 +10,7 @@ import { traverse, builders as b, is } from 'estree-toolkit'; import { parseModule } from 'meriyah'; import { transmogrify } from '../transmogrify'; -import { replaceLwcImport, replaceNamedLwcImport } from './lwc-import'; +import { replaceLwcImport, replaceNamedLwcImport, replaceAllLwcImport } from './lwc-import'; import { catalogTmplImport } from './catalog-tmpls'; import { catalogStaticStylesheets, catalogAndReplaceStyleImports } from './stylesheets'; import { addGenerateMarkupFunction } from './generate-markup'; @@ -26,6 +26,9 @@ const visitors: Visitors = { ExportNamedDeclaration(path) { replaceNamedLwcImport(path); }, + ExportAllDeclaration(path) { + replaceAllLwcImport(path); + }, ImportDeclaration(path, state) { if (!path.node || !path.node.source.value || typeof path.node.source.value !== 'string') { return; diff --git a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts index e8dd767b7d..606db6b8cc 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts @@ -7,7 +7,7 @@ import { builders as b } from 'estree-toolkit'; -import type { ImportDeclaration, ExportNamedDeclaration } from 'estree'; +import type { ImportDeclaration, ExportNamedDeclaration, ExportAllDeclaration } from 'estree'; import type { NodePath } from 'estree-toolkit'; import type { ComponentMetaState } from './types'; @@ -18,7 +18,7 @@ import type { ComponentMetaState } from './types'; * 2. it makes note of the local var name associated with the `LightningElement` import */ export function replaceLwcImport(path: NodePath, state: ComponentMetaState) { - if (!path.node || path.node.source.value !== 'lwc') { + if (!path.node || !isLwcSource(path)) { return; } @@ -40,7 +40,7 @@ export function replaceLwcImport(path: NodePath, state: Compo * This handles lwc barrel exports by replacing "lwc" with "@lwc/ssr-runtime" */ export function replaceNamedLwcImport(path: NodePath) { - if (!path.node || path.node.source?.value !== 'lwc') { + if (!path.node || !isLwcSource(path)) { return; } @@ -52,3 +52,23 @@ export function replaceNamedLwcImport(path: NodePath) { ) ); } + +/** + * This handles all lwc barrel exports by replacing "lwc" with "@lwc/ssr-runtime" + */ +export function replaceAllLwcImport(path: NodePath) { + if (!path.node || !isLwcSource(path)) { + return; + } + + path.replaceWith(b.exportAllDeclaration(b.literal('@lwc/ssr-runtime'))); +} + +/** + * Utility to determine if a node source is 'lwc' + */ +function isLwcSource( + path: NodePath +): boolean { + return path.node?.source?.value === 'lwc'; +} From be6a08b461122de848b09698dac47a2a4e1b18aa Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Tue, 10 Dec 2024 17:49:23 -0700 Subject: [PATCH 3/8] fix: function naming --- packages/@lwc/ssr-compiler/src/compile-js/index.ts | 6 +++--- packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/@lwc/ssr-compiler/src/compile-js/index.ts b/packages/@lwc/ssr-compiler/src/compile-js/index.ts index 63f70e1167..d59d3f2ac3 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/index.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/index.ts @@ -10,7 +10,7 @@ import { traverse, builders as b, is } from 'estree-toolkit'; import { parseModule } from 'meriyah'; import { transmogrify } from '../transmogrify'; -import { replaceLwcImport, replaceNamedLwcImport, replaceAllLwcImport } from './lwc-import'; +import { replaceLwcImport, replaceNamedLwcExport, replaceAllLwcExport } from './lwc-import'; import { catalogTmplImport } from './catalog-tmpls'; import { catalogStaticStylesheets, catalogAndReplaceStyleImports } from './stylesheets'; import { addGenerateMarkupFunction } from './generate-markup'; @@ -24,10 +24,10 @@ import type { CompilationMode } from '@lwc/shared'; const visitors: Visitors = { $: { scope: true }, ExportNamedDeclaration(path) { - replaceNamedLwcImport(path); + replaceNamedLwcExport(path); }, ExportAllDeclaration(path) { - replaceAllLwcImport(path); + replaceAllLwcExport(path); }, ImportDeclaration(path, state) { if (!path.node || !path.node.source.value || typeof path.node.source.value !== 'string') { diff --git a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts index 606db6b8cc..e7b580db7d 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts @@ -39,7 +39,7 @@ export function replaceLwcImport(path: NodePath, state: Compo /** * This handles lwc barrel exports by replacing "lwc" with "@lwc/ssr-runtime" */ -export function replaceNamedLwcImport(path: NodePath) { +export function replaceNamedLwcExport(path: NodePath) { if (!path.node || !isLwcSource(path)) { return; } @@ -56,7 +56,7 @@ export function replaceNamedLwcImport(path: NodePath) { /** * This handles all lwc barrel exports by replacing "lwc" with "@lwc/ssr-runtime" */ -export function replaceAllLwcImport(path: NodePath) { +export function replaceAllLwcExport(path: NodePath) { if (!path.node || !isLwcSource(path)) { return; } From 87eb3c51382696b1b5a04e44fcef7a71bc28739c Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Tue, 10 Dec 2024 17:57:25 -0700 Subject: [PATCH 4/8] fix: barrel import test parity --- .../modules/x/cmp/cmp.js | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js index b06c287855..cc94d2a3a9 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js @@ -1,4 +1,31 @@ // Everything else can be imported in a helper, so we must check in the helper -import { LightningElement } from '../../../barrel.js'; +import { + LightningElement, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, +} from '../../../barrel.js'; + +// "Using" the imports so they don't get removed by the compiler +console.log( + LightningElement, + // The LWC compiler doesn't let us use decorators like this, so we don't need to check them here + // api, + // track, + // wire, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent +); export default class extends LightningElement {} From ed28cf114342361454255afa09e8906011b09e7b Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Wed, 11 Dec 2024 16:57:11 -0700 Subject: [PATCH 5/8] fix: include optional exported alias for export all declaration replacement, tests --- .../fixtures/barrel-export-lwc-all/barrel.js | 4 --- .../barrel-export-lwc-all/expected.html | 4 --- .../modules/x/cmp/cmp.html | 2 -- .../modules/x/cmp/cmp.js | 31 ------------------- .../barrel-export-lwc-named/expected.html | 4 --- .../modules/x/cmp/cmp.html | 2 -- .../modules/x/cmp/cmp.js | 31 ------------------- .../export-all-alias/barrel.js | 2 ++ .../export-all-alias}/error.txt | 0 .../export-all-alias/expected.html | 14 +++++++++ .../export-all-alias}/index.js | 0 .../export-all-alias/modules/x/cmp/cmp.html | 3 ++ .../export-all-alias/modules/x/cmp/cmp.js | 15 +++++++++ .../barrel-lwc-exports/export-all/barrel.js | 2 ++ .../export-all}/error.txt | 0 .../export-all/expected.html | 14 +++++++++ .../export-all}/index.js | 0 .../export-all/modules/x/cmp/cmp.html | 3 ++ .../export-all/modules/x/cmp/cmp.js | 25 +++++++++++++++ .../export-named}/barrel.js | 2 +- .../barrel-lwc-exports/export-named/error.txt | 0 .../export-named/expected.html | 14 +++++++++ .../barrel-lwc-exports/export-named/index.js | 3 ++ .../export-named/modules/x/cmp/cmp.html | 3 ++ .../export-named/modules/x/cmp/cmp.js | 25 +++++++++++++++ .../fixtures/barrel-lwc-exports/shared.js | 10 ++++++ .../ssr-compiler/src/compile-js/lwc-import.ts | 2 +- 27 files changed, 135 insertions(+), 80 deletions(-) delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/barrel.js rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc-all => barrel-lwc-exports/export-all-alias}/error.txt (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc-all => barrel-lwc-exports/export-all-alias}/index.js (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/barrel.js rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc-named => barrel-lwc-exports/export-all}/error.txt (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc-named => barrel-lwc-exports/export-all}/index.js (100%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js rename packages/@lwc/engine-server/src/__tests__/fixtures/{barrel-export-lwc-named => barrel-lwc-exports/export-named}/barrel.js (91%) create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/error.txt create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/index.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.html create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js create mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js deleted file mode 100644 index 29b8bfc209..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/barrel.js +++ /dev/null @@ -1,4 +0,0 @@ -// Barrel exporting from 'lwc' should work -// These imports are the ones that are allowed by the no-disallowed-lwc-imports eslint rule -// Ref: https://github.com/salesforce/eslint-plugin-lwc/blob/34911de749e20cabbf48f5585c92a4b62d082a41/lib/rules/no-disallowed-lwc-imports.js#L11 -export * from 'lwc'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html deleted file mode 100644 index ce996e78e1..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/expected.html +++ /dev/null @@ -1,4 +0,0 @@ - - - \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html deleted file mode 100644 index 6beff5199f..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.html +++ /dev/null @@ -1,2 +0,0 @@ - diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js deleted file mode 100644 index cc94d2a3a9..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/modules/x/cmp/cmp.js +++ /dev/null @@ -1,31 +0,0 @@ -// Everything else can be imported in a helper, so we must check in the helper -import { - LightningElement, - getComponentDef, - isComponentConstructor, - createContextProvider, - readonly, - setFeatureFlagForTest, - unwrap, - createElement, - renderComponent, -} from '../../../barrel.js'; - -// "Using" the imports so they don't get removed by the compiler -console.log( - LightningElement, - // The LWC compiler doesn't let us use decorators like this, so we don't need to check them here - // api, - // track, - // wire, - getComponentDef, - isComponentConstructor, - createContextProvider, - readonly, - setFeatureFlagForTest, - unwrap, - createElement, - renderComponent -); - -export default class extends LightningElement {} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html deleted file mode 100644 index ce996e78e1..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/expected.html +++ /dev/null @@ -1,4 +0,0 @@ - - - \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html deleted file mode 100644 index 6beff5199f..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.html +++ /dev/null @@ -1,2 +0,0 @@ - diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js deleted file mode 100644 index cc94d2a3a9..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/modules/x/cmp/cmp.js +++ /dev/null @@ -1,31 +0,0 @@ -// Everything else can be imported in a helper, so we must check in the helper -import { - LightningElement, - getComponentDef, - isComponentConstructor, - createContextProvider, - readonly, - setFeatureFlagForTest, - unwrap, - createElement, - renderComponent, -} from '../../../barrel.js'; - -// "Using" the imports so they don't get removed by the compiler -console.log( - LightningElement, - // The LWC compiler doesn't let us use decorators like this, so we don't need to check them here - // api, - // track, - // wire, - getComponentDef, - isComponentConstructor, - createContextProvider, - readonly, - setFeatureFlagForTest, - unwrap, - createElement, - renderComponent -); - -export default class extends LightningElement {} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/barrel.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/barrel.js new file mode 100644 index 0000000000..a2b4556278 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/barrel.js @@ -0,0 +1,2 @@ +// Barrel exporting from 'lwc' with alias should work +export * as foo from 'lwc'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/error.txt similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/error.txt rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/error.txt diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html new file mode 100644 index 0000000000..4617344ea6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html @@ -0,0 +1,14 @@ + + + \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/index.js similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-all/index.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/index.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.html new file mode 100644 index 0000000000..561c4b0cb6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.html @@ -0,0 +1,3 @@ + diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js new file mode 100644 index 0000000000..5eda612fa8 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js @@ -0,0 +1,15 @@ +import { foo } from '../../../barrel.js'; +import { importsToString } from '../../../../shared.js'; + +export default class extends foo.LightningElement { + imports = importsToString([ + foo.getComponentDef, + foo.isComponentConstructor, + foo.createContextProvider, + foo.readonly, + foo.setFeatureFlagForTest, + foo.unwrap, + foo.createElement, + foo.renderComponent, + ]); +} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/barrel.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/barrel.js new file mode 100644 index 0000000000..a81633ca19 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/barrel.js @@ -0,0 +1,2 @@ +// Barrel exporting from 'lwc' should work +export * from 'lwc'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/error.txt similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/error.txt rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/error.txt diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html new file mode 100644 index 0000000000..4617344ea6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html @@ -0,0 +1,14 @@ + + + \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/index.js similarity index 100% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/index.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/index.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.html new file mode 100644 index 0000000000..561c4b0cb6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.html @@ -0,0 +1,3 @@ + diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js new file mode 100644 index 0000000000..5387769133 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js @@ -0,0 +1,25 @@ +import { + LightningElement, + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, +} from '../../../barrel.js'; +import { importsToString } from '../../../../shared.js'; + +export default class extends LightningElement { + imports = importsToString([ + getComponentDef, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, + ]); +} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/barrel.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/barrel.js similarity index 91% rename from packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/barrel.js rename to packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/barrel.js index 41c93b8535..14d4be8c94 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-export-lwc-named/barrel.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/barrel.js @@ -3,7 +3,7 @@ // Ref: https://github.com/salesforce/eslint-plugin-lwc/blob/34911de749e20cabbf48f5585c92a4b62d082a41/lib/rules/no-disallowed-lwc-imports.js#L11 export { LightningElement, - getComponentDef, + getComponentDef as getComponentDefAlias, isComponentConstructor, createContextProvider, readonly, diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/error.txt b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/error.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html new file mode 100644 index 0000000000..4617344ea6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html @@ -0,0 +1,14 @@ + + + \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/index.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/index.js new file mode 100644 index 0000000000..55d4302e11 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/index.js @@ -0,0 +1,3 @@ +export const tagName = 'x-cmp'; +export { default } from 'x/cmp'; +export * from 'x/cmp'; diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.html new file mode 100644 index 0000000000..561c4b0cb6 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.html @@ -0,0 +1,3 @@ + diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js new file mode 100644 index 0000000000..854fbc9644 --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js @@ -0,0 +1,25 @@ +import { + LightningElement, + getComponentDefAlias, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, +} from '../../../barrel.js'; +import { importsToString } from '../../../../shared.js'; + +export default class extends LightningElement { + imports = importsToString([ + getComponentDefAlias, + isComponentConstructor, + createContextProvider, + readonly, + setFeatureFlagForTest, + unwrap, + createElement, + renderComponent, + ]); +} diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js new file mode 100644 index 0000000000..0125ba030f --- /dev/null +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js @@ -0,0 +1,10 @@ +export function importsToString(imports) { + return imports + .map((i) => + i + .toString() + .split('(')[0] + .replace('async function serverSideRenderComponent', 'function renderComponent') + ) + .join('\n '); +} diff --git a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts index e7b580db7d..92e9d9d663 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts @@ -61,7 +61,7 @@ export function replaceAllLwcExport(path: NodePath) { return; } - path.replaceWith(b.exportAllDeclaration(b.literal('@lwc/ssr-runtime'))); + path.replaceWith(b.exportAllDeclaration(b.literal('@lwc/ssr-runtime'), path.node.exported)); } /** From 379453554ff20ba4bbf1b5d0234a19779103c599 Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Wed, 11 Dec 2024 17:13:23 -0700 Subject: [PATCH 6/8] chore: explain function name massaging in test --- .../src/__tests__/fixtures/barrel-lwc-exports/shared.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js index 0125ba030f..a1bfb5d28e 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js @@ -4,6 +4,7 @@ export function importsToString(imports) { i .toString() .split('(')[0] + // renderComponent is aliased here: https://github.com/salesforce/lwc/blob/5d01843a7733a03b9ccb59a70ad64af955f15b88/packages/%40lwc/ssr-runtime/src/index.ts#L31 .replace('async function serverSideRenderComponent', 'function renderComponent') ) .join('\n '); From 4b733da982cecfd85a924907508d03873ded2c68 Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Mon, 16 Dec 2024 14:53:56 -0700 Subject: [PATCH 7/8] fix: deep clone objects and optimize tests --- .../export-all-alias/expected.html | 16 ++++++++-------- .../export-all-alias/modules/x/cmp/cmp.js | 8 +++++--- .../barrel-lwc-exports/export-all/expected.html | 16 ++++++++-------- .../export-all/modules/x/cmp/cmp.js | 8 +++++--- .../export-named/expected.html | 16 ++++++++-------- .../export-named/modules/x/cmp/cmp.js | 8 +++++--- .../ssr-compiler/src/compile-js/lwc-import.ts | 12 ++++++++---- 7 files changed, 47 insertions(+), 37 deletions(-) diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html index 4617344ea6..ffea7153d0 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/expected.html @@ -1,14 +1,14 @@ \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js index 5eda612fa8..5c73035674 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all-alias/modules/x/cmp/cmp.js @@ -1,8 +1,7 @@ import { foo } from '../../../barrel.js'; -import { importsToString } from '../../../../shared.js'; export default class extends foo.LightningElement { - imports = importsToString([ + imports = [ foo.getComponentDef, foo.isComponentConstructor, foo.createContextProvider, @@ -11,5 +10,8 @@ export default class extends foo.LightningElement { foo.unwrap, foo.createElement, foo.renderComponent, - ]); + ] + // renderComponent is aliased here: https://github.com/salesforce/lwc/blob/5d01843a7733a03b9ccb59a70ad64af955f15b88/packages/%40lwc/ssr-runtime/src/index.ts#L31 + .map((i) => (i.name === 'serverSideRenderComponent' ? 'renderComponent' : i.name)) + .join('\n '); } diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html index 4617344ea6..ffea7153d0 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/expected.html @@ -1,14 +1,14 @@ \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js index 5387769133..62eca758a8 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-all/modules/x/cmp/cmp.js @@ -9,10 +9,9 @@ import { createElement, renderComponent, } from '../../../barrel.js'; -import { importsToString } from '../../../../shared.js'; export default class extends LightningElement { - imports = importsToString([ + imports = [ getComponentDef, isComponentConstructor, createContextProvider, @@ -21,5 +20,8 @@ export default class extends LightningElement { unwrap, createElement, renderComponent, - ]); + ] + // renderComponent is aliased here: https://github.com/salesforce/lwc/blob/5d01843a7733a03b9ccb59a70ad64af955f15b88/packages/%40lwc/ssr-runtime/src/index.ts#L31 + .map((i) => (i.name === 'serverSideRenderComponent' ? 'renderComponent' : i.name)) + .join('\n '); } diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html index 4617344ea6..ffea7153d0 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/expected.html @@ -1,14 +1,14 @@ \ No newline at end of file diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js index 854fbc9644..ef10c70efb 100644 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js +++ b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/export-named/modules/x/cmp/cmp.js @@ -9,10 +9,9 @@ import { createElement, renderComponent, } from '../../../barrel.js'; -import { importsToString } from '../../../../shared.js'; export default class extends LightningElement { - imports = importsToString([ + imports = [ getComponentDefAlias, isComponentConstructor, createContextProvider, @@ -21,5 +20,8 @@ export default class extends LightningElement { unwrap, createElement, renderComponent, - ]); + ] + // renderComponent is aliased here: https://github.com/salesforce/lwc/blob/5d01843a7733a03b9ccb59a70ad64af955f15b88/packages/%40lwc/ssr-runtime/src/index.ts#L31 + .map((i) => (i.name === 'serverSideRenderComponent' ? 'renderComponent' : i.name)) + .join('\n '); } diff --git a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts index 92e9d9d663..733c806948 100644 --- a/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts +++ b/packages/@lwc/ssr-compiler/src/compile-js/lwc-import.ts @@ -33,7 +33,9 @@ export function replaceLwcImport(path: NodePath, state: Compo } } - path.replaceWith(b.importDeclaration(path.node.specifiers, b.literal('@lwc/ssr-runtime'))); + path.replaceWith( + b.importDeclaration(structuredClone(path.node.specifiers), b.literal('@lwc/ssr-runtime')) + ); } /** @@ -46,8 +48,8 @@ export function replaceNamedLwcExport(path: NodePath) { path.replaceWith( b.exportNamedDeclaration( - path.node.declaration, - path.node.specifiers, + structuredClone(path.node.declaration), + structuredClone(path.node.specifiers), b.literal('@lwc/ssr-runtime') ) ); @@ -61,7 +63,9 @@ export function replaceAllLwcExport(path: NodePath) { return; } - path.replaceWith(b.exportAllDeclaration(b.literal('@lwc/ssr-runtime'), path.node.exported)); + path.replaceWith( + b.exportAllDeclaration(b.literal('@lwc/ssr-runtime'), structuredClone(path.node.exported)) + ); } /** From 7478dcc502887486003e2e88c8a21e0bff9c5f93 Mon Sep 17 00:00:00 2001 From: John Hefferman Date: Mon, 16 Dec 2024 20:09:14 -0700 Subject: [PATCH 8/8] fix: remove unused shared file --- .../__tests__/fixtures/barrel-lwc-exports/shared.js | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js diff --git a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js b/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js deleted file mode 100644 index a1bfb5d28e..0000000000 --- a/packages/@lwc/engine-server/src/__tests__/fixtures/barrel-lwc-exports/shared.js +++ /dev/null @@ -1,11 +0,0 @@ -export function importsToString(imports) { - return imports - .map((i) => - i - .toString() - .split('(')[0] - // renderComponent is aliased here: https://github.com/salesforce/lwc/blob/5d01843a7733a03b9ccb59a70ad64af955f15b88/packages/%40lwc/ssr-runtime/src/index.ts#L31 - .replace('async function serverSideRenderComponent', 'function renderComponent') - ) - .join('\n '); -}