@@ -4,7 +4,7 @@ use std::sync::Arc;
44
55use monostate:: MustBe ;
66use rustc_hash:: FxHashMap ;
7- use serde:: { Deserialize , Serialize } ;
7+ use serde:: Deserialize ;
88#[ cfg( all( test, not( clippy) ) ) ]
99use ts_rs:: TS ;
1010use vite_path:: RelativePathBuf ;
@@ -193,42 +193,46 @@ impl Default for UserTaskOptions {
193193 }
194194}
195195
196- /// Task command: a command string or a sequence of command strings .
197- #[ derive( Debug , Deserialize , Serialize , PartialEq , Eq ) ]
196+ /// Full user-defined task configuration in `vite.config.*`, including the command and options .
197+ #[ derive( Debug , Deserialize , PartialEq , Eq ) ]
198198// TS derive macro generates code using std types that clippy disallows; skip derive during linting
199- #[ cfg_attr( all( test, not( clippy) ) , derive( TS ) ) ]
200- #[ serde( untagged) ]
201- pub enum TaskCommand {
202- /// A single command string.
203- String ( Str ) ,
204- /// Command strings to run in order.
205- Array ( Vec < Str > ) ,
206- }
199+ #[ cfg_attr( all( test, not( clippy) ) , derive( TS ) , ts( optional_fields, rename = "Task" ) ) ]
200+ #[ serde( untagged, rename_all = "camelCase" ) ]
201+ pub enum UserTaskConfig {
202+ /// Task object form with a single command string.
203+ String {
204+ /// Command string to run for the task.
205+ command : Str ,
206+
207+ /// Fields other than the command
208+ #[ serde( flatten) ]
209+ options : UserTaskOptions ,
210+ } ,
211+ /// Task object form with a sequence of command strings.
212+ Array {
213+ /// Command strings to run for the task.
214+ command : Arc < [ Str ] > ,
207215
208- impl From < & str > for TaskCommand {
209- fn from ( value : & str ) -> Self {
210- Self :: String ( value . into ( ) )
211- }
216+ /// Fields other than the command
217+ # [ serde ( flatten ) ]
218+ options : UserTaskOptions ,
219+ } ,
212220}
213221
214- impl From < Str > for TaskCommand {
215- fn from ( value : Str ) -> Self {
216- Self :: String ( value)
222+ impl UserTaskConfig {
223+ #[ must_use]
224+ pub const fn options ( & self ) -> & UserTaskOptions {
225+ match self {
226+ Self :: String { options, .. } | Self :: Array { options, .. } => options,
227+ }
217228 }
218- }
219229
220- /// Full user-defined task configuration in `vite.config.*`, including the command and options.
221- #[ derive( Debug , Deserialize , PartialEq , Eq ) ]
222- // TS derive macro generates code using std types that clippy disallows; skip derive during linting
223- #[ cfg_attr( all( test, not( clippy) ) , derive( TS ) , ts( optional_fields, rename = "Task" ) ) ]
224- #[ serde( rename_all = "camelCase" ) ]
225- pub struct UserTaskConfig {
226- /// Command string or sequence of command strings to run for the task.
227- pub command : TaskCommand ,
228-
229- /// Fields other than the command
230- #[ serde( flatten) ]
231- pub options : UserTaskOptions ,
230+ pub fn into_parts ( self ) -> ( Arc < [ Str ] > , UserTaskOptions ) {
231+ match self {
232+ Self :: String { command, options } => ( vec ! [ command] . into ( ) , options) ,
233+ Self :: Array { command, options } => ( command, options) ,
234+ }
235+ }
232236}
233237
234238/// User-defined task configuration or command-only shorthand in `vite.config.*`.
@@ -240,7 +244,9 @@ pub enum UserTaskDefinition {
240244 /// Full task object form.
241245 Config ( UserTaskConfig ) ,
242246 /// Command-only shorthand form using default task options.
243- Command ( TaskCommand ) ,
247+ CommandString ( Str ) ,
248+ /// Command sequence shorthand form using default task options.
249+ CommandArray ( Arc < [ Str ] > ) ,
244250}
245251
246252/// Root-level cache configuration.
@@ -449,7 +455,10 @@ mod tests {
449455 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
450456 assert_eq ! (
451457 user_config,
452- UserTaskConfig { command: "echo hello" . into( ) , options: UserTaskOptions :: default ( ) }
458+ UserTaskConfig :: String {
459+ command: "echo hello" . into( ) ,
460+ options: UserTaskOptions :: default ( )
461+ }
453462 ) ;
454463 }
455464
@@ -459,11 +468,12 @@ mod tests {
459468 "command" : [ "echo one" , "echo two" , "echo three" ]
460469 } ) ;
461470 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
471+ let ( commands, options) = user_config. into_parts ( ) ;
462472 assert_eq ! (
463- user_config . command ,
464- TaskCommand :: Array ( vec! [ "echo one" . into( ) , "echo two" . into( ) , "echo three" . into( ) ] )
473+ commands ,
474+ Arc :: from ( [ "echo one" . into( ) , "echo two" . into( ) , "echo three" . into( ) ] )
465475 ) ;
466- assert_eq ! ( user_config . options, UserTaskOptions :: default ( ) ) ;
476+ assert_eq ! ( options, UserTaskOptions :: default ( ) ) ;
467477 }
468478
469479 #[ test]
@@ -475,7 +485,7 @@ mod tests {
475485 } ) ;
476486 let mut user_config: UserRunConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
477487 let task = user_config. tasks . as_mut ( ) . unwrap ( ) . remove ( "build" ) . unwrap ( ) ;
478- assert_eq ! ( task, UserTaskDefinition :: Command ( TaskCommand :: String ( "echo build" . into( ) ) ) ) ;
488+ assert_eq ! ( task, UserTaskDefinition :: CommandString ( "echo build" . into( ) ) ) ;
479489 }
480490
481491 #[ test]
@@ -489,7 +499,7 @@ mod tests {
489499 let task = user_config. tasks . as_mut ( ) . unwrap ( ) . remove ( "build" ) . unwrap ( ) ;
490500 assert_eq ! (
491501 task,
492- UserTaskDefinition :: Command ( TaskCommand :: Array ( vec! [
502+ UserTaskDefinition :: CommandArray ( Arc :: from ( [
493503 "echo one" . into( ) ,
494504 "echo two" . into( ) ,
495505 "echo three" . into( )
@@ -506,16 +516,11 @@ mod tests {
506516 "cache" : false
507517 } ) ;
508518 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
509- assert_eq ! (
510- user_config. command,
511- TaskCommand :: Array ( vec![ "echo one" . into( ) , "echo two" . into( ) ] )
512- ) ;
513- assert_eq ! ( user_config. options. cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
514- assert_eq ! ( user_config. options. depends_on. as_ref( ) . unwrap( ) . as_ref( ) , [ Str :: from( "build" ) ] ) ;
515- assert_eq ! (
516- user_config. options. cache_config,
517- UserCacheConfig :: Disabled { cache: MustBe !( false ) }
518- ) ;
519+ let ( commands, options) = user_config. into_parts ( ) ;
520+ assert_eq ! ( commands, Arc :: from( [ "echo one" . into( ) , "echo two" . into( ) ] ) ) ;
521+ assert_eq ! ( options. cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
522+ assert_eq ! ( options. depends_on. as_ref( ) . unwrap( ) . as_ref( ) , [ Str :: from( "build" ) ] ) ;
523+ assert_eq ! ( options. cache_config, UserCacheConfig :: Disabled { cache: MustBe !( false ) } ) ;
519524 }
520525
521526 #[ test]
@@ -543,7 +548,7 @@ mod tests {
543548 "cwd" : "src"
544549 } ) ;
545550 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
546- assert_eq ! ( user_config. options. cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
551+ assert_eq ! ( user_config. options( ) . cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
547552 }
548553
549554 #[ test]
@@ -554,7 +559,7 @@ mod tests {
554559 } ) ;
555560 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
556561 assert_eq ! (
557- user_config. options. cache_config,
562+ user_config. options( ) . cache_config,
558563 UserCacheConfig :: Disabled { cache: MustBe !( false ) }
559564 ) ;
560565 }
0 commit comments