Skip to content

Commit e472091

Browse files
authored
Merge pull request #4504 from adonisjs/feat/hmr
feat: add `--hmr` flag for the `serve` command
2 parents a511872 + fbdae40 commit e472091

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

commands/serve.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default class Serve extends BaseCommand {
2828
'{{ binaryName }} serve --watch',
2929
'```',
3030
'',
31+
'You can also start the server with HMR support using the following command.',
32+
'```',
33+
'{{ binaryName }} serve --unstable-hmr',
34+
'```',
35+
'',
3136
'The assets bundler dev server runs automatically after detecting vite config or webpack config files',
3237
'You may pass vite CLI args using the --assets-args command line flag.',
3338
'```',
@@ -41,6 +46,9 @@ export default class Serve extends BaseCommand {
4146

4247
declare devServer: DevServer
4348

49+
@flags.boolean({ description: 'Start the server with HMR support' })
50+
declare unstableHmr?: boolean
51+
4452
@flags.boolean({
4553
description: 'Watch filesystem and restart the HTTP server on file change',
4654
alias: 'w',
@@ -112,7 +120,14 @@ export default class Serve extends BaseCommand {
112120
return
113121
}
114122

123+
if (this.watch && this.unstableHmr) {
124+
this.logger.error('Cannot use --watch and --unstable-hmr flags together. Choose one of them')
125+
this.exitCode = 1
126+
return
127+
}
128+
115129
this.devServer = new assembler.DevServer(this.app.appRoot, {
130+
hmr: this.unstableHmr === true ? true : false,
116131
clearScreen: this.clear === false ? false : true,
117132
nodeArgs: this.parsed.nodeArgs,
118133
scriptArgs: [],

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"index:commands": "node --loader=ts-node/esm toolkit/main.js index build/commands"
8080
},
8181
"devDependencies": {
82-
"@adonisjs/assembler": "^7.4.0",
82+
"@adonisjs/assembler": "^7.5.0",
8383
"@adonisjs/eslint-config": "^1.3.0",
8484
"@adonisjs/prettier-config": "^1.3.0",
8585
"@adonisjs/tsconfig": "^1.3.0",
@@ -143,7 +143,7 @@
143143
"youch-terminal": "^2.2.3"
144144
},
145145
"peerDependencies": {
146-
"@adonisjs/assembler": "^7.1.0",
146+
"@adonisjs/assembler": "^7.5.0",
147147
"@vinejs/vine": "^2.0.0",
148148
"argon2": "^0.31.2 || ^0.40.0",
149149
"bcrypt": "^5.1.1",

tests/commands/serve.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,25 @@ test.group('Serve command', () => {
337337
await command.exec()
338338
await sleep(1200)
339339
})
340+
341+
test('error if --unstable-hmr and --watch are used together', async ({ assert, fs }) => {
342+
await fs.create('node_modules/ts-node/esm.js', '')
343+
344+
const ace = await new AceFactory().make(fs.baseUrl, {
345+
importer: (filePath) => import(filePath),
346+
})
347+
348+
ace.ui.switchMode('raw')
349+
350+
const command = await ace.create(Serve, ['--unstable-hmr', '--watch', '--no-clear'])
351+
await command.exec()
352+
353+
assert.equal(command.exitCode, 1)
354+
assert.lengthOf(ace.ui.logger.getLogs(), 1)
355+
assert.equal(ace.ui.logger.getLogs()[0].stream, 'stderr')
356+
assert.match(
357+
ace.ui.logger.getLogs()[0].message,
358+
/Cannot use --watch and --unstable-hmr flags together/
359+
)
360+
})
340361
})

0 commit comments

Comments
 (0)