Skip to content

Commit 96f8b24

Browse files
committed
feat(build/serve): add support for disabling main process bundling
1 parent b45b2ef commit 96f8b24

File tree

2 files changed

+201
-127
lines changed

2 files changed

+201
-127
lines changed

__tests__/commands.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,26 @@ describe('electron:build', () => {
269269
// Env var is set correctly
270270
expect(mainConfig.plugins[1].defaultValues.VUE_APP_TEST).toBe('expected')
271271
})
272+
273+
test('Main process file is not bundled if pluginOptions.bundleMainProcess is false', async () => {
274+
await runCommand('electron:build', {
275+
pluginOptions: {
276+
electronBuilder: {
277+
bundleMainProcess: false,
278+
mainProcessFile: 'myBackgroundFile.js',
279+
outputDir: 'outputDir'
280+
}
281+
}
282+
})
283+
284+
// Background file wasn't built
285+
expect(webpack).not.toBeCalled()
286+
// Copied to proper dir instead
287+
expect(fs.copySync).toBeCalledWith(
288+
'projectPath/myBackgroundFile.js',
289+
'projectPath/outputDir/bundled/background.js'
290+
)
291+
})
272292
})
273293

274294
describe('electron:serve', () => {
@@ -529,6 +549,26 @@ describe('electron:serve', () => {
529549
'outputDir/package.json'
530550
)
531551
})
552+
553+
test('Main process file is not bundled if pluginOptions.bundleMainProcess is false', async () => {
554+
await runCommand('electron:serve', {
555+
pluginOptions: {
556+
electronBuilder: {
557+
bundleMainProcess: false,
558+
mainProcessFile: 'myBackgroundFile.js',
559+
outputDir: 'outputDir'
560+
}
561+
}
562+
})
563+
564+
// Background file wasn't built
565+
expect(webpack).not.toBeCalled()
566+
// Copied to proper dir instead
567+
expect(fs.copySync).toBeCalledWith(
568+
'projectPath/myBackgroundFile.js',
569+
'projectPath/outputDir/index.js'
570+
)
571+
})
532572
})
533573

