英文:
Typescript file path .ts inside code, not correct when compiled
问题
I'm using the following code inside workers.ts
, to load a Bull MQ worker file.
const worker = new Worker('parse', `${ __dirname }/workers/worker-parse.ts`);
Now during development I use the following script, which is working fine
npx pm2 start src/index.ts --watch
But when I build using tsc, the .ts file path won't work anymore since it's compiled to .js.
This is my production script
"prod": "tsc && npx pm2 start ecosystem.config.js --env production",
Now I'm wondering, is there any way that I can make sure that in the compiled version, my workers script will look for worker-parse.js, instead of .ts.
Or is there any way in TS to omit the extension? (Tried it but doesn't work out of the box)
英文:
I'm using the following code inside workers.ts
, to load a Bull MQ worker file.
const worker = new Worker('parse', `${ __dirname }/workers/worker-parse.ts`);
Now during development I use the following script, which is working fine
npx pm2 start src/index.ts --watch
But when I build using tsc, the .ts file path won't work anymore since it's compiled to .js.
This is my production script
"prod": "tsc && npx pm2 start ecosystem.config.js --env production",
Now I'm wondering, is there any way that I can make sure that in the compiled version, my workers script will look for worker-parse.js, instead of .ts.
Or is there any way in TS to omit the extension? (Tried it but doesn't work out of the box)
答案1
得分: 1
-
尝试在typescript中使用
worker-parse.js
。在JS中,这显然是正确的。根据所使用的解析器(或运行时),这在TS中可能有效。例如,使用nodenext
解析时,对于ts文件导入,需要使用.js
扩展名,但不清楚对于您的确切情况是否适用于workers。 -
从
import.meta.url
(或__filename
)获取当前文件的扩展名
const ext = import.meta.url.split('.').pop()!;
const worker = new Worker('parse', `${ __dirname }/workers/worker-parse.${ext}`);
英文:
-
Try using
worker-parse.js
in typescript. In JS that would be obviously correct. Depending on the used resolver(or runtime) that could work in TS. E.g. withnodenext
resolutions having.js
extension for ts file imports is regiured, but no idea if that works with workers for your exact case. -
Get the extension of current file from
import.meta.url
(or__filename
)
const ext = import.meta.url.split('.').pop()!;
const worker = new Worker('parse', `${ __dirname }/workers/worker-parse.${ext}`);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论