1
1
const UglifyJSPlugin = require ( 'uglifyjs-webpack-plugin' )
2
2
const webpack = require ( 'webpack' )
3
3
const Config = require ( 'webpack-chain' )
4
+
4
5
module . exports = ( api , options ) => {
6
+ // If plugin options are provided in vue.config.js, those will be used. Otherwise it is empty object
5
7
const pluginOptions =
6
8
options . pluginOptions && options . pluginOptions . electronBuilder
7
9
? options . pluginOptions . electronBuilder
8
10
: { }
11
+ // If option is not set in pluginOptions, default is used
9
12
const usesTypescript = pluginOptions . disableMainProcessTypescript
10
13
? false
11
14
: api . hasPlugin ( 'typescript' )
@@ -31,12 +34,15 @@ module.exports = (api, options) => {
31
34
const fs = require ( 'fs-extra' )
32
35
const builder = require ( 'electron-builder' )
33
36
const yargs = require ( 'yargs' )
37
+ // Import the yargs options from electron-builder
34
38
const configureBuildCommand = require ( 'electron-builder/out/builder' )
35
39
. configureBuildCommand
40
+ // Parse the raw arguments using electron-builder yargs config
36
41
const builderArgs = yargs
37
42
. command ( [ 'build' , '*' ] , 'Build' , configureBuildCommand )
38
43
. parse ( rawArgs )
39
44
const rendererConfig = api . resolveChainableWebpackConfig ( )
45
+ // Base config used in electron-builder build
40
46
const defaultBuildConfig = {
41
47
directories : {
42
48
output : outputDir
@@ -48,13 +54,18 @@ module.exports = (api, options) => {
48
54
] ,
49
55
extends : null
50
56
}
57
+ // User-defined electron-builder config, overwrites/adds to default config
58
+ const userBuildConfig = pluginOptions . builderOptions || { }
59
+ // Arguments to be passed to renderer build
51
60
const vueArgs = {
52
61
_ : [ ] ,
62
+ // For the cli-ui webpack dashboard
53
63
dashboard : args . dashboard ,
64
+ // Make sure files are outputted to proper directory
54
65
dest : outputDir + '/bundled'
55
66
}
56
- const userBuildConfig = pluginOptions . builderOptions || { }
57
67
const mainConfig = new Config ( )
68
+ // Configure main process webpack config
58
69
mainConfig
59
70
. mode ( 'production' )
60
71
. target ( 'electron-main' )
@@ -83,16 +94,20 @@ module.exports = (api, options) => {
83
94
}
84
95
85
96
console . log ( 'Bundling render process:' )
97
+ // Configure base webpack config to work properly with electron
86
98
rendererConfig . target ( 'electron-renderer' ) . output . publicPath ( './' )
87
99
rendererConfig . node . set ( '__dirname' , false ) . set ( '__filename' , false )
100
+ // Build the render process with the custom args and config
88
101
await buildRenderer ( vueArgs , api , options , rendererConfig )
102
+ // Copy fonts to css/fonts. Fixes some issues with static font imports
89
103
if ( fs . existsSync ( api . resolve ( outputDir + '/bundled/fonts' ) ) ) {
90
104
fs . mkdirSync ( api . resolve ( outputDir + '/bundled/css/fonts' ) )
91
105
fs . copySync (
92
106
api . resolve ( outputDir + '/bundled/fonts' ) ,
93
107
api . resolve ( outputDir + '/bundled/css/fonts' )
94
108
)
95
109
}
110
+ // Build the main process into the renderer process output dir
96
111
const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
97
112
console . log ( 'Bundling main process:\n' )
98
113
bundle . run ( ( err , stats ) => {
@@ -123,12 +138,14 @@ module.exports = (api, options) => {
123
138
)
124
139
125
140
console . log ( '\nBuilding app with electron-builder:\n' )
126
-
141
+ // Build the app using electron builder
127
142
builder
128
143
. build ( {
144
+ // Args parsed with yargs
129
145
...builderArgs ,
130
146
config : {
131
147
...defaultBuildConfig ,
148
+ // User-defined config overwrites defaults
132
149
...userBuildConfig
133
150
}
134
151
} )
@@ -154,10 +171,12 @@ module.exports = (api, options) => {
154
171
const execa = require ( 'execa' )
155
172
const serve = require ( '@vue/cli-service/lib/commands/serve' ) . serve
156
173
const rendererConfig = api . resolveChainableWebpackConfig ( )
174
+ // Configure renderer process to work properly with electron
157
175
rendererConfig
158
176
. target ( 'electron-renderer' )
159
177
. node . set ( '__dirname' , false )
160
178
. set ( '__filename' , false )
179
+ // Configure webpack for main process
161
180
const mainConfig = new Config ( )
162
181
mainConfig
163
182
. mode ( 'development' )
@@ -184,8 +203,8 @@ module.exports = (api, options) => {
184
203
. options ( { transpileOnly : ! mainProcessTypeChecking } )
185
204
}
186
205
206
+ // Build the main process
187
207
const bundle = webpack ( mainProcessChain ( mainConfig ) . toConfig ( ) )
188
-
189
208
console . log ( 'Bundling main process:\n' )
190
209
bundle . run ( ( err , stats ) => {
191
210
if ( err ) {
@@ -215,27 +234,32 @@ module.exports = (api, options) => {
215
234
)
216
235
217
236
console . log ( '\nStarting development server:\n' )
218
-
237
+ // Run the serve command with custom webpack config
219
238
serve (
239
+ // Use dashboard if called from ui
220
240
{ _ : [ ] , dashboard : args . dashboard } ,
221
241
api ,
222
242
options ,
223
243
rendererConfig
224
244
) . then ( server => {
245
+ // Launch electron with execa
225
246
console . log ( '\nLaunching Electron...' )
226
247
const child = execa (
227
248
'./node_modules/.bin/electron' ,
249
+ // Have it load the main process file built with webpack
228
250
[ `${ outputDir } /background.js` ] ,
229
251
{
230
252
cwd : api . resolve ( '.' ) ,
231
253
stdio : 'inherit' ,
232
254
env : {
233
255
...process . env ,
256
+ // Give the main process the url to the dev server
234
257
WEBPACK_DEV_SERVER_URL : server . url
235
258
}
236
259
}
237
260
)
238
261
child . on ( 'exit' , ( ) => {
262
+ // Exit when electron is closed
239
263
process . exit ( 0 )
240
264
} )
241
265
} )
0 commit comments