cargo_leptos/config/
cli.rs

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