I have discovered a simple method for adding multiple scripts, although it may not be widely known. The key factor lies in the name
field of the configuration as it determines the prefix of the webpack constant. For example, setting
"name": "my_name"
will create a constant named
MY_NAME_PRELOAD_WEBPACK_ENTRY
.
To add multiple preload scripts, you need to define multiple entry points with corresponding names. Each entry point must include the html
and js
fields, but placeholders can be used for unnecessary files.
Here is an illustrative example:
"config": {
"forge": {
"plugins": [
{
"name": "@electron-forge/plugin-webpack",
"config": {
"mainConfig": "./webpack.main.config.js",
"renderer": {
"config": "./webpack.renderer.config.js",
"entryPoints": [
{
"name": "main_window",
"html": "./src/main_window/index.html",
"js": "./src/main_window/index.js",
"preload": {
"js": "./src/main_window/preload.js"
}
},
{
"name": "extra_preload_main_window",
"html": "./src/empty.html",
"js": "./src/empty.js",
"preload": {
"js": "./src/main_window/preload2.js"
}
},
{
"name": "second_window",
"html": "./src/second_window/index.html",
"js": "./src/second_window/index.js",
"preload": {
"js": "./src/second_window/preload.js"
}
}
]
}
}
}
]
}
}
Corresponding constants are then generated, such as:
// Main window renderer path.
declare MAIN_WINDOW_WEBPACK_ENTRY;
// First main window preload script path.
declare MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY;
// Second main window preload script path.
declare EXTRA_PRELOAD_MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY;
// Other window preload and renderer paths.
declare SECOND_WINDOW_WEBPACK_ENTRY;
declare SECOND_WINDOW_PRELOAD_WEBPACK_ENTRY;
In addition, there is also:
declare EXTRA_PRELOAD_MAIN_WINDOW_WEBPACK_ENTRY;
though this points to an empty file within the webpack output.