forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
42 lines (36 loc) · 1.13 KB
/
main.rs
File metadata and controls
42 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::fs;
use clap::{Args, Parser};
use parcel_css::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet};
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
enum Cli {
/// Minify a given CSS file
Minify(MinifyArgs),
}
#[derive(Args, Debug)]
struct MinifyArgs {
/// The CSS file to minify
input_file: String,
/// The name of the output file
#[clap(short, long)]
output_file: Option<String>,
}
pub fn main() -> Result<(), std::io::Error> {
match Cli::parse() {
Cli::Minify(cli_args) => {
minify_css(cli_args)
}
}
}
fn minify_css(cli_args: MinifyArgs) -> Result<(), std::io::Error> {
let source = fs::read_to_string(&cli_args.input_file)?;
let mut stylesheet = StyleSheet::parse(cli_args.input_file.to_string(), &source, ParserOptions::default()).unwrap();
stylesheet.minify(MinifyOptions::default()).unwrap();
let res = stylesheet.to_css(PrinterOptions { minify: true, ..PrinterOptions::default()}).unwrap();
if let Some(output_file) = cli_args.output_file {
fs::write(output_file, res.code.as_bytes())?;
} else {
println!("{}", res.code);
}
Ok(())
}