@@ -2,6 +2,7 @@ const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
2
2
const webpack = require ( 'webpack' )
3
3
const Config = require ( 'webpack-chain' )
4
4
const merge = require ( 'lodash.merge' )
5
+ const fs = require ( 'fs-extra' )
5
6
6
7
module . exports = ( api , options ) => {
7
8
// If plugin options are provided in vue.config.js, those will be used. Otherwise it is empty object
@@ -34,7 +35,6 @@ module.exports = (api, options) => {
34
35
} ,
35
36
async ( args , rawArgs ) => {
36
37
const buildRenderer = require ( '@vue/cli-service/lib/commands/build' ) . build
37
- const fs = require ( 'fs-extra' )
38
38
const builder = require ( 'electron-builder' )
39
39
const yargs = require ( 'yargs' )
40
40
// Import the yargs options from electron-builder
@@ -257,73 +257,88 @@ module.exports = (api, options) => {
257
257
mainConfig
258
258
. plugin ( 'env' )
259
259
. tap ( args => [ { ...args , WEBPACK_DEV_SERVER_URL : server . url } ] )
260
- // Build the main process
261
- const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
262
- console . log ( 'Bundling main process:\n' )
263
- bundle . run ( ( err , stats ) => {
264
- if ( err ) {
265
- console . error ( err . stack || err )
266
- if ( err . details ) {
267
- console . error ( err . details )
268
- }
269
- process . exit ( 1 )
260
+ // Electron process
261
+ let child
262
+ // Function to bundle main process and start Electron
263
+ const startElectron = ( ) => {
264
+ if ( child ) {
265
+ // Prevent self exit on Electron process death
266
+ child . removeAllListeners ( )
267
+ // Kill old Electron process
268
+ child . kill ( )
270
269
}
270
+ // Build the main process
271
+ const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
272
+ console . log ( 'Bundling main process:\n' )
273
+ bundle . run ( ( err , stats ) => {
274
+ if ( err ) {
275
+ console . error ( err . stack || err )
276
+ if ( err . details ) {
277
+ console . error ( err . details )
278
+ }
279
+ process . exit ( 1 )
280
+ }
271
281
272
- const info = stats . toJson ( )
282
+ const info = stats . toJson ( )
273
283
274
- if ( stats . hasErrors ( ) ) {
275
- console . error ( info . errors )
276
- process . exit ( 1 )
277
- }
284
+ if ( stats . hasErrors ( ) ) {
285
+ console . error ( info . errors )
286
+ process . exit ( 1 )
287
+ }
278
288
279
- if ( stats . hasWarnings ( ) ) {
280
- console . warn ( info . warnings )
281
- }
289
+ if ( stats . hasWarnings ( ) ) {
290
+ console . warn ( info . warnings )
291
+ }
282
292
283
- console . log (
284
- stats . toString ( {
285
- chunks : false ,
286
- colors : true
287
- } )
288
- )
289
- if ( args . debug ) {
290
- // Do not launch electron and provide instructions on launching through debugger
291
- console . log (
292
- '\nNot launching electron as debug argument was passed. You must launch electron though your debugger.'
293
- )
294
293
console . log (
295
- `If you are using Spectron, make sure to set the IS_TEST env variable to true.`
294
+ stats . toString ( {
295
+ chunks : false ,
296
+ colors : true
297
+ } )
296
298
)
297
- console . log (
298
- 'Learn more about debugging the main process at https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/testingAndDebugging.html#debugging.'
299
- )
300
- } else if ( args . headless ) {
301
- // Log information for spectron
302
- console . log ( `$outputDir=${ outputDir } ` )
303
- console . log ( `$WEBPACK_DEV_SERVER_URL=${ server . url } ` )
304
- } else {
305
- // Launch electron with execa
306
- console . log ( '\nLaunching Electron...' )
307
- const child = execa (
308
- './node_modules/.bin/electron' ,
309
- // Have it load the main process file built with webpack
310
- [ `${ outputDir } /background.js` ] ,
311
- {
312
- cwd : api . resolve ( '.' ) ,
313
- stdio : 'inherit' ,
314
- env : {
315
- ...process . env ,
316
- // Disable electron security warnings
317
- ELECTRON_DISABLE_SECURITY_WARNINGS : true
299
+ if ( args . debug ) {
300
+ // Do not launch electron and provide instructions on launching through debugger
301
+ console . log (
302
+ '\nNot launching electron as debug argument was passed. You must launch electron though your debugger.'
303
+ )
304
+ console . log (
305
+ `If you are using Spectron, make sure to set the IS_TEST env variable to true.`
306
+ )
307
+ console . log (
308
+ 'Learn more about debugging the main process at https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/testingAndDebugging.html#debugging.'
309
+ )
310
+ } else if ( args . headless ) {
311
+ // Log information for spectron
312
+ console . log ( `$outputDir=${ outputDir } ` )
313
+ console . log ( `$WEBPACK_DEV_SERVER_URL=${ server . url } ` )
314
+ } else {
315
+ // Launch electron with execa
316
+ console . log ( '\nLaunching Electron...' )
317
+ child = execa (
318
+ require ( 'electron' ) ,
319
+ // Have it load the main process file built with webpack
320
+ [ `${ outputDir } /background.js` ] ,
321
+ {
322
+ cwd : api . resolve ( '.' ) ,
323
+ stdio : 'inherit' ,
324
+ env : {
325
+ ...process . env ,
326
+ // Disable electron security warnings
327
+ ELECTRON_DISABLE_SECURITY_WARNINGS : true
328
+ }
318
329
}
319
- }
320
- )
321
- child . on ( 'exit' , ( ) => {
322
- // Exit when electron is closed
323
- process . exit ( 0 )
324
- } )
325
- }
326
- } )
330
+ )
331
+ child . on ( 'exit' , ( ) => {
332
+ // Exit when electron is closed
333
+ process . exit ( 0 )
334
+ } )
335
+ }
336
+ } )
337
+ }
338
+ // Initial start of Electron
339
+ startElectron ( )
340
+ // Restart on main process file change
341
+ fs . watchFile ( api . resolve ( mainProcessFile ) , startElectron )
327
342
} )
328
343
}
329
344
)
0 commit comments