Skip to content

Commit af3242c

Browse files
committed
set dev server url during webpack build instead of env variable
1 parent ab97516 commit af3242c

File tree

5 files changed

+47
-61
lines changed

5 files changed

+47
-61
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ The build command consists of three main phases: render build, main build, and e
200200

201201
The serve command also consists of 3 main phases: main build, dev server launch, and electron launch:
202202

203-
1. Main build: This phase, like in the build command, bundles your app's main process, but in development mode.
204-
2. Dev server launch: This phase starts the built in dev server with a few modifications to work properly with electron.
203+
1. Dev server launch: This phase starts the built in dev server with a few modifications to work properly with electron.
204+
2. Main build: This phase, like in the build command, bundles your app's main process, but in development mode.
205205
3. Electron launch: This phase launches electron and tells it to load the url of the above dev server.

__tests__/commands.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ describe('testWithSpectron', async () => {
289289
// Proper url is returned
290290
expect(url).toBe('http://localhost:1234/')
291291
const appArgs = Application.mock.calls[0][0]
292-
// Spectron is launched with proper url
293-
expect(appArgs.env.WEBPACK_DEV_SERVER_URL).toBe('http://localhost:1234/')
294292
// Spectron is launched with proper path to background
295293
expect(appArgs.args).toEqual(['customOutput/background.js'])
296294
})

__tests__/serve.helper.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ portfinder.basePort = 9515
88
const serve = (project, notifyUpdate) =>
99
new Promise((resolve, reject) => {
1010
const child = project.run('vue-cli-service serve:electron')
11-
let isFirstMatch = true
1211
let log = ''
1312
child.stdout.on('data', async data => {
1413
data = data.toString()
1514
log += data
1615
try {
17-
const urlMatch = data.match(/http:\/\/[^/]+\//)
18-
if (urlMatch && isFirstMatch) {
19-
isFirstMatch = false
20-
let url = urlMatch[0]
21-
16+
if (data.match(/Launching Electron\.\.\./)) {
2217
resolve({
2318
stdout: log,
24-
url,
2519
stopServe: () => {
2620
child.stdin.write('close')
2721
}
@@ -46,14 +40,13 @@ const runTests = useTS =>
4640
// Prevent electron from being launched
4741
jest.mock('execa')
4842
// Wait for dev server to start
49-
const { stopServe, url } = await serve(project)
43+
const { stopServe } = await serve(project)
5044
expect(project.has('dist_electron/background.js')).toBe(true)
5145
// Launch app with spectron
5246
const app = new Application({
5347
path: electronPath,
5448
args: [projectPath('dist_electron/background.js')],
5549
env: {
56-
WEBPACK_DEV_SERVER_URL: url,
5750
IS_TEST: true
5851
},
5952
cwd: projectPath(''),

index.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -229,59 +229,60 @@ module.exports = (api, options) => {
229229
.options({ transpileOnly: !mainProcessTypeChecking })
230230
}
231231

232-
// Build the main process
233-
const bundle = webpack(mainProcessChain(mainConfig).toConfig())
234-
console.log('Bundling main process:\n')
235-
bundle.run((err, stats) => {
236-
if (err) {
237-
console.error(err.stack || err)
238-
if (err.details) {
239-
console.error(err.details)
232+
console.log('\nStarting development server:\n')
233+
// Run the serve command with custom webpack config
234+
serve(
235+
{
236+
_: [],
237+
// Use dashboard if called from ui
238+
dashboard: args.dashboard,
239+
// Serve in development mode if launched in headless mode
240+
mode: args.headless && !args.forceDev ? 'production' : 'development'
241+
},
242+
api,
243+
options,
244+
rendererConfig
245+
).then(server => {
246+
// Set dev server url
247+
mainConfig
248+
.plugin('env')
249+
.tap(args => [{ ...args, WEBPACK_DEV_SERVER_URL: server.url }])
250+
// Build the main process
251+
const bundle = webpack(mainProcessChain(mainConfig).toConfig())
252+
console.log('Bundling main process:\n')
253+
bundle.run((err, stats) => {
254+
if (err) {
255+
console.error(err.stack || err)
256+
if (err.details) {
257+
console.error(err.details)
258+
}
259+
process.exit(1)
240260
}
241-
process.exit(1)
242-
}
243261

244-
const info = stats.toJson()
262+
const info = stats.toJson()
245263

246-
if (stats.hasErrors()) {
247-
console.error(info.errors)
248-
process.exit(1)
249-
}
264+
if (stats.hasErrors()) {
265+
console.error(info.errors)
266+
process.exit(1)
267+
}
250268

251-
if (stats.hasWarnings()) {
252-
console.warn(info.warnings)
253-
}
269+
if (stats.hasWarnings()) {
270+
console.warn(info.warnings)
271+
}
254272

255-
console.log(
256-
stats.toString({
257-
chunks: false,
258-
colors: true
259-
})
260-
)
261-
console.log('\nStarting development server:\n')
262-
// Run the serve command with custom webpack config
263-
serve(
264-
{
265-
_: [],
266-
// Use dashboard if called from ui
267-
dashboard: args.dashboard,
268-
// Serve in development mode if launched in headless mode
269-
mode: args.headless && !args.forceDev ? 'production' : 'development'
270-
},
271-
api,
272-
options,
273-
rendererConfig
274-
).then(server => {
273+
console.log(
274+
stats.toString({
275+
chunks: false,
276+
colors: true
277+
})
278+
)
275279
if (args.debug) {
276280
// Do not launch electron and provide instructions on launching through debugger
277281
console.log(
278282
'Not launching electron as debug argument was passed. You must launch electron though your debugger.'
279283
)
280284
console.log(
281-
`Make sure to set the WEBPACK_DEV_SERVER_URL env variable to ${
282-
server.url
283-
}
284-
And IS_TEST to true`
285+
`If you are using Spectron, make sure to set the IS_TEST env variable to true.`
285286
)
286287
console.log(
287288
'Learn more about debugging the main process at https://github.com/nklayman/vue-cli-plugin-electron-builder#debugging.'
@@ -302,8 +303,6 @@ module.exports = (api, options) => {
302303
stdio: 'inherit',
303304
env: {
304305
...process.env,
305-
// Give the main process the url to the dev server
306-
WEBPACK_DEV_SERVER_URL: server.url,
307306
// Disable electron security warnings
308307
ELECTRON_DISABLE_SECURITY_WARNINGS: true
309308
}

lib/testWithSpectron.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ module.exports = (options = {}) =>
4343
// Apply user options
4444
...options.spectronOptions
4545
}
46-
if (!spectronOptions.env.WEBPACK_DEV_SERVER_URL) {
47-
// Make sure dev server url is set
48-
spectronOptions.env.WEBPACK_DEV_SERVER_URL = url
49-
}
5046

5147
// Launch app with spectron
5248
app = new Application(spectronOptions)

0 commit comments

Comments
 (0)