-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Ability to import WebWorkers #403
Comments
In fact WebWorkers work just fine in dev out of the box, but not in prod though... try this: // src/index.js
import { wrap } from 'https://unpkg.com/comlink/dist/esm/comlink.mjs';
const worker = new Worker('src/worker.js', { type: 'module' });
const obj = wrap(worker);
obj.inc().then(count => {
console.log('count', count);
// count 1
});
// src/worker.js
import { expose } from 'https://unpkg.com/comlink/dist/esm/comlink.mjs';
const obj = {
counter: 0,
inc () {
return ++this.counter;
}
};
expose(obj); or if workers module support is an issue // src/index.js
import { wrap } from 'https://unpkg.com/comlink/dist/esm/comlink.mjs';
const worker = new Worker('src/worker.js');
const obj = wrap(worker);
obj.inc().then(count => {
console.log('count', count);
// count 1
});
// src/worker.js
importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
const obj = {
counter: 0,
inc () {
return ++this.counter;
}
};
Comlink.expose(obj); |
Since the worker file should be left un-bundled for production, you can place in I guess what @ashubham want is to be able to import the work script (and automatically inlining it into the bundle), which is a reasonable feature request, but standard worker usage should just work when it's inside |
Thanks @yyx990803 Yes thats what I am looking for, inlining workers into modules. |
One thing I see from the Module workers can benefit from code splitting between main entrypoint and worker, or perhaps you wanted a SharedWorker instead? 🙂 edit: actually I realized just now that it does a module worker by default, but that still doesn't address the second point of wanting to do a SharedWorker with module type. |
I might be nitpicking too much here, but is there a way to configure it so that it doesn't inline? with large workers it makes the bundle sizes considerably large considering that it has to account for sourcemaps as well (?, not sure why this is chucked in during production) |
If you don't want inline, you can just put the worker script in |
That wouldn't get you the code splitting benefits of a module worker though unfortunately 😅 |
I could perhaps write a plugin to override this behavior ^^ |
Any known workaround for this? Looking to port parcel 1 project to Vite, and bundling of worker dependencies is the one feature still outstanding for me. I suppose if it is inlined, one could make use of the dynamic // worker-import.js
export {default} from './worker-file?worker'
// main.js
(async () => {
const MyWorker = await import('./worker-import') // dynamic import allows code splitting, yes?
//...
})() For that matter, is there anything barring the use of const MyWorker = await import('./worker-file?worker') |
@egriff38 have you tried this? const MyWorkerModule = await import('./worker-file?worker')
const myWorker = MyWorkerModule.default() Since you are using dynamic import, then the definition
// web worker
declare module '*?worker' {
const workerConstructor: {
new (): Worker
}
export default workerConstructor
} but the function is just a plain function returning the worker (you can see it in worker plugin):
async transform(_, id) {
...
return `export default function WorkerWrapper() {
return new Worker(${JSON.stringify(url)}, { type: 'module' })
}`
} |
Is your feature request related to a problem? Please describe.
I have been using Rollup with the https://github.com/darionco/rollup-plugin-web-worker-loader . Which enables to load web workers within modules. I want to move to ViteJS for Dev but this limitation prevents me from doing the migration.
Describe the solution you'd like
Support web-workers import within ViteJS.
Describe alternatives you've considered
Not moving to VIte until this is resolved.
Additional context
There are other plugins which do the same thing like: https://github.com/surma/rollup-plugin-off-main-thread
The text was updated successfully, but these errors were encountered: