Skip to content

Commit

Permalink
feat(serve): merge #93, add support for custom electron args
Browse files Browse the repository at this point in the history
  • Loading branch information
nklayman committed Oct 1, 2018
1 parent f9289e5 commit b45b2ef
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
33 changes: 33 additions & 0 deletions __tests__/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,39 @@ describe('electron:serve', () => {
expect(mainConfig.node.shouldBe).toBe('expected')
})

test('Custom launch arguments is used if provided', async () => {
let watchCb
fs.watchFile.mockImplementation((file, cb) => {
// Set callback to be called later
watchCb = cb
})
await runCommand('electron:serve', {
pluginOptions: {
electronBuilder: {
mainProcessFile: 'customBackground',
mainProcessArgs: ['--a-flag', 'a-value']
}
}
})

expect(execa).toHaveBeenCalledTimes(1)
expect(execa.mock.calls[0][1]).toEqual([
'dist_electron',
'--a-flag',
'a-value'
])

// Mock change of background file
watchCb()

expect(execa).toHaveBeenCalledTimes(2)
expect(execa.mock.calls[0][1]).toEqual([
'dist_electron',
'--a-flag',
'a-value'
])
})

test('process.env.IS_ELECTRON is set to true', async () => {
await runCommand('electron:serve')
expect(process.env.IS_ELECTRON).toBe('true')
Expand Down
6 changes: 5 additions & 1 deletion docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ module.exports = {
mainProcessFile: 'src/myBackgroundFile.js',
// Provide an array of files that, when changed, will recompile the main process and restart Electron
// Your main process file will be added by default
mainProcessWatch: ['src/myFile1', 'src/myFile2']
mainProcessWatch: ['src/myFile1', 'src/myFile2'],
// [1.0.0-rc.4+] Provide a list of arguments that Electron will be launched with during "electron:serve",
// which can be accessed from the main process (src/background.js).
// Note that it is ignored when --debug flag is used with "electron:serve", as you must launch Electron yourself
mainProcessArgs: ['--arg-name', 'arg-value']
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ module.exports = (api, options) => {
mainProcessFile,
...(pluginOptions.mainProcessWatch || [])
]
const mainProcessArgs = pluginOptions.mainProcessArgs || []

console.log('\nStarting development server:\n')
// Run the serve command
Expand Down Expand Up @@ -265,11 +266,23 @@ module.exports = (api, options) => {
console.log(`$WEBPACK_DEV_SERVER_URL=${server.url}`)
} else {
// Launch electron with execa
console.log('\nLaunching Electron...')
if (mainProcessArgs) {
console.log(
'\nLaunching Electron with arguments: ' +
mainProcessArgs.join(' ') +
' ...'
)
} else {
console.log('\nLaunching Electron...')
}
child = execa(
require('electron'),
// Have it load the main process file built with webpack
[outputDir],
[
// Have it load the main process file built with webpack
outputDir,
// Append other arguments specified in plugin options
...mainProcessArgs
],
{
cwd: api.resolve('.'),
env: {
Expand Down

0 comments on commit b45b2ef

Please sign in to comment.