Skip to content

webpack 5 web worker support for CORS? #14648

Answered by piotr-oles
piotr-oles asked this question in Q&A
Discussion options

You must be logged in to vote

I think I found a nice work-around. I created custom worker class in a separate file:

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;
    }
}

and then I imported it in another file as Worker:

import { CorsWorker as Worker } from './cors-worker';

// aliasing CorsWorker to Worker makes it statically analyzable

Replies: 3 comments 46 replies

Comment options

You must be logged in to vote
7 replies
@piotr-oles
Comment options

@piotr-oles
Comment options

@alexander-akait
Comment options

@alexander-akait
Comment options

@LeeKevin
Comment options

Comment options

You must be logged in to vote
33 replies
@gigadie
Comment options

@CraigglesO
Comment options

@gigadie
Comment options

@OmarCastro
Comment options

@mslmsl66
Comment options

Answer selected by piotr-oles
Comment options

You must be logged in to vote
6 replies
@CraigglesO
Comment options

@Manitej66
Comment options

@CraigglesO
Comment options

@kumardeo
Comment options

@kumardeo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet