Skip to content

Commit e4d27ae

Browse files
committed
feat(serve): allow custom main process watch, closes #45
1 parent 5ae8f27 commit e4d27ae

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

__tests__/commands.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,57 @@ describe('serve:electron', () => {
326326
// Electron was re-launched
327327
expect(execa).toHaveBeenCalledTimes(2)
328328
})
329+
330+
test('Main process is recompiled and Electron is relaunched when file in list change', async () => {
331+
process.exit = jest.fn()
332+
let watchCb = {}
333+
fs.watchFile.mockImplementation((file, cb) => {
334+
// Set callback to be called later
335+
watchCb[file] = cb
336+
})
337+
await runCommand('serve:electron', {
338+
pluginOptions: {
339+
electronBuilder: {
340+
mainProcessFile: 'customBackground',
341+
mainProcessWatch: ['listFile']
342+
}
343+
}
344+
})
345+
346+
// Proper file is watched
347+
expect(fs.watchFile.mock.calls[0][0]).toBe('projectPath/customBackground')
348+
expect(fs.watchFile.mock.calls[1][0]).toBe('projectPath/listFile')
349+
// Child has not yet been killed or unwatched
350+
expect(mockExeca.kill).not.toBeCalled()
351+
expect(mockExeca.removeAllListeners).not.toBeCalled()
352+
// Main process was bundled and Electron was launched initially
353+
expect(webpack).toHaveBeenCalledTimes(1)
354+
expect(execa).toHaveBeenCalledTimes(1)
355+
356+
// Mock change of listed file
357+
watchCb['projectPath/listFile']()
358+
// Electron was killed and listeners removed
359+
expect(mockExeca.kill).toHaveBeenCalledTimes(1)
360+
expect(mockExeca.removeAllListeners).toHaveBeenCalledTimes(1)
361+
// Process did not exit on Electron close
362+
expect(process.exit).not.toBeCalled()
363+
// Main process file was recompiled
364+
expect(webpack).toHaveBeenCalledTimes(2)
365+
// Electron was re-launched
366+
expect(execa).toHaveBeenCalledTimes(2)
367+
368+
// Mock change of background file
369+
watchCb['projectPath/customBackground']()
370+
// Electron was killed and listeners removed
371+
expect(mockExeca.kill).toHaveBeenCalledTimes(2)
372+
expect(mockExeca.removeAllListeners).toHaveBeenCalledTimes(2)
373+
// Process did not exit on Electron close
374+
expect(process.exit).not.toBeCalled()
375+
// Main process file was recompiled
376+
expect(webpack).toHaveBeenCalledTimes(3)
377+
// Electron was re-launched
378+
expect(execa).toHaveBeenCalledTimes(3)
379+
})
329380
})
330381

331382
describe('testWithSpectron', async () => {

docs/guide/configuration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ module.exports = {
5757
return config
5858
},
5959
// Use this to change the entrypoint of your app's main process
60-
mainProcessFile: 'src/myBackgroundFile.js'
60+
mainProcessFile: 'src/myBackgroundFile.js',
61+
// Provide an array of files that, when changed, will recompile the main process and restart Electron
62+
// Your main process file will be added by default
63+
mainProcessWatch: ['src/myFile1', 'src/myFile2']
6164
}
6265
}
6366
}

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ module.exports = (api, options) => {
192192
const execa = require('execa')
193193
const serve = require('@vue/cli-service/lib/commands/serve').serve
194194
const rendererConfig = api.resolveChainableWebpackConfig()
195+
const mainProcessWatch = [
196+
mainProcessFile,
197+
...(pluginOptions.mainProcessWatch || [])
198+
]
195199
// Configure renderer process to work properly with electron
196200
rendererConfig
197201
.target('electron-renderer')
@@ -338,7 +342,9 @@ module.exports = (api, options) => {
338342
// Initial start of Electron
339343
startElectron()
340344
// Restart on main process file change
341-
fs.watchFile(api.resolve(mainProcessFile), startElectron)
345+
mainProcessWatch.forEach(file => {
346+
fs.watchFile(api.resolve(file), startElectron)
347+
})
342348
})
343349
}
344350
)

0 commit comments

Comments
 (0)