Skip to content

Commit 522333d

Browse files
remove unnecessary polyfills from Vitest pool workers, ensuring that the necessary native features are enabled
1 parent 178fa91 commit 522333d

File tree

16 files changed

+55
-277
lines changed

16 files changed

+55
-277
lines changed

packages/vitest-pool-workers/scripts/bundle.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ await esbuild.build({
7878
});
7979

8080
// Build pool, worker and libs
81-
const libPaths = Array.from(walk(path.join(pkgRoot, "src", "worker", "lib")));
81+
const libPaths = [
82+
...walk(path.join(pkgRoot, "src/worker/lib")),
83+
...walk(path.join(pkgRoot, "src/worker/node")),
84+
];
8285

8386
/** @type {import("esbuild").BuildOptions} */
8487
const commonOptions = {

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,28 @@ function buildProjectWorkerOptions(
478478
runnerWorker.compatibilityFlags.push("unsafe_module");
479479
}
480480

481+
// The following nodejs compat flags enable features required for Vitest to work properly
482+
ensureFlag(
483+
runnerWorker.compatibilityFlags,
484+
"enable_nodejs_tty_module",
485+
"disable_nodejs_tty_module"
486+
);
487+
ensureFlag(
488+
runnerWorker.compatibilityFlags,
489+
"enable_nodejs_fs_module",
490+
"disable_nodejs_fs_module"
491+
);
492+
ensureFlag(
493+
runnerWorker.compatibilityFlags,
494+
"enable_nodejs_http_modules",
495+
"disable_nodejs_http_modules"
496+
);
497+
ensureFlag(
498+
runnerWorker.compatibilityFlags,
499+
"enable_nodejs_perf_hooks_module",
500+
"disable_nodejs_perf_hooks_module"
501+
);
502+
481503
// Make sure we define an unsafe eval binding and enable the fallback service
482504
runnerWorker.unsafeEvalBinding = "__VITEST_POOL_WORKERS_UNSAFE_EVAL";
483505
runnerWorker.unsafeUseModuleFallbackService = true;
@@ -579,18 +601,18 @@ function buildProjectWorkerOptions(
579601
},
580602
// The native workerd provided nodejs modules don't always support everything Vitest needs.
581603
// As a short-term fix, inject polyfills into the worker bundle that override the native modules.
582-
{
583-
type: "ESModule",
584-
path: path.join(modulesRoot, "node:vm"),
585-
contents: fs.readFileSync(path.join(DIST_PATH, "worker/lib/node/vm.mjs")),
586-
},
587604
{
588605
type: "ESModule",
589606
path: path.join(modulesRoot, "node:console"),
590607
contents: fs.readFileSync(
591-
path.join(DIST_PATH, "worker/lib/node/console.mjs")
608+
path.join(DIST_PATH, `worker/node/console.mjs`)
592609
),
593610
},
611+
{
612+
type: "ESModule",
613+
path: path.join(modulesRoot, "node:vm"),
614+
contents: fs.readFileSync(path.join(DIST_PATH, `worker/node/vm.mjs`)),
615+
},
594616
];
595617

596618
// Build array of workers contributed by the workspace
@@ -1241,3 +1263,24 @@ export default function (ctx: Vitest): ProcessPool {
12411263
},
12421264
};
12431265
}
1266+
1267+
/**
1268+
* Ensures that the specified compatibility feature is set correctly.
1269+
* @param flags The list of current flags.
1270+
* @param enableFlag The flag that enables the feature.
1271+
* @param disableFlag The flag that disables the feature.
1272+
*/
1273+
function ensureFlag(flags: string[], enableFlag: string, disableFlag: string) {
1274+
if (!flags.includes(enableFlag)) {
1275+
log.debug(
1276+
`Adding \`${enableFlag}\` compatibility flag during tests as this feature is needed to support the Vitest runner.`
1277+
);
1278+
flags.push(enableFlag);
1279+
}
1280+
if (flags.includes(disableFlag)) {
1281+
log.info(
1282+
`Removing \`${disableFlag}\` compatibility flag during tests as that feature is needed to support the Vitest runner.`
1283+
);
1284+
flags.splice(flags.indexOf(disableFlag), 1);
1285+
}
1286+
}

packages/vitest-pool-workers/src/worker/lib/node/console.ts

Lines changed: 0 additions & 139 deletions
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/dns.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/fs.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/fs/promises.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/http.cts

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/module.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/net.cts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vitest-pool-workers/src/worker/lib/node/perf_hooks.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)