diff --git a/src/main/holdUntilFileExist.ts b/src/main/holdUntilFileExist.ts new file mode 100644 index 0000000000..465788f0ae --- /dev/null +++ b/src/main/holdUntilFileExist.ts @@ -0,0 +1,51 @@ +import fs from "fs"; +import path from "path"; + +import logger from "./logger"; +import { appName, publicPath } from "../common/vars"; +import { webpackLensRenderer } from "../../webpack.renderer"; + +const rendererConfig = webpackLensRenderer({ showVars: false }); +const { optimization } = rendererConfig; + +const holdUntilFileExist = ({ + file, watchInterval, msg +}: { + file: string, watchInterval: number, msg: string +}): Promise => + new Promise((resolve) => { + const timeout = setInterval(() => { + const fileExists = fs.existsSync(file); + + if (fileExists) { + clearInterval(timeout); + resolve(true); + } else { + logger.info(msg); + } + }, watchInterval); + }); + +const holdUntilRendererStaticsExist = async ( + { watchInterval }: { watchInterval: number } +): Promise => { + const dir = `${__static}${publicPath}`; + // @ts-ignore (type definition is broken in webpack-dev-server 4 beta0) + const chuckFileName = optimization.splitChunks?.name; + // @ts-ignore (type definition is broken in webpack-dev-server 4 beta0) + const runtimeChuckFileName = optimization.runtimeChunk?.name; + + await Promise.all([ + `${appName}.js`, `${appName}.html`, + `${chuckFileName}.js`, `${runtimeChuckFileName}.js` + ].map((filename) => + holdUntilFileExist({ + file: path.join(dir, filename), + watchInterval, + msg: `waiting for ${filename}... have you compiled renderer process statics? 🤔` + }) + )); +}; + +export default holdUntilFileExist; +export { holdUntilRendererStaticsExist }; diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index c536e3d4a9..67f6c8579e 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -9,6 +9,7 @@ import { initTray } from "./tray"; import { Singleton } from "../common/utils"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; import { reload } from "./reload"; +import { holdUntilRendererStaticsExist } from "./holdUntilFileExist"; export class WindowManager extends Singleton { protected mainWindow: BrowserWindow; @@ -31,6 +32,11 @@ export class WindowManager extends Singleton { } async initMainWindow(showSplash = true) { + + if (process.env.NODE_ENV === "development") { + await holdUntilRendererStaticsExist({ watchInterval: 5000 }); + } + // Manage main window size and position with state persistence if (!this.windowState) { this.windowState = windowStateKeeper({