forge: Support webpack externals, modules listed as external should not be pruned during package
- I have read the contribution documentation for this project.
- I agree to follow the code of conduct that this project follows, as appropriate.
- I have searched the issue tracker for an issue that matches the one I want to file, without success.
I am using ‘electron-edge-js’ to run a simple C# script in my main.js file. To use the imported package I’ve set my webpack.main.config.js to:
module.exports = {
entry: ['babel-polyfill','./src/main.js'],
module: {
rules: require('./webpack.rules'),
},
resolve: {
extensions: ['*','.js', '.jsx'],
},
externals: {
'electron-edge-js': 'electron-edge-js',
}
};
This works as expected when using the start script (because the packages are in the node_modules folder). But when the app is packaged, I get the error: module ‘electron-edge-js’ not found. When inspecting the built resources>app>node_modules folder I see that it’s empty and the created package.json shows no dependencies. I feel like I’m missing something obvious but I’ve searched and tried possible solutions to no avail.
What does your config.forge
data in package.json
look like?
"config": {
"forge": {
"packagerConfig": {},
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "gps_app",
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
]
},
{
"name": "@electron-forge/maker-deb",
"config": {}
},
{
"name": "@electron-forge/maker-rpm",
"config": {}
}
],
"plugins": [
[
"@electron-forge/plugin-webpack",
{
"mainConfig": "./webpack.main.config.js",
"renderer": {
"config": "./webpack.renderer.config.js",
"entryPoints": [
{
"html": "./src/index.html",
"js": "./src/renderer.js",
"name": "main_window"
}
]
}
}
]
]
}
},
Minimum test case to reproduce can be found here or clone: git@github.com:dustinstein/test-app.git
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 7
- Comments: 15 (3 by maintainers)
I have been digging through the packger code for hours and found a solution by adding a few hooks. Basically we need to find out which packages got marked as externals in webpack, and run
npm install
in the build path to fill up thenode_modules
folder in the package. Seems hacky but I think it works fine. The inspiration comes from the build steps of serverless-webpack package.Sharing my forge config here:
I’m almost embarrassed to say what I did but I was at the end of my expertise. I started a new project with electron-edge-js-quick-start and copied my main electron process code to that project. Since the electron-forge webpack was working well (before packaging or making) I built the project with electron-forge and used the compiled index.js file in .webpack/renderer/main_window folder in my electron-edge-js-quick-start project and it ran and packaged well.
Of course you’ll have to manually move dependencies to your package.json and you may have to change Electron versions depending on your project. When I have time I’m going to dive deeper and see if maybe I can do something a bit cleaner. It works but it’s a messy solution.
Good prior art from @timfish https://github.com/electron-userland/electron-forge/issues/1276#issuecomment-863158445
https://www.npmjs.com/package/@timfish/forge-externals-plugin
thank you for taking your time to post it here.
I’m getting an error when I try to make my app with that, I’m adding the hooks to
webpack.main.config
using electron-forge: 6.0.0-beta.63the error:
should I add the hooks somewhere else?