Skip to content

Commit 1efd990

Browse files
committed
improve setup
1 parent 88bb9cc commit 1efd990

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

packages/vitest-pool-workers/src/config/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ function createConfigPlugin(): Plugin<WorkersConfigPluginAPI> {
159159
config.test.deps ??= {};
160160
config.test.deps.optimizer ??= {};
161161
config.test.deps.optimizer.ssr ??= {};
162-
config.test.deps.optimizer.ssr.enabled = true;
163-
config.test.deps.optimizer.ssr.exclude = [
164-
...cloudflareBuiltInModules,
165-
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
166-
];
162+
config.test.deps.optimizer.ssr.enabled ??= true;
167163
config.test.deps.optimizer.ssr.esbuildOptions = {
168164
platform: "neutral",
169165
resolveExtensions: [
@@ -179,6 +175,11 @@ function createConfigPlugin(): Plugin<WorkersConfigPluginAPI> {
179175
".ctx",
180176
],
181177
};
178+
config.test.deps.optimizer.ssr.exclude ??= [];
179+
ensureArrayIncludes(config.test.deps.optimizer.ssr.exclude, [
180+
...cloudflareBuiltInModules,
181+
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
182+
]);
182183

183184
// Ideally, we would force `pool` to be @cloudflare/vitest-pool-workers here,
184185
// but the tests in `packages/vitest-pool-workers` define `pool` as "../..".

packages/vitest-pool-workers/src/pool/module-fallback.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ function trimSuffix(suffix: string, value: string) {
4141
return value.substring(0, value.length - suffix.length);
4242
}
4343

44+
const versionHashRegExp = /\?v=[0-9a-f]+$/;
45+
/**
46+
* When pre-bundling is enabled, Vite will add a hash to the end of the file path
47+
* @see https://vite.dev/guide/features.html#npm-dependency-resolving-and-pre-bundling
48+
* @see https://github.com/cloudflare/workers-sdk/pull/5673
49+
*/
50+
function trimViteVersionHash(filePath: string) {
51+
return filePath.replace(versionHashRegExp, "");
52+
}
53+
4454
// RegExp for path suffix to force loading module as specific type.
4555
// (e.g. `/path/to/module.wasm?mf_vitest_force=CompiledWasm`)
4656
// This suffix will be added by the pool when fetching a module that matches a
@@ -323,21 +333,8 @@ async function viteResolve(
323333
// (Specifically, the "tinyrainbow" module imports `node:tty` as `tty`)
324334
return id;
325335
}
326-
// strip off deps optimization hash from vite
327-
// @see https://github.com/cloudflare/workers-sdk/pull/5673
328-
if (resolved.id.includes("?v=")) {
329-
const [id, hash] = resolved.id.split("?v=");
330-
debuglog(
331-
"Trimming deps optimization hash from vite, id =",
332-
id,
333-
"v =",
334-
hash
335-
);
336336

337-
return id;
338-
}
339-
340-
return resolved.id;
337+
return trimViteVersionHash(resolved.id);
341338
}
342339

343340
type ResolveMethod = "import" | "require";
@@ -490,9 +487,6 @@ async function load(
490487
filePath = trimSuffix(disableCjsEsmShimSuffix, filePath);
491488
}
492489

493-
// debuglog("Trimming ");
494-
// filePath = filePath.split("?v=")[0];
495-
496490
let isEsm =
497491
filePath.endsWith(".mjs") ||
498492
(filePath.endsWith(".js") && isWithinTypeModuleContext(filePath));

0 commit comments

Comments
 (0)