534574
describe('Custom webpack chain', () => {

index.js

Lines changed: 161 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module.exports = (api, options) => {
2020
(usesTypescript ? 'src/background.ts' : 'src/background.js')
2121
const mainProcessChain =
2222
pluginOptions.chainWebpackMainProcess || (config => config)
23+
const bundleMainProcess =
24+
pluginOptions.bundleMainProcess == null
25+
? true
26+
: pluginOptions.bundleMainProcess
2327

2428
// Apply custom webpack config
2529
api.chainWebpack(async config => {
@@ -106,46 +110,59 @@ module.exports = (api, options) => {
106110
api.resolve(outputDir + '/bundled/css/fonts')
107111
)
108112
}
109-
// Build the main process into the renderer process output dir
110-
const bundle = bundleMain({
111-
mode: 'build',
112-
api,
113-
args,
114-
pluginOptions,
115-
outputDir,
116-
mainProcessFile,
117-
mainProcessChain,
118-
usesTypescript
119-
})
120-
console.log('Bundling main process:\n')
121-
bundle.run((err, stats) => {
122-
if (err) {
123-
console.error(err.stack || err)
124-
if (err.details) {
125-
console.error(err.details)
113+
114+
if (bundleMainProcess) {
115+
// Build the main process into the renderer process output dir
116+
const bundle = bundleMain({
117+
mode: 'build',
118+
api,
119+
args,
120+
pluginOptions,
121+
outputDir,
122+
mainProcessFile,
123+
mainProcessChain,
124+
usesTypescript
125+
})
126+
console.log('Bundling main process:\n')
127+
bundle.run((err, stats) => {
128+
if (err) {
129+
console.error(err.stack || err)
130+
if (err.details) {
131+
console.error(err.details)
132+
}
133+
return reject(err)
126134
}
127-
return reject(err)
128-
}
129135

130-
const info = stats.toJson()
136+
const info = stats.toJson()
131137

132-
if (stats.hasErrors()) {
133-
return reject(info.errors)
134-
}
138+
if (stats.hasErrors()) {
139+
return reject(info.errors)
140+
}
135141

136-
if (stats.hasWarnings()) {
137-
console.warn(info.warnings)
138-
}
142+
if (stats.hasWarnings()) {
143+
console.warn(info.warnings)
144+
}
145+
146+
console.log(
147+
stats.toString({
148+
chunks: false,
149+
colors: true
150+
})
151+
)
139152

153+
buildApp()
154+
})
155+
} else {
140156
console.log(
141-
stats.toString({
142-
chunks: false,
143-
colors: true
144-
})
157+
'Not bundling main process as bundleMainProcess was set to false in plugin options'
158+
)
159+
// Copy main process file instead of bundling it
160+
fs.copySync(
161+
api.resolve(mainProcessFile),
162+
api.resolve(`${outputDir}/bundled/background.js`)
145163
)
146-
147164
buildApp()
148-
})
165+
}
149166
}
150167
function buildApp () {
151168
console.log('\nBuilding app with electron-builder:\n')
@@ -210,116 +227,133 @@ module.exports = (api, options) => {
210227
// Kill old Electron process
211228
child.kill()
212229
}
213-
// Build the main process
214-
const bundle = bundleMain({
215-
mode: 'serve',
216-
api,
217-
args,
218-
pluginOptions,
219-
outputDir,
220-
mainProcessFile,
221-
mainProcessChain,
222-
usesTypescript,
223-
server
224-
})
225-
console.log('Bundling main process:\n')
226-
bundle.run((err, stats) => {
227-
if (err) {
228-
console.error(err.stack || err)
229-
if (err.details) {
230-
console.error(err.details)
230+
231+
if (bundleMainProcess) {
232+
// Build the main process
233+
const bundle = bundleMain({
234+
mode: 'serve',
235+
api,
236+
args,
237+
pluginOptions,
238+
outputDir,
239+
mainProcessFile,
240+
mainProcessChain,
241+
usesTypescript,
242+
server
243+
})
244+
console.log('Bundling main process:\n')
245+
bundle.run((err, stats) => {
246+
if (err) {
247+
console.error(err.stack || err)
248+
if (err.details) {
249+
console.error(err.details)
250+
}
251+
process.exit(1)
231252
}
232-
process.exit(1)
233-
}
234253

235-
const info = stats.toJson()
254+
const info = stats.toJson()
236255

237-
if (stats.hasErrors()) {
238-
console.error(info.errors)
239-
process.exit(1)
240-
}
256+
if (stats.hasErrors()) {
257+
console.error(info.errors)
258+
process.exit(1)
259+
}
241260

242-
if (stats.hasWarnings()) {
243-
console.warn(info.warnings)
244-
}
261+
if (stats.hasWarnings()) {
262+
console.warn(info.warnings)
263+
}
245264

246-
console.log(
247-
stats.toString({
248-
chunks: false,
249-
colors: true
250-
})
251-
)
252-
if (args.debug) {
253-
// Do not launch electron and provide instructions on launching through debugger
254265
console.log(
255-
'\nNot launching electron as debug argument was passed. You must launch electron though your debugger.'
256-
)
257-
console.log(
258-
`If you are using Spectron, make sure to set the IS_TEST env variable to true.`
266+
stats.toString({
267+
chunks: false,
268+
colors: true
269+
})
259270
)
271+
launchElectron()
272+
})
273+
} else {
274+
console.log(
275+
'Not bundling main process as bundleMainProcess was set to false in plugin options'
276+
)
277+
// Copy main process file instead of bundling it
278+
fs.copySync(
279+
api.resolve(mainProcessFile),
280+
api.resolve(`${outputDir}/index.js`)
281+
)
282+
launchElectron()
283+
}
284+
}
285+
// Initial start of Electron
286+
startElectron()
287+
// Restart on main process file change
288+
mainProcessWatch.forEach(file => {
289+
fs.watchFile(api.resolve(file), startElectron)
290+
})
291+
292+
function launchElectron () {
293+
if (args.debug) {
294+
// Do not launch electron and provide instructions on launching through debugger
295+
console.log(
296+
'\nNot launching electron as debug argument was passed. You must launch electron though your debugger.'
297+
)
298+
console.log(
299+
`If you are using Spectron, make sure to set the IS_TEST env variable to true.`
300+
)
301+
console.log(
302+
'Learn more about debugging the main process at https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/testingAndDebugging.html#debugging.'
303+
)
304+
} else if (args.headless) {
305+
// Log information for spectron
306+
console.log(`$outputDir=${outputDir}`)
307+
console.log(`$WEBPACK_DEV_SERVER_URL=${server.url}`)
308+
} else {
309+
// Launch electron with execa
310+
if (mainProcessArgs) {
260311
console.log(
261-
'Learn more about debugging the main process at https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/testingAndDebugging.html#debugging.'
312+
'\nLaunching Electron with arguments: ' +
313+
mainProcessArgs.join(' ') +
314+
' ...'
262315
)
263-
} else if (args.headless) {
264-
// Log information for spectron
265-
console.log(`$outputDir=${outputDir}`)
266-
console.log(`$WEBPACK_DEV_SERVER_URL=${server.url}`)
267316
} else {
268-
// Launch electron with execa
269-
if (mainProcessArgs) {
270-
console.log(
271-
'\nLaunching Electron with arguments: ' +
272-
mainProcessArgs.join(' ') +
273-
' ...'
274-
)
275-
} else {
276-
console.log('\nLaunching Electron...')
277-
}
278-
child = execa(
279-
require('electron'),
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-
],
286-
{
287-
cwd: api.resolve('.'),
288-
env: {
289-
...process.env,
290-
// Disable electron security warnings
291-
ELECTRON_DISABLE_SECURITY_WARNINGS: true
292-
}
317+
console.log('\nLaunching Electron...')
318+
}
319+
child = execa(
320+
require('electron'),
321+
[
322+
// Have it load the main process file built with webpack
323+
outputDir,
324+
// Append other arguments specified in plugin options
325+
...mainProcessArgs
326+
],
327+
{
328+
cwd: api.resolve('.'),
329+
env: {
330+
...process.env,
331+
// Disable electron security warnings
332+
ELECTRON_DISABLE_SECURITY_WARNINGS: true
293333
}
294-
)
295-
296-
if (pluginOptions.removeElectronJunk === false) {
297-
// Pipe output to console
298-
child.stdout.pipe(process.stdout)
299-
child.stderr.pipe(process.stderr)
300-
} else {
301-
// Remove junk terminal output (#60)
302-
child.stdout
303-
.pipe(require('./lib/removeJunk.js')())
304-
.pipe(process.stdout)
305-
child.stderr
306-
.pipe(require('./lib/removeJunk.js')())
307-
.pipe(process.stderr)
308334
}
335+
)
309336

310-
child.on('exit', () => {
311-
// Exit when electron is closed
312-
process.exit(0)
313-
})
337+
if (pluginOptions.removeElectronJunk === false) {
338+
// Pipe output to console
339+
child.stdout.pipe(process.stdout)
340+
child.stderr.pipe(process.stderr)
341+
} else {
342+
// Remove junk terminal output (#60)
343+
child.stdout
344+
.pipe(require('./lib/removeJunk.js')())
345+
.pipe(process.stdout)
346+
child.stderr
347+
.pipe(require('./lib/removeJunk.js')())
348+
.pipe(process.stderr)
314349
}
315-
})
350+
351+
child.on('exit', () => {
352+
// Exit when electron is closed
353+
process.exit(0)
354+
})
355+
}
316356
}
317-
// Initial start of Electron
318-
startElectron()
319-
// Restart on main process file change
320-
mainProcessWatch.forEach(file => {
321-
fs.watchFile(api.resolve(file), startElectron)
322-
})
323357
}
324358
)
325359

0 commit comments

Comments
 (0)