@@ -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,47 @@ 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- }
219-
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 ,
228229
229- /// Fields other than the command
230- #[ serde( flatten) ]
231- pub options : UserTaskOptions ,
230+ #[ must_use]
231+ pub fn into_parts ( self ) -> ( Arc < [ Str ] > , UserTaskOptions ) {
232+ match self {
233+ Self :: String { command, options } => ( vec ! [ command] . into ( ) , options) ,
234+ Self :: Array { command, options } => ( command, options) ,
235+ }
236+ }
232237}
233238
234239/// User-defined task configuration or command-only shorthand in `vite.config.*`.
@@ -240,7 +245,9 @@ pub enum UserTaskDefinition {
240245 /// Full task object form.
241246 Config ( UserTaskConfig ) ,
242247 /// Command-only shorthand form using default task options.
243- Command ( TaskCommand ) ,
248+ CommandString ( Str ) ,
249+ /// Command sequence shorthand form using default task options.
250+ CommandArray ( Arc < [ Str ] > ) ,
244251}
245252
246253/// Root-level cache configuration.
@@ -449,7 +456,10 @@ mod tests {
449456 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
450457 assert_eq ! (
451458 user_config,
452- UserTaskConfig { command: "echo hello" . into( ) , options: UserTaskOptions :: default ( ) }
459+ UserTaskConfig :: String {
460+ command: "echo hello" . into( ) ,
461+ options: UserTaskOptions :: default ( )
462+ }
453463 ) ;
454464 }
455465
@@ -459,11 +469,12 @@ mod tests {
459469 "command" : [ "echo one" , "echo two" , "echo three" ]
460470 } ) ;
461471 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
472+ let ( commands, options) = user_config. into_parts ( ) ;
462473 assert_eq ! (
463- user_config . command ,
464- TaskCommand :: Array ( vec! [ "echo one" . into( ) , "echo two" . into( ) , "echo three" . into( ) ] )
474+ commands ,
475+ Arc :: from ( [ "echo one" . into( ) , "echo two" . into( ) , "echo three" . into( ) ] )
465476 ) ;
466- assert_eq ! ( user_config . options, UserTaskOptions :: default ( ) ) ;
477+ assert_eq ! ( options, UserTaskOptions :: default ( ) ) ;
467478 }
468479
469480 #[ test]
@@ -475,7 +486,7 @@ mod tests {
475486 } ) ;
476487 let mut user_config: UserRunConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
477488 let task = user_config. tasks . as_mut ( ) . unwrap ( ) . remove ( "build" ) . unwrap ( ) ;
478- assert_eq ! ( task, UserTaskDefinition :: Command ( TaskCommand :: String ( "echo build" . into( ) ) ) ) ;
489+ assert_eq ! ( task, UserTaskDefinition :: CommandString ( "echo build" . into( ) ) ) ;
479490 }
480491
481492 #[ test]
@@ -489,7 +500,7 @@ mod tests {
489500 let task = user_config. tasks . as_mut ( ) . unwrap ( ) . remove ( "build" ) . unwrap ( ) ;
490501 assert_eq ! (
491502 task,
492- UserTaskDefinition :: Command ( TaskCommand :: Array ( vec! [
503+ UserTaskDefinition :: CommandArray ( Arc :: from ( [
493504 "echo one" . into( ) ,
494505 "echo two" . into( ) ,
495506 "echo three" . into( )
@@ -506,16 +517,11 @@ mod tests {
506517 "cache" : false
507518 } ) ;
508519 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- ) ;
520+ let ( commands, options) = user_config. into_parts ( ) ;
521+ assert_eq ! ( commands, Arc :: from( [ "echo one" . into( ) , "echo two" . into( ) ] ) ) ;
522+ assert_eq ! ( options. cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
523+ assert_eq ! ( options. depends_on. as_ref( ) . unwrap( ) . as_ref( ) , [ Str :: from( "build" ) ] ) ;
524+ assert_eq ! ( options. cache_config, UserCacheConfig :: Disabled { cache: MustBe !( false ) } ) ;
519525 }
520526
521527 #[ test]
@@ -543,7 +549,7 @@ mod tests {
543549 "cwd" : "src"
544550 } ) ;
545551 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" ) ;
552+ assert_eq ! ( user_config. options( ) . cwd_relative_to_package. as_ref( ) . unwrap( ) . as_str( ) , "src" ) ;
547553 }
548554
549555 #[ test]
@@ -554,7 +560,7 @@ mod tests {
554560 } ) ;
555561 let user_config: UserTaskConfig = serde_json:: from_value ( user_config_json) . unwrap ( ) ;
556562 assert_eq ! (
557- user_config. options. cache_config,
563+ user_config. options( ) . cache_config,
558564 UserCacheConfig :: Disabled { cache: MustBe !( false ) }
559565 ) ;
560566 }
0 commit comments