-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Description
Is your feature request related to a problem? Please describe.
Currently workers can only run ".js", ".mjs", or ".cjs" files. However, you can pass them, through execArgv, a preloaded module to handle any other extension. This is the case with babel-register
or esbuild-register
, allowing node to run ts
files on the fly. node -r esbuild-register foo.ts
.
This would work if it weren't for the extension check:
const { Worker } = require("worker_threads");
const worker = new Worker("./test.ts", {
execArgv: ["--require", "esbuild-register"],
});
Describe the solution you'd like
Maybe the current extension check is not needed, or should be able to be manually disabled through an option.
Describe alternatives you've considered
A workaround is to monkeypatch path.extname
to masquerade your worker file as .js
file. While it is obviously inappropriate, it shows that everything indeed works without any extension check.
const path = require("path");
const oldExtname = path.extname;
path.extname = filename => filename.endsWith('test.ts') ? ".js" : oldExtname(filename);
const { Worker } = require("worker_threads");
const worker = new Worker("./test.ts", {
execArgv: ["--require", "esbuild-register"],
});