Reputation: 261
So basically I am building an electron app. I have reached an interesting problem I have been trying to figure out for a few hours now. There is part of the app that needs to be a forked process(child_process.fork()) which is relative to the source directory.
Like this:
- app
- main-process
- core
- manager
- index.js
- fork.js
- some-module.js
- main.js
webpack compiles the source code into one file/bundle(the main.js file), along with electron-builder compiling in app.asar.
The issue I'm having is how to use child_process.fork() in a relative directory that is requiring modules from the forked process when the app is compiled & minified. Those modules required, when compiled are now in the main.js file minfied thus the refence is lost in the fork.js making "some-module" not be found.
// app/main-process/core/manager/index.js
const child_process = require('child_process');
let child = child_process.fork('./fork.js');
child.send('start');
// app/main-process/core/manager/fork.js
const someModule = require('./some-module');
someModule(()=>{
...
})
When compiled, webpack automatically resolves these require paths into references in the bundle but since child_process.fork()
requires a file, it would make sense that this would not work.
The webpack config is fairly straitforwards config. Its not a complex app yet. This is a multi-webpack config but im just showing the main process config.
const backend = {
entry:path.resolve(__dirname, '../app/main.js'),
devtool:false,
target:'node',
output: {
filename: 'main-compiled.js',
sourceMapFilename: 'main-compiled.js.map',
},
node:{
fs:'empty',
http:'empty',
crypto:'empty',
electorn:'empty',
__dirname:true,
__filename:true,
},
module:{
loaders:[
{
test:/\.json$/,
use:[
{loader:'json-loader'},
]
}
]
},
plugins:[
new webpack.optimize.UglifyJsPlugin({
sourceMap:true,
parallel: true,
compress:{
warnings:false,
drop_console:true
}
})
],
externals:[
nodeExternals(),
]
};
I have already figured out that with webpack i can exclude this fork.js file from being included in the bundle and with electron-builder, keep the directory structor by using the files array in the config.
Upvotes: 3
Views: 1872
Reputation: 261
I figured it out. I was overcomplicating the thought process. The kind of behavior can be achieved by using the cluster master/worker pattern and just using the main.js file to coordinate what logic is executed via command arguments when spawned as a "child"(fork).
Upvotes: 2