Skip to content

Commit 72de4f3

Browse files
authored
feat(vite_task): proxy and skip cache for vite dev (#147)
This is still a temporary workaround. The final implementation requires a task-aware runner.
1 parent bc6dc74 commit 72de4f3

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

crates/vite_task/src/config/task_command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl From<TaskCommand> for TaskConfig {
4141
}
4242

4343
impl TaskCommand {
44-
pub fn is_vite(&self) -> bool {
45-
matches!(self, Self::Parsed(parsed_command) if parsed_command.program == "vite")
44+
pub fn need_skip_cache(&self) -> bool {
45+
matches!(self, Self::Parsed(parsed_command) if parsed_command.program == "vite" || (parsed_command.program.ends_with("vite.js") && parsed_command.args.first() == Some(&("dev".into()))))
4646
}
4747
}
4848

crates/vite_task/src/execute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ pub async fn execute_task(
335335
cmd
336336
}
337337
TaskCommand::Parsed(task_parsed_command) => {
338-
if resolved_command.fingerprint.command.is_vite() {
339-
let mut child = tokio::process::Command::new("vite")
338+
if resolved_command.fingerprint.command.need_skip_cache() {
339+
let mut child = tokio::process::Command::new(&task_parsed_command.program)
340340
.args(&task_parsed_command.args)
341341
.envs(&resolved_command.all_envs)
342342
.envs(&task_parsed_command.envs)

crates/vite_task/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ pub enum Commands {
9696
/// Arguments to pass to vite install
9797
args: Vec<String>,
9898
},
99+
Dev {
100+
#[clap(last = true)]
101+
/// Arguments to pass to vite dev
102+
args: Vec<String>,
103+
},
99104
/// Manage the task cache
100105
Cache {
101106
#[clap(subcommand)]
@@ -334,6 +339,15 @@ pub async fn main<
334339
let exit_status = install::InstallCommand::builder(cwd).build().execute(args).await?;
335340
return Ok(exit_status);
336341
}
342+
Some(Commands::Dev { args }) => {
343+
let mut workspace = Workspace::partial_load(cwd)?;
344+
if let Some(vite_fn) = options.map(|o| o.vite) {
345+
let exit_status = vite::create_vite("dev", vite_fn, &mut workspace, args).await?;
346+
workspace.unload().await?;
347+
return Ok(exit_status);
348+
}
349+
return Ok(None);
350+
}
337351
Some(Commands::Cache { subcmd }) => {
338352
let cache_path = Workspace::get_cache_path(&cwd)?;
339353
match subcmd {
@@ -472,8 +486,8 @@ mod tests {
472486
fn test_args_with_task_args() {
473487
// Now "test" is a dedicated command, so let's use a different task name for implicit mode
474488
let args =
475-
Args::try_parse_from(&["vite-plus", "dev", "--", "--watch", "--verbose"]).unwrap();
476-
assert_eq!(args.task, Some("dev".into()));
489+
Args::try_parse_from(&["vite-plus", "develop", "--", "--watch", "--verbose"]).unwrap();
490+
assert_eq!(args.task, Some("develop".into()));
477491
assert_eq!(args.task_args, vec!["--watch", "--verbose"]);
478492
assert!(args.commands.is_none());
479493
assert!(!args.debug);

crates/vite_task/src/schedule.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ async fn get_cached_or_execute<'a>(
205205
Err(cache_miss) => (
206206
Some(cache_miss),
207207
async move {
208-
let is_vite = task.resolved_command.fingerprint.command.is_vite();
208+
let skip_cache = task.resolved_command.fingerprint.command.need_skip_cache();
209209
let executed_task = execute_task(&task.resolved_command, base_dir).await?;
210210
let exit_status = executed_task.exit_status;
211-
if !is_vite && exit_status.success() {
211+
if !skip_cache && exit_status.success() {
212212
let cached_task = CommandCacheValue::create(executed_task, fs, base_dir)?;
213213
cache.update(&task, cached_task).await?;
214214
}

0 commit comments

Comments
 (0)