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    /// Split WASM binary based on #[lazy] macros.
64    #[arg(long)]
65    pub split: bool,
66
67    /// Only build the frontend.
68    #[arg(long)]
69    pub frontend_only: bool,
70
71    /// Only build the server.
72    #[arg(long, conflicts_with="frontend_only")]
73    pub server_only: bool,
74}
75
76#[derive(Debug, Clone, Parser, PartialEq, Default)]
77pub struct BinOpts {
78    #[command(flatten)]
79    opts: Opts,
80
81    #[arg(trailing_var_arg = true)]
82    bin_args: Vec<String>,
83}
84
85#[derive(Debug, Parser)]
86#[clap(version)]
87pub struct Cli {
88    /// Path to Cargo.toml.
89    #[arg(long)]
90    pub manifest_path: Option<Utf8PathBuf>,
91
92    /// Output logs from dependencies (multiple --log accepted).
93    #[arg(long)]
94    pub log: Vec<Log>,
95
96    #[command(subcommand)]
97    pub command: Commands,
98}
99
100impl Cli {
101    pub fn opts(&self) -> Option<Opts> {
102        match &self.command {
103            Commands::New(_) => None,
104            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(bin_opts.opts.clone()),
105            Commands::Build(opts) | Commands::Test(opts) | Commands::EndToEnd(opts) => {
106                Some(opts.clone())
107            }
108        }
109    }
110
111    pub fn opts_mut(&mut self) -> Option<&mut Opts> {
112        match &mut self.command {
113            Commands::New(_) => None,
114            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => Some(&mut bin_opts.opts),
115            Commands::Build(opts) | Commands::Test(opts) | Commands::EndToEnd(opts) => Some(opts),
116        }
117    }
118
119    pub fn bin_args(&self) -> Option<&[String]> {
120        match &self.command {
121            Commands::Serve(bin_opts) | Commands::Watch(bin_opts) => {
122                Some(bin_opts.bin_args.as_ref())
123            }
124            _ => None,
125        }
126    }
127}
128
129#[derive(Debug, Subcommand, PartialEq)]
130pub enum Commands {
131    /// Build the server (feature ssr) and the client (wasm with feature hydrate).
132    Build(Opts),
133    /// Run the cargo tests for app, client and server.
134    Test(Opts),
135    /// Start the server and end-2-end tests.
136    EndToEnd(Opts),
137    /// Serve. Defaults to hydrate mode.
138    Serve(BinOpts),
139    /// Serve and automatically reload when files change.
140    Watch(BinOpts),
141    /// Start a wizard for creating a new project (using cargo-generate).
142    New(NewCommand),
143}