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
297 lines (269 loc) · 9.09 KB
/
main.rs
File metadata and controls
297 lines (269 loc) · 9.09 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use atty::Stream;
use clap::{ArgGroup, Parser};
use lightningcss::bundler::{Bundler, FileProvider};
use lightningcss::stylesheet::{MinifyOptions, ParserFlags, ParserOptions, PrinterOptions, StyleSheet};
use lightningcss::targets::Browsers;
use parcel_sourcemap::SourceMap;
use serde::Serialize;
use std::borrow::Cow;
use std::sync::{Arc, RwLock};
use std::{ffi, fs, io, path::Path};
#[cfg(target_os = "macos")]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[clap(group(
ArgGroup::new("targets-resolution")
.args(&["targets", "browserslist"]),
))]
struct CliArgs {
/// Target CSS file (default: stdin)
#[clap(value_parser)]
input_file: Vec<String>,
/// Destination file for the output
#[clap(short, long, group = "output_file", value_parser)]
output_file: Option<String>,
/// Destination directory to output into.
#[clap(short = 'd', long, group = "output_file", value_parser)]
output_dir: Option<String>,
/// Minify the output
#[clap(short, long, value_parser)]
minify: bool,
/// Enable parsing CSS nesting
#[clap(long, value_parser)]
nesting: bool,
/// Enable parsing custom media queries
#[clap(long, value_parser)]
custom_media: bool,
/// Enable CSS modules in output.
/// If no filename is provided, <output_file>.json will be used.
/// If no --output-file is specified, code and exports will be printed to stdout as JSON.
#[clap(long, group = "css_modules", value_parser)]
css_modules: Option<Option<String>>,
#[clap(long, requires = "css_modules", value_parser)]
css_modules_pattern: Option<String>,
#[clap(long, requires = "css_modules", value_parser)]
css_modules_dashed_idents: bool,
/// Enable sourcemap, at <output_file>.map
#[clap(long, requires = "output_file", value_parser)]
sourcemap: bool,
#[clap(long, value_parser)]
bundle: bool,
#[clap(short, long, value_parser)]
targets: Vec<String>,
#[clap(long, value_parser)]
browserslist: bool,
#[clap(long, value_parser)]
error_recovery: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SourceMapJson<'a> {
version: u8,
mappings: String,
sources: &'a Vec<String>,
sources_content: &'a Vec<String>,
names: &'a Vec<String>,
}
pub fn main() -> Result<(), std::io::Error> {
let cli_args = CliArgs::parse();
let project_root = std::env::current_dir()?;
// If we're given an input file, read from it and adjust its name.
//
// If we're not given an input file and stdin was redirected, read
// from it and create a fake name. Return an error if stdin was not
// redirected (otherwise the program will hang waiting for input).
//
let inputs = if !cli_args.input_file.is_empty() {
if cli_args.input_file.len() > 1 && cli_args.output_file.is_some() {
eprintln!("Cannot use the --output-file option with multiple inputs. Use --output-dir instead.");
std::process::exit(1);
}
if cli_args.input_file.len() > 1 && cli_args.output_file.is_none() && cli_args.output_dir.is_none() {
eprintln!("Cannot output to stdout with multiple inputs. Use --output-dir instead.");
std::process::exit(1);
}
cli_args
.input_file
.into_iter()
.map(|ref f| -> Result<_, std::io::Error> {
let absolute_path = fs::canonicalize(f)?;
let filename = pathdiff::diff_paths(absolute_path, &project_root).unwrap();
let filename = filename.to_string_lossy().into_owned();
let contents = fs::read_to_string(f)?;
Ok((filename, contents))
})
.collect::<Result<_, _>>()?
} else {
// Don't silently wait for input if stdin was not redirected.
if atty::is(Stream::Stdin) {
return Err(io::Error::new(
io::ErrorKind::Other,
"Not reading from stdin as it was not redirected",
));
}
let filename = format!("stdin-{}", std::process::id());
let contents = io::read_to_string(io::stdin())?;
vec![(filename, contents)]
};
let css_modules = if let Some(_) = cli_args.css_modules {
let pattern = if let Some(pattern) = cli_args.css_modules_pattern.as_ref() {
match lightningcss::css_modules::Pattern::parse(pattern) {
Ok(p) => p,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
}
} else {
Default::default()
};
Some(lightningcss::css_modules::Config {
pattern,
dashed_idents: cli_args.css_modules_dashed_idents,
..Default::default()
})
} else {
cli_args.css_modules.as_ref().map(|_| Default::default())
};
let fs = FileProvider::new();
for (filename, source) in inputs {
let warnings = if cli_args.error_recovery {
Some(Arc::new(RwLock::new(Vec::new())))
} else {
None
};
let mut source_map = if cli_args.sourcemap {
Some(SourceMap::new(&project_root.to_string_lossy()))
} else {
None
};
let output_file = if let Some(output_file) = &cli_args.output_file {
Some(Cow::Borrowed(Path::new(output_file)))
} else if let Some(dir) = &cli_args.output_dir {
Some(Cow::Owned(
Path::new(dir).join(Path::new(&filename).file_name().unwrap()),
))
} else {
None
};
let res = {
let mut flags = ParserFlags::empty();
flags.set(ParserFlags::NESTING, cli_args.nesting);
flags.set(ParserFlags::CUSTOM_MEDIA, cli_args.custom_media);
let mut options = ParserOptions {
flags,
css_modules: css_modules.clone(),
error_recovery: cli_args.error_recovery,
warnings: warnings.clone(),
..ParserOptions::default()
};
let mut stylesheet = if cli_args.bundle {
let mut bundler = Bundler::new(&fs, source_map.as_mut(), options);
bundler.bundle(Path::new(&filename)).unwrap()
} else {
if let Some(sm) = &mut source_map {
sm.add_source(&filename);
let _ = sm.set_source_content(0, &source);
}
options.filename = filename;
StyleSheet::parse(&source, options).unwrap()
};
let targets = if !cli_args.targets.is_empty() {
Browsers::from_browserslist(&cli_args.targets).unwrap()
} else if cli_args.browserslist {
Browsers::load_browserslist().unwrap()
} else {
None
}
.into();
stylesheet
.minify(MinifyOptions {
targets,
..MinifyOptions::default()
})
.unwrap();
stylesheet
.to_css(PrinterOptions {
minify: cli_args.minify,
source_map: source_map.as_mut(),
project_root: Some(&project_root.to_string_lossy()),
targets,
..PrinterOptions::default()
})
.unwrap()
};
let map = if let Some(ref mut source_map) = source_map {
let mut vlq_output: Vec<u8> = Vec::new();
source_map
.write_vlq(&mut vlq_output)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Error writing sourcemap vlq"))?;
let sm = SourceMapJson {
version: 3,
mappings: unsafe { String::from_utf8_unchecked(vlq_output) },
sources: source_map.get_sources(),
sources_content: source_map.get_sources_content(),
names: source_map.get_names(),
};
serde_json::to_vec(&sm).ok()
} else {
None
};
if let Some(warnings) = warnings {
let warnings = Arc::try_unwrap(warnings).unwrap().into_inner().unwrap();
for warning in warnings {
eprintln!("{}", warning);
}
}
if let Some(output_file) = &output_file {
let mut code = res.code;
if cli_args.sourcemap {
if let Some(map_buf) = map {
let map_filename = output_file.to_string_lossy() + ".map";
code += &format!("\n/*# sourceMappingURL={} */\n", map_filename);
fs::write(map_filename.as_ref(), map_buf)?;
}
}
if let Some(p) = output_file.parent() {
fs::create_dir_all(p)?
};
fs::write(output_file, code.as_bytes())?;
if let Some(css_modules) = &cli_args.css_modules {
let css_modules_filename = if let Some(name) = css_modules {
Cow::Borrowed(name)
} else {
Cow::Owned(infer_css_modules_filename(output_file.as_ref())?)
};
if let Some(exports) = res.exports {
let css_modules_json = serde_json::to_string(&exports)?;
fs::write(css_modules_filename.as_ref(), css_modules_json)?;
}
}
} else {
if let Some(exports) = res.exports {
println!(
"{}",
serde_json::json!({
"code": res.code,
"exports": exports
})
);
} else {
println!("{}", res.code);
}
}
}
Ok(())
}
fn infer_css_modules_filename(path: &Path) -> Result<String, std::io::Error> {
if path.extension() == Some(ffi::OsStr::new("json")) {
Err(io::Error::new(
io::ErrorKind::Other,
"Cannot infer a css modules json filename, since the output file extension is '.json'",
))
} else {
// unwrap: the filename option is a String from clap, so is valid utf-8
Ok(path.with_extension("json").to_str().unwrap().into())
}
}