1- import { BrowserWindow , ipcMain , app } from "electron" ;
2- import { is } from "electron-util" ;
3- import url from "url" ;
4- import path from "path" ;
1+ const { BrowserWindow, ipcMain, app } = require ( "electron" ) ;
2+ const { is } = require ( "electron-util" ) ;
3+ const url = require ( "url" ) ;
4+ const path = require ( "path" ) ;
55
66const DEFAULT_WINDOW_WIDTH = 580 ;
77const DEFAULT_WINDOW_HEIGHT = 312 ;
@@ -10,28 +10,40 @@ function setupMainWindowClose({ win }) {
1010 win . on ( "closed" , ( ) => {
1111 // Dereference the window
1212 // For multiple windows store them in an array
13- win = undefined ;
13+ win = null ;
1414 } ) ;
1515}
1616
1717function showWindowOnReady ( { win } ) {
18- win . on ( "ready-to-show" , ( ) => {
18+ win . once ( "ready-to-show" , ( ) => {
1919 win . show ( ) ;
2020 } ) ;
2121}
2222
23+ function urlForLoad ( ) {
24+ if ( is . development ) {
25+ const port = process . env . ELECTRON_WEBPACK_WDS_PORT ;
26+ if ( ! port ) {
27+ throw "No Electron webpack WDS port set for dev. Try running `yarn run dev` instead for development mode." ;
28+ }
29+
30+ return `http://localhost:${ port } ` ;
31+ } else {
32+ return url . format ( {
33+ pathname : path . join ( __dirname , "index.html" ) ,
34+ protocol : "file" ,
35+ slashes : true
36+ } ) ;
37+ }
38+ }
39+
2340function mainWindowLoadUrl ( { win } ) {
24- const urlForLoad = is . development
25- ? `http://localhost:${ process . env . ELECTRON_WEBPACK_WDS_PORT } `
26- : url . format ( {
27- pathname : path . join ( __dirname , "index.html" ) ,
28- protocol : "file" ,
29- slashes : true
30- } ) ;
31- win . loadURL ( urlForLoad ) ;
41+ const url = urlForLoad ( ) ;
42+
43+ win . loadURL ( url ) ;
3244}
3345
34- export const createMainWindow = async function ( ) {
46+ const createMainWindow = async function ( ) {
3547 return new BrowserWindow ( {
3648 title : app . name ,
3749 show : false ,
@@ -41,8 +53,13 @@ export const createMainWindow = async function() {
4153 } ) ;
4254} ;
4355
44- export const setupMainWindow = function ( { win } ) {
56+ const setupMainWindow = function ( { win } ) {
4557 setupMainWindowClose ( { win : win } ) ;
4658 showWindowOnReady ( { win : win } ) ;
4759 mainWindowLoadUrl ( { win : win } ) ;
4860} ;
61+
62+ module . exports = {
63+ createMainWindow,
64+ setupMainWindow
65+ } ;
0 commit comments