diff --git a/.testcafe-electron-rc b/.testcafe-electron-rc index 2bf4a41ed2f..8f5f2127c9f 100644 --- a/.testcafe-electron-rc +++ b/.testcafe-electron-rc @@ -1,4 +1,4 @@ { - "mainWindowUrl": "./dist/index.html", + "mainWindowUrl": "http://localhost:3100/index.html", "appPath": "." } diff --git a/electron/controller.js b/electron/controller.js index 74948cb13c0..892a55c096d 100644 --- a/electron/controller.js +++ b/electron/controller.js @@ -1,4 +1,8 @@ import { app, ipcMain } from 'electron' +import serveStatic from 'serve-static' +import http from 'http' +import finalhandler from 'finalhandler' +import getPort from 'get-port' import { mainLog } from '@zap/utils/log' import sanitize from '@zap/utils/sanitize' @@ -38,15 +42,7 @@ class ZapController { * * @param {[object]} options Options to pass through to the renderer */ - init(options) { - // Load the application into the main window. - if (process.env.HOT) { - const port = process.env.PORT || 1212 - this.mainWindow.loadURL(`http://localhost:${port}/dist/index.html`) - } else { - this.mainWindow.loadURL(`file://${__dirname}/index.html`) - } - + async init(options = {}) { // Once the winow content has fully loaded, bootstrap the app. this.mainWindow.webContents.on('did-finish-load', () => { mainLog.trace('webContents.did-finish-load') @@ -58,6 +54,28 @@ class ZapController { // Start app. this.initApp(options) }) + + // Load the application into the main window. + if (process.env.HOT) { + // If hot mode is enabled, serve from webpack + const port = process.env.PORT || 1212 + this.mainWindow.loadURL(`http://localhost:${port}/dist/index.html`) + } + // Otherwise it's a production build, serve content over a static html server. + // We do this rather than loading over a `file://` in order to mitigate issues with external services wrongly + // detecting that we don't support ssl. + else { + const serve = serveStatic(__dirname, { index: ['index.html'] }) + const server = http.createServer((req, res) => { + serve(req, res, finalhandler(req, res)) + }) + const port = await getPort({ + host: 'localhost', + port: getPort.makeRange(3100, 3199), + }) + server.listen(port) + this.mainWindow.loadURL(`http://localhost:${port}/index.html`) + } } /** diff --git a/package.json b/package.json index 10253973ccc..07c5c71e709 100644 --- a/package.json +++ b/package.json @@ -306,6 +306,7 @@ "downshift": "3.3.4", "dropbox": "4.0.30", "electron-is-dev": "1.1.0", + "finalhandler": "1.1.2", "get-port": "5.0.0", "googleapis": "43.0.0", "history": "4.10.1", @@ -340,6 +341,7 @@ "reselect": "4.0.0", "satoshi-bitcoin": "1.0.4", "semver": "6.3.0", + "serve-static": "1.14.1", "source-map-support": "0.5.13", "split2": "3.1.1", "styled-components": "4.4.0", diff --git a/webpack/webpack.config.dll.js b/webpack/webpack.config.dll.js index 90d43bb85cf..393ac400042 100644 --- a/webpack/webpack.config.dll.js +++ b/webpack/webpack.config.dll.js @@ -19,12 +19,14 @@ export default merge.smart(baseConfig, { 'config', 'electron', 'electron-is-dev', + 'finalhandler', 'get-port', 'lndconnect', 'lnd-grpc', 'react-hot-loader', 'redux-electron-ipc', 'rimraf', + 'serve-static', 'source-map-support', 'googleapis', 'keytar', diff --git a/yarn.lock b/yarn.lock index f287203ea45..7ad43f0378b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8479,7 +8479,7 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" -finalhandler@~1.1.2: +finalhandler@1.1.2, finalhandler@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/finalhandler/-/finalhandler-1.1.2.tgz#b7e7d000ffd11938d0fdb053506f6ebabe9f587d" integrity sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==