cargo_leptos/config/
cli.rs

1use crate::command::NewCommand;
2use camino::Utf8PathBuf;
3use clap::{Parser, Subcommand, ValueEnum};
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
6pub enum Log {
7    /// WASM build (wasm, wasm-opt, walrus)
8    Wasm,
9    /// Internal reload and csr server (hyper, axum)
10    Server,
11}
12
13#[derive(Debug, Clone, Parser, PartialEq, Default)]
14pub struct Opts {
15    /// Build artifacts in release mode, with optimizations.
16    #[arg(short, long)]
17    pub release: bool,
18
19    /// Precompress static assets with gzip and brotli. Applies to release builds only.
20    #[arg(short = 'P', long)]
21    pub precompress: bool,
22
23    /// Turn on partial hot-reloading. Requires rust nightly [beta]
24    #[arg(long)]
25    pub hot_reload: bool,
26
27    /// Which project to use, from a list of projects defined in a workspace
28    #[arg(short, long)]
29    pub project: Option<String>,
30
31    /// The features to use when compiling all targets
32    #[arg(long)]
33    pub features: Vec<String>,
34
35    /// The features to use when compiling the lib target
36    #[arg(long)]
37    pub lib_features: Vec<String>,
38
39    /// The cargo flags to pass to cargo when compiling the lib target
40    #[arg(long)]
41    pub lib_cargo_args: Option<Vec<String>>,
42
43    /// The features to use when compiling the bin target
44    #[arg(long)]
45    pub bin_features: Vec<String>,
46
47    /// The cargo flags to pass to cargo when compiling the bin target
48    #[arg(long)]
49    pub bin_cargo_args: Option<Vec<String>>,
50
51    /// Include debug information in Wasm output. Includes source maps and DWARF debug info.
52    #[arg(long)]
53    pub wasm_debug: bool,
54
55    /// Verbosity (none: info, errors & warnings, -v: verbose, -vv: very verbose).
56    #[arg(short, action = clap::ArgAction::Count)]
57    pub verbose: u8,
58
59    /// Minify javascript assets with swc. Applies to release builds only.
60    #[arg(long, default_value = "true", value_parser=clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
61    pub js_minify: bool,
62}
63
64#[derive(Debug, Clone, Parser, PartialEq, Default)]
65pub struct BinOpts {
66    #[command(flatten)]
67    opts: Opts,
68
69    #[arg(trailing_var_arg = true)]
70    bin_args: Vec<String>,
71}
72
73#[derive(Debug, Parser)]
74#[clap(version)]
75pub struct Cli {
76    /// Path to Cargo.toml.
77    #[arg(long)]
78    pub manifest_path: Option<Utf8PathBuf>,
79
80    /// Output logs from dependencies (multiple --log accepted).
81    #[arg(long)]
82    pub log: Vec<Log>,
83
84    #[command(subcommand)]
85    pub command: Commands,
86}
87
88impl Cli {
89    pub fn opts(&self) -> Option<Opts> {
90        match &self.command {
91            Commands::New(_) => None,
92            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(bin_opts.opts.clone()),
93            Commands::Build(opts) | Commands::Test(opts) | Commands::EndToEnd(opts) => {
94                Some(opts.clone())
95            }
96        }
97    }
98
99    pub fn opts_mut(&mut self) -> Option<&mut Opts> {
100        match &mut self.command {
101            Commands::New(_) => None,
102            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(&mut bin_opts.opts),
103            Commands::Build(opts) | Commands::Test(opts) | Commands::EndToEnd(opts) => Some(opts),
104        }
105    }
106
107    pub fn bin_args(&self) -> Option<&[String]> {
108        match &self.command {
109            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => {
110                Some(bin_opts.bin_args.as_ref())
111            }
112            _ => None,
113        }
114    }
115}
116
117#[derive(Debug, Subcommand, PartialEq)]
118pub enum Commands {
119    /// Build the server (feature ssr) and the client (wasm with feature hydrate).
120    Build(Opts),
121    /// Run the cargo tests for app, client and server.
122    Test(Opts),
123    /// Start the server and end-2-end tests.
124    EndToEnd(Opts),
125    /// Serve. Defaults to hydrate mode.
126    Serve(BinOpts),
127    /// Serve and automatically reload when files change.
128    Watch(BinOpts),
129    /// Start a wizard for creating a new project (using cargo-generate).
130    New(NewCommand),
131}