Skip to content

Commit 6185811

Browse files
committed
test(wtr): clean up module compilation
1 parent aa47c49 commit 6185811

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

packages/@lwc/integration-not-karma/configs/plugins/serve-hydration.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ async function exists(path) {
3232
}
3333
}
3434

35-
async function getCompiledModule(dir, compileForSSR, format = 'iife') {
35+
async function compileModule(input, targetSSR, format) {
36+
const modulesDir = path.join(ROOT_DIR, input.slice(0, -COMPONENT_ENTRYPOINT.length));
3637
const bundle = await rollup({
37-
input: path.join(dir, COMPONENT_ENTRYPOINT),
38+
input,
3839
plugins: [
3940
lwcRollupPlugin({
40-
targetSSR: !!compileForSSR,
41-
modules: [{ dir: path.join(ROOT_DIR, dir) }],
41+
targetSSR,
42+
modules: [{ dir: modulesDir }],
4243
experimentalDynamicComponent: {
4344
loader: fileURLToPath(new URL('../../helpers/loader.js', import.meta.url)),
4445
strict: true,
@@ -158,30 +159,28 @@ async function existsUp(dir, file) {
158159
* Hydration test `index.spec.js` files are actually config files, not spec files.
159160
* This function wraps those configs in the test code to be executed.
160161
*/
161-
async function wrapHydrationTest(filePath) {
162+
async function wrapHydrationTest(configPath) {
162163
const {
163164
default: { expectedSSRConsoleCalls, requiredFeatureFlags },
164-
} = await import(path.join(ROOT_DIR, filePath));
165+
} = await import(path.join(ROOT_DIR, configPath));
165166

166167
try {
167168
requiredFeatureFlags?.forEach((featureFlag) => {
168169
lwcSsr.setFeatureFlagForTest(featureFlag, true);
169170
});
170171

171-
const suiteDir = path.dirname(filePath);
172+
const suiteDir = path.dirname(configPath);
172173
const componentEntrypoint = path.join(suiteDir, COMPONENT_ENTRYPOINT);
173174
// You can add an `.only` file alongside an `index.spec.js` file to make it `fdescribe()`
174175
const onlyFileExists = await existsUp(suiteDir, '.only');
175176

176-
const componentDefSSR = ENGINE_SERVER
177-
? await getCompiledModule(suiteDir, false)
178-
: await getCompiledModule(suiteDir, true);
179-
const ssrOutput = await getSsrCode(componentDefSSR, filePath, expectedSSRConsoleCalls);
177+
const componentDefSSR = await compileModule(componentEntrypoint, !ENGINE_SERVER, 'iife');
178+
const ssrOutput = await getSsrCode(componentDefSSR, configPath, expectedSSRConsoleCalls);
180179

181180
return `
182181
import { runTest } from '/helpers/test-hydrate.js';
183182
runTest(
184-
'/${filePath}?original=1',
183+
'/${configPath}?original=1',
185184
'/${componentEntrypoint}',
186185
${JSON.stringify(ssrOutput) /* escape quotes */},
187186
${onlyFileExists}
@@ -205,7 +204,7 @@ export default {
205204
if (ctx.path.endsWith('.spec.js') && !ctx.query.original) {
206205
return await wrapHydrationTest(ctx.path.slice(1)); // remove leading /
207206
} else if (ctx.path.endsWith('/' + COMPONENT_ENTRYPOINT)) {
208-
return getCompiledModule(ctx.path.slice(1, -COMPONENT_ENTRYPOINT.length), false, 'esm');
207+
return compileModule(ctx.path.slice(1) /* remove leading / */, false, 'esm');
209208
}
210209
},
211210
};

0 commit comments

Comments
 (0)