@@ -15,7 +15,6 @@ module.exports = (api, options) => {
15
15
const usesTypescript = pluginOptions . disableMainProcessTypescript
16
16
? false
17
17
: api . hasPlugin ( 'typescript' )
18
- const mainProcessTypeChecking = pluginOptions . mainProcessTypeChecking || false
19
18
const outputDir = pluginOptions . outputDir || 'dist_electron'
20
19
const mainProcessFile =
21
20
pluginOptions . mainProcessFile ||
@@ -87,40 +86,6 @@ module.exports = (api, options) => {
87
86
// Enable modern mode unless --legacy is passed
88
87
modern : ! args . legacy
89
88
}
90
- const mainConfig = new Config ( )
91
- // Configure main process webpack config
92
- mainConfig
93
- . mode ( 'production' )
94
- . target ( 'electron-main' )
95
- . node . set ( '__dirname' , false )
96
- . set ( '__filename' , false )
97
- // Set externals
98
- mainConfig . externals ( getExternals ( api , pluginOptions ) )
99
- mainConfig . output
100
- . path ( api . resolve ( outputDir + '/bundled' ) )
101
- . filename ( 'background.js' )
102
- // Set __static to __dirname (files in public get copied here)
103
- mainConfig
104
- . plugin ( 'define' )
105
- . use ( webpack . DefinePlugin , [ { __static : '__dirname' } ] )
106
- mainConfig . plugin ( 'uglify' ) . use ( UglifyJSPlugin , [
107
- {
108
- parallel : true
109
- }
110
- ] )
111
- mainConfig
112
- . plugin ( 'env' )
113
- . use ( webpack . EnvironmentPlugin , [ { NODE_ENV : 'production' } ] )
114
- mainConfig . entry ( 'background' ) . add ( api . resolve ( mainProcessFile ) )
115
- if ( usesTypescript ) {
116
- mainConfig . resolve . extensions . merge ( [ '.js' , '.ts' ] )
117
- mainConfig . module
118
- . rule ( 'ts' )
119
- . test ( / \. t s $ / )
120
- . use ( 'ts-loader' )
121
- . loader ( 'ts-loader' )
122
- . options ( { transpileOnly : ! mainProcessTypeChecking } )
123
- }
124
89
// Set the base url so that the app protocol is used
125
90
options . baseUrl = './'
126
91
console . log ( 'Bundling render process:' )
@@ -135,7 +100,16 @@ module.exports = (api, options) => {
135
100
)
136
101
}
137
102
// Build the main process into the renderer process output dir
138
- const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
103
+ const bundle = bundleMain ( {
104
+ mode : 'build' ,
105
+ api,
106
+ args,
107
+ pluginOptions,
108
+ outputDir,
109
+ mainProcessFile,
110
+ mainProcessChain,
111
+ usesTypescript
112
+ } )
139
113
console . log ( 'Bundling main process:\n' )
140
114
bundle . run ( ( err , stats ) => {
141
115
if ( err ) {
@@ -206,46 +180,6 @@ module.exports = (api, options) => {
206
180
mainProcessFile ,
207
181
...( pluginOptions . mainProcessWatch || [ ] )
208
182
]
209
- // Configure webpack for main process
210
- const mainConfig = new Config ( )
211
- mainConfig
212
- . mode ( 'development' )
213
- . target ( 'electron-main' )
214
- . node . set ( '__dirname' , false )
215
- . set ( '__filename' , false )
216
- mainConfig . output . path ( api . resolve ( outputDir ) ) . filename ( 'background.js' )
217
- if ( args . debug ) {
218
- // Enable source maps for debugging
219
- mainConfig . devtool ( 'source-map' )
220
- } else {
221
- // Minify for better performance
222
- mainConfig . plugin ( 'uglify' ) . use ( UglifyJSPlugin , [
223
- {
224
- parallel : true
225
- }
226
- ] )
227
- }
228
- // Set __static to absolute path to public folder
229
- mainConfig . plugin ( 'define' ) . use ( webpack . DefinePlugin , [
230
- {
231
- __static : JSON . stringify ( api . resolve ( './public' ) )
232
- }
233
- ] )
234
- mainConfig
235
- . plugin ( 'env' )
236
- . use ( webpack . EnvironmentPlugin , [ { NODE_ENV : 'development' } ] )
237
- mainConfig . entry ( 'background' ) . add ( api . resolve ( mainProcessFile ) )
238
- // Set external (native) deps
239
- mainConfig . externals ( getExternals ( api , pluginOptions ) )
240
- if ( usesTypescript ) {
241
- mainConfig . resolve . extensions . merge ( [ '.js' , '.ts' ] )
242
- mainConfig . module
243
- . rule ( 'ts' )
244
- . test ( / \. t s $ / )
245
- . use ( 'ts-loader' )
246
- . loader ( 'ts-loader' )
247
- . options ( { transpileOnly : ! mainProcessTypeChecking } )
248
- }
249
183
250
184
console . log ( '\nStarting development server:\n' )
251
185
// Run the serve command
@@ -255,17 +189,8 @@ module.exports = (api, options) => {
255
189
dashboard : args . dashboard
256
190
} )
257
191
258
- // Set dev server url
259
- mainConfig . plugin ( 'env' ) . tap ( args => [
260
- {
261
- // Existing env values
262
- ...args ,
263
- // Dev server url
264
- WEBPACK_DEV_SERVER_URL : server . url ,
265
- // Path to node_modules (for externals in development)
266
- NODE_MODULES_PATH : api . resolve ( './node_modules' )
267
- }
268
- ] )
192
+ // Copy package.json so electron can detect app's name
193
+ fs . copySync ( api . resolve ( './package.json' ) , `${ outputDir } /package.json` )
269
194
// Electron process
270
195
let child
271
196
// Function to bundle main process and start Electron
@@ -277,7 +202,17 @@ module.exports = (api, options) => {
277
202
child . kill ( )
278
203
}
279
204
// Build the main process
280
- const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
205
+ const bundle = bundleMain ( {
206
+ mode : 'serve' ,
207
+ api,
208
+ args,
209
+ pluginOptions,
210
+ outputDir,
211
+ mainProcessFile,
212
+ mainProcessChain,
213
+ usesTypescript,
214
+ server
215
+ } )
281
216
console . log ( 'Bundling main process:\n' )
282
217
bundle . run ( ( err , stats ) => {
283
218
if ( err ) {
@@ -326,7 +261,7 @@ module.exports = (api, options) => {
326
261
child = execa (
327
262
require ( 'electron' ) ,
328
263
// Have it load the main process file built with webpack
329
- [ ` ${ outputDir } /background.js` ] ,
264
+ [ outputDir ] ,
330
265
{
331
266
cwd : api . resolve ( '.' ) ,
332
267
env : {
@@ -367,6 +302,80 @@ module.exports = (api, options) => {
367
302
}
368
303
)
369
304
}
305
+
306
+ function bundleMain ( {
307
+ mode,
308
+ api,
309
+ args,
310
+ pluginOptions,
311
+ outputDir,
312
+ mainProcessFile,
313
+ mainProcessChain,
314
+ usesTypescript,
315
+ server
316
+ } ) {
317
+ const mainProcessTypeChecking = pluginOptions . mainProcessTypeChecking || false
318
+ const isBuild = mode === 'build'
319
+ const NODE_ENV = process . env . NODE_ENV
320
+ const config = new Config ( )
321
+ config
322
+ . mode ( NODE_ENV )
323
+ . target ( 'electron-main' )
324
+ . node . set ( '__dirname' , false )
325
+ . set ( '__filename' , false )
326
+ // Set externals
327
+ config . externals ( getExternals ( api , pluginOptions ) )
328
+
329
+ config . output
330
+ . path ( api . resolve ( outputDir + ( isBuild ? '/bundled' : '' ) ) )
331
+ // Electron will not detect background.js on dev server, only index.js
332
+ . filename ( isBuild ? 'background.js' : 'index.js' )
333
+ if ( isBuild ) {
334
+ // Set __static to __dirname (files in public get copied here)
335
+ config
336
+ . plugin ( 'define' )
337
+ . use ( webpack . DefinePlugin , [ { __static : '__dirname' } ] )
338
+ } else {
339
+ // Set __static to public folder
340
+ config . plugin ( 'define' ) . use ( webpack . DefinePlugin , [
341
+ {
342
+ __static : JSON . stringify ( api . resolve ( './public' ) )
343
+ }
344
+ ] )
345
+ config . plugin ( 'env' ) . use ( webpack . EnvironmentPlugin , [
346
+ {
347
+ // Dev server url
348
+ WEBPACK_DEV_SERVER_URL : server . url ,
349
+ // Path to node_modules (for externals in development)
350
+ NODE_MODULES_PATH : api . resolve ( './node_modules' )
351
+ }
352
+ ] )
353
+ }
354
+ if ( args . debug ) {
355
+ // Enable source maps for debugging
356
+ config . devtool ( 'source-map' )
357
+ } else if ( NODE_ENV === 'production' ) {
358
+ // Minify for better performance
359
+ config . plugin ( 'uglify' ) . use ( UglifyJSPlugin , [
360
+ {
361
+ parallel : true
362
+ }
363
+ ] )
364
+ }
365
+ config . entry ( 'background' ) . add ( api . resolve ( mainProcessFile ) )
366
+ if ( usesTypescript ) {
367
+ config . resolve . extensions . merge ( [ '.js' , '.ts' ] )
368
+ config . module
369
+ . rule ( 'ts' )
370
+ . test ( / \. t s $ / )
371
+ . use ( 'ts-loader' )
372
+ . loader ( 'ts-loader' )
373
+ . options ( { transpileOnly : ! mainProcessTypeChecking } )
374
+ }
375
+ mainProcessChain ( config )
376
+ return webpack ( config . toConfig ( ) )
377
+ }
378
+
370
379
module . exports . defaultModes = {
371
380
'build:electron' : 'production' ,
372
381
'serve:electron' : 'development'
0 commit comments