Skip to content

Commit cadec5a

Browse files
committed
add comments to describe code
1 parent 9d23004 commit cadec5a

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

generator/template/src/background.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ function createMainWindow () {
1414

1515
if (isDevelopment) {
1616
window.webContents.openDevTools()
17+
// Load the url of the dev server if in development mode
1718
window.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
1819
} else {
20+
// Load the index.html when not in development
1921
window.loadURL(
2022
formatUrl({
2123
pathname: path.join(__dirname, 'index.html'),

index.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
22
const webpack = require('webpack')
33
const Config = require('webpack-chain')
4+
45
module.exports = (api, options) => {
6+
// If plugin options are provided in vue.config.js, those will be used. Otherwise it is empty object
57
const pluginOptions =
68
options.pluginOptions && options.pluginOptions.electronBuilder
79
? options.pluginOptions.electronBuilder
810
: {}
11+
// If option is not set in pluginOptions, default is used
912
const usesTypescript = pluginOptions.disableMainProcessTypescript
1013
? false
1114
: api.hasPlugin('typescript')
@@ -31,12 +34,15 @@ module.exports = (api, options) => {
3134
const fs = require('fs-extra')
3235
const builder = require('electron-builder')
3336
const yargs = require('yargs')
37+
// Import the yargs options from electron-builder
3438
const configureBuildCommand = require('electron-builder/out/builder')
3539
.configureBuildCommand
40+
// Parse the raw arguments using electron-builder yargs config
3641
const builderArgs = yargs
3742
.command(['build', '*'], 'Build', configureBuildCommand)
3843
.parse(rawArgs)
3944
const rendererConfig = api.resolveChainableWebpackConfig()
45+
// Base config used in electron-builder build
4046
const defaultBuildConfig = {
4147
directories: {
4248
output: outputDir
@@ -48,13 +54,18 @@ module.exports = (api, options) => {
4854
],
4955
extends: null
5056
}
57+
// User-defined electron-builder config, overwrites/adds to default config
58+
const userBuildConfig = pluginOptions.builderOptions || {}
59+
// Arguments to be passed to renderer build
5160
const vueArgs = {
5261
_: [],
62+
// For the cli-ui webpack dashboard
5363
dashboard: args.dashboard,
64+
// Make sure files are outputted to proper directory
5465
dest: outputDir + '/bundled'
5566
}
56-
const userBuildConfig = pluginOptions.builderOptions || {}
5767
const mainConfig = new Config()
68+
// Configure main process webpack config
5869
mainConfig
5970
.mode('production')
6071
.target('electron-main')
@@ -83,16 +94,20 @@ module.exports = (api, options) => {
8394
}
8495

8596
console.log('Bundling render process:')
97+
// Configure base webpack config to work properly with electron
8698
rendererConfig.target('electron-renderer').output.publicPath('./')
8799
rendererConfig.node.set('__dirname', false).set('__filename', false)
100+
// Build the render process with the custom args and config
88101
await buildRenderer(vueArgs, api, options, rendererConfig)
102+
// Copy fonts to css/fonts. Fixes some issues with static font imports
89103
if (fs.existsSync(api.resolve(outputDir + '/bundled/fonts'))) {
90104
fs.mkdirSync(api.resolve(outputDir + '/bundled/css/fonts'))
91105
fs.copySync(
92106
api.resolve(outputDir + '/bundled/fonts'),
93107
api.resolve(outputDir + '/bundled/css/fonts')
94108
)
95109
}
110+
// Build the main process into the renderer process output dir
96111
const bundle = webpack(mainProcessChain(mainConfig).toConfig())
97112
console.log('Bundling main process:\n')
98113
bundle.run((err, stats) => {
@@ -123,12 +138,14 @@ module.exports = (api, options) => {
123138
)
124139

125140
console.log('\nBuilding app with electron-builder:\n')
126-
141+
// Build the app using electron builder
127142
builder
128143
.build({
144+
// Args parsed with yargs
129145
...builderArgs,
130146
config: {
131147
...defaultBuildConfig,
148+
// User-defined config overwrites defaults
132149
...userBuildConfig
133150
}
134151
})
@@ -154,10 +171,12 @@ module.exports = (api, options) => {
154171
const execa = require('execa')
155172
const serve = require('@vue/cli-service/lib/commands/serve').serve
156173
const rendererConfig = api.resolveChainableWebpackConfig()
174+
// Configure renderer process to work properly with electron
157175
rendererConfig
158176
.target('electron-renderer')
159177
.node.set('__dirname', false)
160178
.set('__filename', false)
179+
// Configure webpack for main process
161180
const mainConfig = new Config()
162181
mainConfig
163182
.mode('development')
@@ -184,8 +203,8 @@ module.exports = (api, options) => {
184203
.options({ transpileOnly: !mainProcessTypeChecking })
185204
}
186205

206+
// Build the main process
187207
const bundle = webpack(mainProcessChain(mainConfig).toConfig())
188-
189208
console.log('Bundling main process:\n')
190209
bundle.run((err, stats) => {
191210
if (err) {
@@ -215,27 +234,32 @@ module.exports = (api, options) => {
215234
)
216235

217236
console.log('\nStarting development server:\n')
218-
237+
// Run the serve command with custom webpack config
219238
serve(
239+
// Use dashboard if called from ui
220240
{ _: [], dashboard: args.dashboard },
221241
api,
222242
options,
223243
rendererConfig
224244
).then(server => {
245+
// Launch electron with execa
225246
console.log('\nLaunching Electron...')
226247
const child = execa(
227248
'./node_modules/.bin/electron',
249+
// Have it load the main process file built with webpack
228250
[`${outputDir}/background.js`],
229251
{
230252
cwd: api.resolve('.'),
231253
stdio: 'inherit',
232254
env: {
233255
...process.env,
256+
// Give the main process the url to the dev server
234257
WEBPACK_DEV_SERVER_URL: server.url
235258
}
236259
}
237260
)
238261
child.on('exit', () => {
262+
// Exit when electron is closed
239263
process.exit(0)
240264
})
241265
})

ui.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = api => {
1111
let hadFailed = false
1212
let modernMode = false
1313

14+
// For webpack dashboard
1415
function resetSharedData (key) {
1516
setSharedData(`${key}-status`, null)
1617
setSharedData(`${key}-progress`, 0)
@@ -20,6 +21,7 @@ module.exports = api => {
2021
setSharedData(`${key}-problems`, null)
2122
}
2223

24+
// For webpack dashboard
2325
async function onWebpackMessage ({ data: message }) {
2426
if (message.webpackDashboardData) {
2527
const type = message.webpackDashboardData.type
@@ -91,26 +93,32 @@ module.exports = api => {
9193
if (answers.dir) args.push('--dir')
9294
if (answers.windows) {
9395
args.push('--windows')
96+
// For each windows target, add it after --windows
9497
answers.windowsTargets.forEach(t => {
9598
args.push(t)
9699
})
97100
}
98101
if (answers.linux) {
99102
args.push('--linux')
103+
// For each linux target, add it after --linux
100104
answers.linuxTargets.forEach(t => {
101105
args.push(t)
102106
})
103107
}
104108
if (answers.macos) {
105109
args.push('--macos')
110+
// For each macos target, add it after --macos
106111
answers.macosTargets.forEach(t => {
107112
args.push(t)
108113
})
109114
}
115+
// add --[arch] for each architecture target
110116
answers.archs.forEach(a => {
111117
args.push(`--${a}`)
112118
})
119+
// Webpack dashboard
113120
setSharedData('modern-mode', (modernMode = !!answers.modern))
121+
// Tell renderer build to send status to dashboard
114122
args.push('--dashboard')
115123
// Data
116124
resetSharedData('build')
@@ -144,7 +152,7 @@ module.exports = api => {
144152
link:
145153
'https://github.com/nklayman/vue-cli-plugin-electron-builder/tree/v1-dev#serve-command',
146154
onBeforeRun: ({ answers, args }) => {
147-
// Args
155+
// Tell dev server to send status to dashboard
148156
args.push('--dashboard')
149157

150158
// Data

0 commit comments

Comments
 (0)