Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --hmr flag for the serve command #4504

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default class Serve extends BaseCommand {
'{{ binaryName }} serve --watch',
'```',
'',
'You can also start the server with HMR support using the following command.',
'```',
'{{ binaryName }} serve --unstable-hmr',
'```',
'',
'The assets bundler dev server runs automatically after detecting vite config or webpack config files',
'You may pass vite CLI args using the --assets-args command line flag.',
'```',
Expand All @@ -41,6 +46,9 @@ export default class Serve extends BaseCommand {

declare devServer: DevServer

@flags.boolean({ description: 'Start the server with HMR support' })
declare unstableHmr?: boolean

@flags.boolean({
description: 'Watch filesystem and restart the HTTP server on file change',
alias: 'w',
Expand Down Expand Up @@ -112,7 +120,14 @@ export default class Serve extends BaseCommand {
return
}

if (this.watch && this.unstableHmr) {
this.logger.error('Cannot use --watch and --unstable-hmr flags together. Choose one of them')
this.exitCode = 1
return
}

this.devServer = new assembler.DevServer(this.app.appRoot, {
hmr: this.unstableHmr === true ? true : false,
clearScreen: this.clear === false ? false : true,
nodeArgs: this.parsed.nodeArgs,
scriptArgs: [],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"index:commands": "node --loader=ts-node/esm toolkit/main.js index build/commands"
},
"devDependencies": {
"@adonisjs/assembler": "^7.4.0",
"@adonisjs/assembler": "^7.5.0",
"@adonisjs/eslint-config": "^1.3.0",
"@adonisjs/prettier-config": "^1.3.0",
"@adonisjs/tsconfig": "^1.3.0",
Expand Down Expand Up @@ -143,7 +143,7 @@
"youch-terminal": "^2.2.3"
},
"peerDependencies": {
"@adonisjs/assembler": "^7.1.0",
"@adonisjs/assembler": "^7.5.0",
"@vinejs/vine": "^2.0.0",
"argon2": "^0.31.2 || ^0.40.0",
"bcrypt": "^5.1.1",
Expand Down
21 changes: 21 additions & 0 deletions tests/commands/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,25 @@ test.group('Serve command', () => {
await command.exec()
await sleep(1200)
})

test('error if --unstable-hmr and --watch are used together', async ({ assert, fs }) => {
await fs.create('node_modules/ts-node/esm.js', '')

const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})

ace.ui.switchMode('raw')

const command = await ace.create(Serve, ['--unstable-hmr', '--watch', '--no-clear'])
await command.exec()

assert.equal(command.exitCode, 1)
assert.lengthOf(ace.ui.logger.getLogs(), 1)
assert.equal(ace.ui.logger.getLogs()[0].stream, 'stderr')
assert.match(
ace.ui.logger.getLogs()[0].message,
/Cannot use --watch and --unstable-hmr flags together/
)
})
})