Skip to content

Commit b45b2ef

Browse files
committed
feat(serve): merge #93, add support for custom electron args
1 parent f9289e5 commit b45b2ef

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

__tests__/commands.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,39 @@ describe('electron:serve', () => {
340340
expect(mainConfig.node.shouldBe).toBe('expected')
341341
})
342342

343+
test('Custom launch arguments is used if provided', async () => {
344+
let watchCb
345+
fs.watchFile.mockImplementation((file, cb) => {
346+
// Set callback to be called later
347+
watchCb = cb
348+
})
349+
await runCommand('electron:serve', {
350+
pluginOptions: {
351+
electronBuilder: {
352+
mainProcessFile: 'customBackground',
353+
mainProcessArgs: ['--a-flag', 'a-value']
354+
}
355+
}
356+
})
357+
358+
expect(execa).toHaveBeenCalledTimes(1)
359+
expect(execa.mock.calls[0][1]).toEqual([
360+
'dist_electron',
361+
'--a-flag',
362+
'a-value'
363+
])
364+
365+
// Mock change of background file
366+
watchCb()
367+
368+
expect(execa).toHaveBeenCalledTimes(2)
369+
expect(execa.mock.calls[0][1]).toEqual([
370+
'dist_electron',
371+
'--a-flag',
372+
'a-value'
373+
])
374+
})
375+
343376
test('process.env.IS_ELECTRON is set to true', async () => {
344377
await runCommand('electron:serve')
345378
expect(process.env.IS_ELECTRON).toBe('true')

docs/guide/configuration.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ module.exports = {
6262
mainProcessFile: 'src/myBackgroundFile.js',
6363
// Provide an array of files that, when changed, will recompile the main process and restart Electron
6464
// Your main process file will be added by default
65-
mainProcessWatch: ['src/myFile1', 'src/myFile2']
65+
mainProcessWatch: ['src/myFile1', 'src/myFile2'],
66+
// [1.0.0-rc.4+] Provide a list of arguments that Electron will be launched with during "electron:serve",
67+
// which can be accessed from the main process (src/background.js).
68+
// Note that it is ignored when --debug flag is used with "electron:serve", as you must launch Electron yourself
69+
mainProcessArgs: ['--arg-name', 'arg-value']
6670
}
6771
}
6872
}

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ module.exports = (api, options) => {
187187
mainProcessFile,
188188
...(pluginOptions.mainProcessWatch || [])
189189
]
190+
const mainProcessArgs = pluginOptions.mainProcessArgs || []
190191

191192
console.log('\nStarting development server:\n')
192193
// Run the serve command
@@ -265,11 +266,23 @@ module.exports = (api, options) => {
265266
console.log(`$WEBPACK_DEV_SERVER_URL=${server.url}`)
266267
} else {
267268
// Launch electron with execa
268-
console.log('\nLaunching Electron...')
269+
if (mainProcessArgs) {
270+
console.log(
271+
'\nLaunching Electron with arguments: ' +
272+
mainProcessArgs.join(' ') +
273+
' ...'
274+
)
275+
} else {
276+
console.log('\nLaunching Electron...')
277+
}
269278
child = execa(
270279
require('electron'),
271-
// Have it load the main process file built with webpack
272-
[outputDir],
280+
[
281+
// Have it load the main process file built with webpack
282+
outputDir,
283+
// Append other arguments specified in plugin options
284+
...mainProcessArgs
285+
],
273286
{
274287
cwd: api.resolve('.'),
275288
env: {

0 commit comments

Comments
 (0)