Skip to content

Commit c2c350b

Browse files
fengmk2claude
andcommitted
feat(cli): default to dev command when no subcommand provided
Running `vite` without arguments or with options (e.g., `vite --port 3000`) now defaults to the dev command, matching the behavior of the upstream Vite CLI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 317191b commit c2c350b

5 files changed

Lines changed: 97 additions & 20 deletions

File tree

packages/cli/binding/src/lib.rs

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ impl From<JsCommandResolvedResult> for ResolveCommandResult {
8080
}
8181
}
8282

83-
static BUILTIN_COMMANDS: &[&str] = &["lint", "fmt", "build", "test", "doc", "lib", "preview"];
83+
static BUILTIN_COMMANDS: &[&str] =
84+
&["dev", "lint", "fmt", "build", "test", "doc", "lib", "preview"];
8485

8586
/// Main entry point for the CLI, called from JavaScript.
8687
///
@@ -267,26 +268,59 @@ fn js_error_to_resolve_universal_vite_config_error(err: napi::Error) -> Error {
267268
fn parse_args() -> Args {
268269
// ArgsOs [node, vite-plus, ...]
269270
let mut raw_args = std::env::args_os().skip(2);
270-
if let Some(first) = raw_args.next()
271-
&& let Some(first) = first.to_str()
272-
&& BUILTIN_COMMANDS.contains(&first)
273-
{
274-
let forwarded_args = raw_args
275-
.map(|a| a.into_string().unwrap_or_else(|os_str| os_str.to_string_lossy().into_owned()))
276-
.collect();
271+
if let Some(first) = raw_args.next() {
272+
if let Some(first_str) = first.to_str() {
273+
if BUILTIN_COMMANDS.contains(&first_str) {
274+
let forwarded_args = raw_args
275+
.map(|a| {
276+
a.into_string()
277+
.unwrap_or_else(|os_str| os_str.to_string_lossy().into_owned())
278+
})
279+
.collect();
280+
return Args {
281+
task: None,
282+
task_args: vec![],
283+
commands: match first_str {
284+
"dev" => Commands::Dev { args: forwarded_args },
285+
"lint" => Commands::Lint { args: forwarded_args },
286+
"fmt" => Commands::Fmt { args: forwarded_args },
287+
"build" => Commands::Build { args: forwarded_args },
288+
"test" => Commands::Test { args: forwarded_args },
289+
"doc" => Commands::Doc { args: forwarded_args },
290+
"lib" => Commands::Lib { args: forwarded_args },
291+
"preview" => Commands::Preview { args: forwarded_args },
292+
_ => unreachable!(),
293+
},
294+
debug: false,
295+
no_debug: true,
296+
};
297+
}
298+
// If first arg starts with '-' but is NOT a help/version flag, treat as options for dev command
299+
if first_str.starts_with('-')
300+
&& !matches!(first_str, "-h" | "--help" | "-V" | "--version")
301+
{
302+
let forwarded_args: Vec<String> = std::iter::once(first)
303+
.chain(raw_args)
304+
.map(|a| {
305+
a.into_string()
306+
.unwrap_or_else(|os_str| os_str.to_string_lossy().into_owned())
307+
})
308+
.collect();
309+
return Args {
310+
task: None,
311+
task_args: vec![],
312+
commands: Commands::Dev { args: forwarded_args },
313+
debug: false,
314+
no_debug: true,
315+
};
316+
}
317+
}
318+
} else {
319+
// No arguments provided, default to dev command
277320
return Args {
278321
task: None,
279322
task_args: vec![],
280-
commands: match first {
281-
"lint" => Commands::Lint { args: forwarded_args },
282-
"fmt" => Commands::Fmt { args: forwarded_args },
283-
"build" => Commands::Build { args: forwarded_args },
284-
"test" => Commands::Test { args: forwarded_args },
285-
"doc" => Commands::Doc { args: forwarded_args },
286-
"lib" => Commands::Lib { args: forwarded_args },
287-
"preview" => Commands::Preview { args: forwarded_args },
288-
_ => unreachable!(),
289-
},
323+
commands: Commands::Dev { args: vec![] },
290324
debug: false,
291325
no_debug: true,
292326
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
> vite dev --port 12312312312 2>&1 | grep RangeError # intentionally use an invalid port (exceeds 0-65535) to trigger RangeError
22
RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received type number (12312312312).
3+
4+
> vite --port 12312312313 2>&1 | grep RangeError # vite without args should be alias to dev command
5+
RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received type number (12312312313).

packages/cli/snap-tests/command-dev-with-port/steps.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"VITE_DISABLE_AUTO_INSTALL": "1"
55
},
66
"commands": [
7-
"vite dev --port 12312312312 2>&1 | grep RangeError # intentionally use an invalid port (exceeds 0-65535) to trigger RangeError"
7+
"vite dev --port 12312312312 2>&1 | grep RangeError # intentionally use an invalid port (exceeds 0-65535) to trigger RangeError",
8+
"vite --port 12312312313 2>&1 | grep RangeError # vite without args should be alias to dev command"
89
]
910
}

packages/cli/snap-tests/command-helper/snap.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,41 @@ Options:
357357
-m, --mode <mode> [string] set env mode
358358
-h, --help Display this message
359359

360+
361+
> vite dev -h # dev help message
362+
vite/<semver>
363+
364+
Usage:
365+
$ vite [root]
366+
367+
Commands:
368+
[root] start dev server
369+
build [root] build for production
370+
optimize [root] pre-bundle dependencies (deprecated, the pre-bundle process runs automatically and does not need to be called)
371+
preview [root] locally preview production build
372+
373+
For more info, run any command with the `--help` flag:
374+
$ vite --help
375+
$ vite build --help
376+
$ vite optimize --help
377+
$ vite preview --help
378+
379+
Options:
380+
--host [host] [string] specify hostname
381+
--port <port> [number] specify port
382+
--open [path] [boolean | string] open browser on startup
383+
--cors [boolean] enable CORS
384+
--strictPort [boolean] exit if specified port is already in use
385+
--force [boolean] force the optimizer to ignore the cache and re-bundle
386+
--experimentalBundle [boolean] use experimental full bundle mode (this is highly experimental)
387+
-c, --config <file> [string] use specified config file
388+
--base <path> [string] public base path (default: /)
389+
-l, --logLevel <level> [string] info | warn | error | silent
390+
--clearScreen [boolean] allow/disable clear screen when logging
391+
--configLoader <loader> [string] use 'bundle' to bundle the config with Rolldown, or 'runner' (experimental) to process it on the fly, or 'native' (experimental) to load using the native runtime (default: bundle)
392+
-d, --debug [feat] [string | boolean] show debug logs
393+
-f, --filter <filter> [string] filter debug logs
394+
-m, --mode <mode> [string] set env mode
395+
-h, --help Display this message
396+
-v, --version Display version number
397+

packages/cli/snap-tests/command-helper/steps.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"vite lint -h # lint help message",
1111
"vite build -h # build help message",
1212
"vite test -h # test help message",
13-
"vite preview -h # preview help message"
13+
"vite preview -h # preview help message",
14+
"vite dev -h # dev help message"
1415
]
1516
}

0 commit comments

Comments
 (0)