-
Notifications
You must be signed in to change notification settings - Fork 15
5. Add HTML Webpack Harddisk Plugin
In the previous step, we started our electron application in dev mode where we loaded the webpack dev server url (http://localhost:3000
) in renderer window which we didn't want to do.
In this article, we will load the index.html file with file://
protocol whether in dev mode or prod mode. If you clearly see, our react app's index.html file is generated dynamically with webpack's compiled bundles injected by famous HTML Webpack Plugin and this file is not written on the disc in case of webpack dev server running. As long as a file is not on the disc, we cannot load it with name as we don't know the exact path.
To make us able to load it via file protocol, first we would need to write this generated index.html file on disc (probabely in build folder) and that can be achieved HTML Webpack Hard Disc Plugin.
Install it with:
npm install --save-dev html-webpack-harddisk-plugin && npm run build:dll
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
new HtmlWebpackPlugin({
inject: true,
template: 'app/index.html',
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin(),
const path = require('path');
mainWindow.loadURL(
`file://${path.resolve(path.join(__dirname, '..', '..', 'build'))}/index.html`,
);
Browser history won't work with file protocol, so we need to change it as Hash history.
Change:
import createHistory from 'history/createBrowserHistory';
To
import createHistory from 'history/createHashHistory';
Start Main Process In Dev
cross-env HOT=1 NODE_ENV=development BABEL_ENV=electron node --trace-warnings ./node_modules/electron/cli.js -r babel-register ./app/electron/main.dev.js
Start Renderer Process In Dev
cross-env NODE_ENV=development node --trace-warnings ./node_modules/webpack-dev-server/bin/webpack-dev-server --config internals/webpack/webpack.dev.babel.js
HOPEFULLY YOU SHOULD SEE THE SAME THING IN ELECTRON WINDOW