Skip to content

Commit 443ffc9

Browse files
committed
feat: use a random VITE_HMR_PORT and inject DEV_MODE env var when running dev server
1 parent 71921ce commit 443ffc9

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/dev_server.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import getRandomPort from 'get-port'
1011
import { cliui } from '@poppinss/cliui'
1112
import type Hooks from '@poppinss/hooks'
1213
import prettyHrtime from 'pretty-hrtime'
@@ -95,6 +96,12 @@ export class DevServer {
9596
*/
9697
#stickyPort!: string
9798

99+
/**
100+
* The stickyHmrPort is set by the start and the startAndWatch methods
101+
* and we will continue to use that port during restart
102+
*/
103+
#stickyHmrPort!: string
104+
98105
/**
99106
* The mode is set by the start and the startAndWatch methods
100107
*/
@@ -166,7 +173,7 @@ export class DevServer {
166173
this.#httpServer.removeAllListeners()
167174
this.#httpServer.kill('SIGKILL')
168175
}
169-
await this.#startHTTPServer(this.#stickyPort)
176+
await this.#startHTTPServer(this.#stickyPort, this.#stickyHmrPort)
170177
}, 'restartHTTPServer')
171178

172179
/**
@@ -652,6 +659,7 @@ export class DevServer {
652659

653660
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
654661
this.#stickyPort = String(await getPort(this.cwd))
662+
this.#stickyHmrPort = String(getRandomPort({ port: 24678 }))
655663
this.#fileSystem = new FileSystem(this.cwdPath, tsConfig, this.options)
656664

657665
this.ui.logger.info('loading hooks...')
@@ -695,7 +703,7 @@ export class DevServer {
695703
* @example
696704
* await devServer.#startHTTPServer('3333')
697705
*/
698-
async #startHTTPServer(port: string) {
706+
async #startHTTPServer(port: string, hmrPort: string) {
699707
/**
700708
* Execute the registered before creating the child process. This will allow
701709
* hooks to modify the options before they are used.
@@ -709,7 +717,7 @@ export class DevServer {
709717
*/
710718
this.#httpServer = runNode(this.cwd, {
711719
script: this.scriptFile,
712-
env: { PORT: port, ...this.options.env },
720+
env: { PORT: port, VITE_HMR_PORT: hmrPort, DEV_MODE: 'true', ...this.options.env },
713721
nodeArgs: this.options.nodeArgs,
714722
reject: true,
715723
scriptArgs: this.options.scriptArgs,
@@ -854,7 +862,7 @@ export class DevServer {
854862
}
855863

856864
this.ui.logger.info('starting HTTP server...')
857-
await this.#startHTTPServer(this.#stickyPort)
865+
await this.#startHTTPServer(this.#stickyPort, this.#stickyHmrPort)
858866

859867
if (this.#mode !== 'hmr') {
860868
return
@@ -894,7 +902,7 @@ export class DevServer {
894902
}
895903

896904
this.ui.logger.info('starting HTTP server...')
897-
await this.#startHTTPServer(this.#stickyPort)
905+
await this.#startHTTPServer(this.#stickyPort, this.#stickyHmrPort)
898906

899907
this.#watcher = this.#createWatcher({ poll: options?.poll })
900908
this.#watcher.on('add', (filePath) => {

src/test_runner.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import getRandomPort from 'get-port'
1011
import { join } from 'node:path/posix'
1112
import { cliui } from '@poppinss/cliui'
1213
import type Hooks from '@poppinss/hooks'
@@ -65,6 +66,12 @@ export class TestRunner {
6566
*/
6667
#stickyPort!: string
6768

69+
/**
70+
* The stickyHmrPort is set by the start and the startAndWatch methods
71+
* and we will continue to use that port during restart
72+
*/
73+
#stickyHmrPort!: string
74+
6875
/**
6976
* Reference to chokidar watcher
7077
*/
@@ -113,7 +120,7 @@ export class TestRunner {
113120
this.#testsProcess.removeAllListeners()
114121
this.#testsProcess.kill('SIGKILL')
115122
}
116-
await this.#runTests(this.#stickyPort, filters)
123+
await this.#runTests(this.#stickyPort, this.#stickyHmrPort, filters)
117124
}, 'reRunTests')
118125

119126
/**
@@ -242,7 +249,7 @@ export class TestRunner {
242249
* @param port - The port number to set in the environment
243250
* @param filters - Optional test filters to apply for this run
244251
*/
245-
async #runTests(port: string, filters?: TestRunnerOptions['filters']) {
252+
async #runTests(port: string, hmrPort: string, filters?: TestRunnerOptions['filters']) {
246253
/**
247254
* Execute the registered before creating the child process. This will allow
248255
* hooks to modify the options before they are used.
@@ -265,7 +272,7 @@ export class TestRunner {
265272
this.#testsProcess = runNode(this.cwd, {
266273
script: this.scriptFile,
267274
reject: true,
268-
env: { PORT: port, ...this.options.env },
275+
env: { PORT: port, VITE_HMR_PORT: hmrPort, ...this.options.env },
269276
nodeArgs: this.options.nodeArgs,
270277
scriptArgs,
271278
})
@@ -400,6 +407,7 @@ export class TestRunner {
400407
*/
401408
async run() {
402409
this.#stickyPort = String(await getPort(this.cwd))
410+
this.#stickyHmrPort = String(getRandomPort({ port: 24678 }))
403411
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
404412

405413
this.#clearScreen()
@@ -418,7 +426,7 @@ export class TestRunner {
418426
await this.#indexGenerator.generate()
419427

420428
this.ui.logger.info('booting application to run tests...')
421-
await this.#runTests(this.#stickyPort)
429+
await this.#runTests(this.#stickyPort, this.#stickyHmrPort)
422430
}
423431

424432
/**
@@ -440,6 +448,7 @@ export class TestRunner {
440448
}
441449

442450
this.#stickyPort = String(await getPort(this.cwd))
451+
this.#stickyHmrPort = String(getRandomPort({ port: 24678 }))
443452
this.#indexGenerator = new IndexGenerator(this.cwdPath, this.ui.logger)
444453

445454
this.#fileSystem = new FileSystem(this.cwdPath, tsConfig, {
@@ -476,7 +485,7 @@ export class TestRunner {
476485
await this.#indexGenerator.generate()
477486

478487
this.ui.logger.info('booting application to run tests...')
479-
await this.#runTests(this.#stickyPort)
488+
await this.#runTests(this.#stickyPort, this.#stickyHmrPort)
480489

481490
/**
482491
* Create watcher

0 commit comments

Comments
 (0)