Embedding worker in Electron app using Webpack and electron-builder

Disclaimer: as per electron-boilerplate

Make webpack bundle your worker code. I’ll skip the entire code here because it’s too long: https://gist.github.com/gsongsong/02b93857e83b770554017c0566510371

Remeber the output path

1
2
3
4
5
6
{
output: {
path: path.join(__dirname, '..'),
filename: './app/dist/worker.js',
}
}

Make electron-builder copy your worker bundle file .

1
2
3
4
5
6
7
{
"build": {
"files": [
"dist/"
]
}
}

This will place your worker bunlde file inside resources/app.asar.

Adjust worker path and working directory for fork in main.dev.ts.

1
2
3
4
5
6
7
8
9
const workerPath =
process.env.NODE_ENV === 'development'
? 'app/worker.js'
: 'app.asar/dist/worker.js';
const workerCwd =
process.env.NODE_ENV === 'development'
? undefined
: join(__dirname, '..');
const worker = fork(workerPath, { cwd: workerCwd });

References