Open danutzcodrescu opened 3 years ago
I'm getting the same __webpack_exports__ is not defined
error in prod.
Webpack 5. No error in development mode.
Same for me, works in dev, broken in prod. Workaround with webpack optimization.usedExports: false
suggested by @danutzcodrescu helps though.
I got an error in only webpack 5 prod like that. Anyone has same issue ?
optimization.usedExport: false
it seem a workaround for me
@lianghx-319 Got Cannot read property 'apply' of undefined
in webpack 5 production build.
But in my case, turning off tree shaking throughout the entire project by optimization.usedExport: false
should not be an ideal solution.
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 problem is that WebPack 5.0 doesn't uses worker-loader
since it supports web worker natively, details here.
The way I solved this problem was simply to use the functionalities in WebPack 5.0 and then wrap the code with ComLink
as follow:
PrimeNumber.ts
(I add a parameter on the constructor on purpose for testing):
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 this.worker.terminate();
as pointed out in #31 i think it is necessary.
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?