Replies: 6 comments 30 replies
-
In theory we can implement |
Beta Was this translation helpful? Give feedback.
-
I am also currently facing challenges in regards to WebPack and WebWorkers (+AudioWorklets). While I could figure out most of my aspects, exactly the point of having inline workers is still open. So I'm joining here the discussion related to the support of inline workers. The
Case 1: I would vote for a new syntax which triggers the same worker generation logic like today, but not being dependent on the |
Beta Was this translation helpful? Give feedback.
-
@alexander-akait As per my proposal I started with a change which would introduce a new module variable. I have it over at my fork https://github.com/Danielku15/webpack/tree/feature/inline-workers (diff here). It basically consists of following changes:
The trickiest part was to get it running in the test environment because the whole worker part is customized in the WebPack worker environment to run in a Node environment. I had to fiddle a bit to get the tests working and I couldn't make a full test because I ran into strange syntax errors being reported if I used imports in the inline workers. I didn't dare yet to open a PR because we don't have confirmation yet if the additional module variable would be your preferred solution to this problem. But it gives an idea. The tests give a good indication about how it would be used: If you are fine with this path, I could open a PR for a more detailed discussion on the code changes and what further tests should be added. If this change finds its way into WebPack, I would try to also build on top of this a worklet support (#11543). For this I would then need to adapt the built-in |
Beta Was this translation helpful? Give feedback.
-
I need this.I think it is very import feature. |
Beta Was this translation helpful? Give feedback.
-
I could bypassed the cross-origin problem with this trick instead of inlining the worker code. This is just a workaround, but I hope it helps someone for now. And huge thanks to the original author, @piotr-oles. I would just copy and paste the code for reference: export class CorsWorker {
private readonly worker: Worker;
constructor(url: string | URL) {
const objectURL = URL.createObjectURL(
new Blob([`importScripts(${JSON.stringify(url.toString())});`], {
type: 'application/javascript'
})
);
this.worker = new Worker(objectURL);
URL.revokeObjectURL(objectURL);
}
getWorker() {
return this.worker;
}
} import { CorsWorker as Worker } from './cors-worker';
// aliasing CorsWorker to Worker makes it statically analyzable
const corsWorker = new Worker(new URL('../worker', import.meta.url));
// tada!
const worker = corsWorker.getWorker(); The idea is using a wrapper class FYI: This is the code in my actual project. The caller file where the wrapper class is imported as |
Beta Was this translation helpful? Give feedback.
-
In latest webpack, that's a https://webpack.js.org/configuration/output/#outputworkerpublicpath |
Beta Was this translation helpful? Give feedback.
-
I need to host all assets in CDN, but Web Worker requires the same domain policy.
The below code works correctly if I don't use CDN.
Here are the docs https://webpack.js.org/guides/web-workers/, but they don't mention how to make inline workers.
worker-loader for webpack4 https://github.com/webpack-contrib/worker-loader#cross-origin-policy
In the old approach, there is a workaround for it, but I can't find any solutions for wepback5.
I know, it's possible to use a proxy from the same domain, but I need to use CDN because of the performance.
Beta Was this translation helpful? Give feedback.
All reactions