-
Notifications
You must be signed in to change notification settings - Fork 40
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
support for webpack 5 #34
Comments
I'm getting the same |
Same for me, works in dev, broken in prod. Workaround with webpack |
@lianghx-319 Got But in my case, turning off tree shaking throughout the entire project by And here's another workaround for singleton mode: // in worker module
export const doSomething1 = ...;
export const doSomething2 = ...;
// @ts-ignore
__webpack_exports__ = { doSomething1, doSomething2 }; |
I ran in to this problem as well using the new Create React App, which now includes WebPack 5.0. The way I solved this problem was simply to use the functionalities in WebPack 5.0 and then wrap the code with
import {expose} from 'comlink';
export type PrimeNumberClassConstructors = { new ( num : number ): PrimeNumber };
export default class PrimeNumber {
private num : number =0;
constructor ( num : number ){
this.num = num;
}
public generate( ) {
let list : number [] = [2];
let val = 3;
while( list.length < this.num ){
let isPrime = list.every((v)=>(val%v)!==0);
if( isPrime ) list.push(val)
val ++;
}
return list;
}
}
expose(PrimeNumber) and the I use the worker as follow: import * as Comlink from 'comlink'
import PrimeNumber, {PrimeNumberClassConstructors} from './PrimeNumber';
export default class PrimeNumberProxy {
private worker : Worker;
private proxy : Comlink.Remote<PrimeNumber>|null;
private num : number = 0;
constructor ( num : number ){
this.worker = new Worker( new URL('./PrimeNumber.ts',import.meta.url));
this.proxy = null;
this.num = num;
}
public async generate ( ) : Promise<number[]> {
if( this.proxy == null ){
const factory = Comlink.wrap<PrimeNumberClassConstructors>(this.worker);
this.proxy = await new factory(this.num);
}
return this.proxy.generate()
}
public async dispose() {
if( this.proxy ) this.proxy[Comlink.releaseProxy]();
this.worker.terminate();
}
} Notice the |
It seems that webpack 5 in production mode breaks the singleton mode (that was working fine for webpack 4). It throws an error that
__webpack_exports__ is not defined
. In development mode for webpack 5 everything works as expected, but only in production it generates the error. I identified that if I set in webpack optimizationusedExports: false
then the issue is resolved (however that is an undesirable option).This is the config for webpack:
This is the babel config:
It works fine in webpack 4 (dev and prod) and only in dev mode in webpack 5. Any ideas how can we improve comlink support for webpack 5 in prod?
The text was updated successfully, but these errors were encountered: