-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow importing classic Web Workers (not Module Workers) #2550
Comments
Is there a way to build with vite the worker with all its dependencies inside (no code splitting)? I'm testing web workers and I can use For example, I get my logic and put on new project, then I configure import { execSync } from 'child_process'
// @ts-ignore
import { commands } from './commands'
for (const command of commands)
execSync(command, { stdio: 'inherit' }) where export const commands = [
'npx tsup src/parse-file-worker.ts --no-splitting --sourcemap --format cjs -d dist/build',
'npx tsup src/parse-file-worker-module.ts --sourcemap --target esnext --format esm -d dist/build',
] then I add a new "scripts": {
"build": "esno scripts/build.ts"
}, My worker looks like this import { expose } from 'comlink/dist/esm/comlink' // <== for esm format, for cjs format just use `import { expose } from 'comlink'`
import type { ParseFileFn } from '~/types'
import { parseFileAsync } from '~/parse-file-async' // <== 128 module dependencies
const parseFile: ParseFileFn = async(
file,
onFileInfo,
onControlParsed,
onProgress,
onProcessError,
onFinished,
canceled,
) => {
await parseFileAsync(
file,
onFileInfo,
onControlParsed,
onProgress,
onProcessError,
onFinished,
canceled,
)
}
// SSR compatibility
typeof self !== 'undefined' && expose(parseFile) and finally I can use it (just setting the output on public directory) and referencing it via: const worker = new Worker('/parse-file-worker-module.mjs', { type: 'module' })
const parseFile = wrap<ParseFileFn>(worker) |
I forgot to mention that we can use async function useWebWorker() {
const MyWorkerModule = await import('./my-worker?worker') // <=== code splitting
return MyWorkerModule.default() // <=== use `default` function
} Right now, the docs explains how to use with import MyWorker from './my-worker?worker'
function useWebWorker() {
return new MyWorker()
} This should be included in the |
@Shinigami92 this issue doesn't have a PR anymore |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
I think it can now be worked using |
yes, I add the options to show the |
Is this a new but undocumented feature? A PR to have this implemented is still open. |
The document has been changed. Maybe it hasn't been released yet. 2.9 hasn't been released yet. Now you can try 2.9.0-beta nine |
This is awesome @poyoho! Thanks for clarifying. |
Vite provides an easy way to import Web Workers, as described in the docs:
This gets compiled into module worker initialization:
Module workers are different from classic workers, and their support is very limited (Chrome 80+, no Safari, no Firefox), which means that shipping this code is not an option.
What's more, module workers are not drop-in replacement for classic workers, as they don't support
importScripts
, which existing implementations rely on to import scripts from CDN:I would like to request support for classic workers by allowing to pass worker options to the worker constructor:
Worker constructor options should allow all of worker options, which look like this:
--
The alternative right now is to bundle worker code in a separate process and use the usual worker initialization:
This complicates the process and doesn't provide out of the box file name hashing. To add file name hashing would require further complications.
The text was updated successfully, but these errors were encountered: