forked from wilsonzlin/minify-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
194 lines (172 loc) · 6.54 KB
/
main.rs
File metadata and controls
194 lines (172 loc) · 6.54 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use minify_html::minify;
use minify_html::Cfg;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use std::fs::File;
use std::io::stdin;
use std::io::stdout;
use std::io::Read;
use std::io::Write;
use std::process::exit;
use std::sync::Arc;
use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(
name = "minhtml",
about = "Extremely fast and smart HTML + JS + CSS minifier"
)]
// WARNING: Keep descriptions in sync with Cfg.
struct Cli {
/// Files to minify; omit for stdin. If more than one is provided, they will be parallel minified in place, and --output must be omitted.
#[structopt(parse(from_os_str))]
inputs: Vec<std::path::PathBuf>,
/// Output destination; omit for stdout.
#[structopt(short, long, parse(from_os_str))]
output: Option<std::path::PathBuf>,
/// Allow unquoted attribute values in the output to contain characters prohibited by the [WHATWG specification](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). These will still be parsed correctly by almost all browsers.
#[structopt(long)]
allow_noncompliant_unquoted_attribute_values: bool,
/// Allow some minifications around entities that may not pass validation, but will still be parsed correctly by almost all browsers.
#[structopt(long)]
allow_optimal_entities: bool,
/// Allow removing_spaces between attributes when possible, which may not be spec compliant. These will still be parsed correctly by almost all browsers.
#[structopt(long)]
allow_removing_spaces_between_attributes: bool,
/// Do not omit closing tags when possible.
#[structopt(long)]
keep_closing_tags: bool,
/// Keep all comments.
#[structopt(long)]
keep_comments: bool,
/// Do not omit `<html>` and `<head>` opening tags when they don't have attributes.
#[structopt(long)]
keep_html_and_head_opening_tags: bool,
/// Keep `type=text` attribute name and value on `<input>` elements.
#[structopt(long)]
keep_input_type_text_attr: bool,
/// Keep SSI comments.
#[structopt(long)]
keep_ssi_comments: bool,
/// Minify CSS in `<style>` tags and `style` attributes using [https://github.com/parcel-bundler/lightningcss](lightningcss).
#[structopt(long)]
minify_css: bool,
/// Minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant, but will still be parsed correctly by almost all browsers.
#[structopt(long)]
minify_doctype: bool,
/// Minify JavaScript in `<script>` tags using
/// [minify-js](https://github.com/wilsonzlin/minify-js).
///
/// Only `<script>` tags with a valid or no
/// [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) is considered to
/// contain JavaScript, as per the specification.
#[structopt(long)]
minify_js: bool,
/// When `{{`, `{#`, or `{%` are seen in content, all source code until the subsequent matching closing `}}`, `#}`, or `%}` respectively gets piped through untouched.
#[structopt(long)]
preserve_brace_template_syntax: bool,
/// When `<%` is seen in content, all source code until the subsequent matching closing `%>` gets piped through untouched.
#[structopt(long)]
preserve_chevron_percent_template_syntax: bool,
/// Remove all bangs.
#[structopt(long)]
remove_bangs: bool,
/// Remove all processing instructions.
#[structopt(long)]
remove_processing_instructions: bool,
}
macro_rules! io_expect {
($name:expr, $expr:expr, $msg:literal) => {
match $expr {
Ok(r) => r,
Err(e) => {
eprintln!("[{}] {}: {}", $name, $msg, e);
exit(1);
}
}
};
}
fn main() {
let args = Cli::from_args();
if args.output.is_some() && args.inputs.len() > 1 {
eprintln!("Cannot provide --output when multiple inputs are provided.");
exit(1);
};
#[rustfmt::skip]
let cfg = Arc::new(Cfg {
allow_noncompliant_unquoted_attribute_values: args.allow_noncompliant_unquoted_attribute_values,
allow_optimal_entities: args.allow_optimal_entities,
allow_removing_spaces_between_attributes: args.allow_removing_spaces_between_attributes,
keep_closing_tags: args.keep_closing_tags,
keep_comments: args.keep_comments,
keep_html_and_head_opening_tags: args.keep_html_and_head_opening_tags,
keep_input_type_text_attr: args.keep_input_type_text_attr,
keep_ssi_comments: args.keep_ssi_comments,
minify_css: args.minify_css,
minify_doctype: args.minify_doctype,
minify_js: args.minify_js,
preserve_brace_template_syntax: args.preserve_brace_template_syntax,
preserve_chevron_percent_template_syntax: args.preserve_chevron_percent_template_syntax,
remove_bangs: args.remove_bangs,
remove_processing_instructions: args.remove_processing_instructions,
});
if args.inputs.len() <= 1 {
// Single file mode or stdin mode.
let input_name = args
.inputs
.get(0)
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_else(|| "stdin".to_string());
let mut src_file: Box<dyn Read> = match args.inputs.get(0) {
Some(p) => Box::new(io_expect!(
input_name,
File::open(p),
"Could not open source file"
)),
None => Box::new(stdin()),
};
let mut src_code = Vec::<u8>::new();
io_expect!(
input_name,
src_file.read_to_end(&mut src_code),
"Could not load source code"
);
let out_code = minify(&src_code, &cfg);
let mut out_file: Box<dyn Write> = match args.output {
Some(p) => Box::new(io_expect!(
input_name,
File::create(p),
"Could not open output file"
)),
None => Box::new(stdout()),
};
io_expect!(
input_name,
out_file.write_all(&out_code),
"Could not save minified code"
);
} else {
args.inputs.par_iter().for_each(|input| {
let input_name = input.to_string_lossy().into_owned();
let mut src_file = io_expect!(input_name, File::open(input), "Could not open source file");
let mut src_code = Vec::<u8>::new();
io_expect!(
input_name,
src_file.read_to_end(&mut src_code),
"Could not load source code"
);
let out_code = minify(&src_code, &cfg);
let mut out_file = io_expect!(
input_name,
File::create(input),
"Could not open output file"
);
io_expect!(
input_name,
out_file.write_all(&out_code),
"Could not save minified code"
);
// Just print the name, since this is the default output and any prefix becomes redundant. It'd also allow piping into another command (quite nice for something like `minify-html *.html | xargs gzip`), copying as list of files, etc.
println!("{}", input_name);
});
}
}