developit / workerize-loader

🏗️ Automatically move a module into a Web Worker (Webpack loader)

Home Page:https://npm.im/workerize-loader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

importScripts

OhFlohre opened this issue · comments

is there a way to use the WorkerGlobalScope.importScripts() method with workerize-loader?

commented

I'd like to know this too

commented

why not self.importScripts?

Indeed - the existence of importScripts just depends on how you load the module. If it's a Classic Worker (new Worker(url)), importScripts() should work fine. If it's a Module Worker (new Worker(url, {type:'module'})), you'll want to use import() instead.

Read more here: https://web.dev/module-workers/

(I get the impression this question was prompted by TypeScript making it seem like importScripts() isn't defined - TypeScript doesn't know that you're in a Worker context unless you tell it you are, which requires moving your worker code into a directory with its own tsconfig.js that replaces DOM typings with Worker ones. It's... a rough edge)