-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcss.rs
159 lines (138 loc) · 4.29 KB
/
css.rs
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
use std::{hash::Hasher, path::Path};
use anyhow::Context;
use codemap::SpanLoc;
use grass::OutputStyle;
use lightningcss::{
printer::PrinterOptions,
stylesheet::{MinifyOptions, ParserOptions, StyleSheet},
targets::{Browsers, Targets},
};
use manganis_core::CssAssetOptions;
pub(crate) fn process_css(
css_options: &CssAssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
let css = std::fs::read_to_string(source)?;
let css = if css_options.minified() {
// Try to minify the css. If we fail, log the error and use the unminified css
match minify_css(&css) {
Ok(minified) => minified,
Err(err) => {
tracing::error!(
"Failed to minify css; Falling back to unminified css. Error: {}",
err
);
css
}
}
} else {
css
};
std::fs::write(output_path, css).with_context(|| {
format!(
"Failed to write css to output location: {}",
output_path.display()
)
})?;
Ok(())
}
pub(crate) fn minify_css(css: &str) -> anyhow::Result<String> {
let options = ParserOptions {
error_recovery: true,
..Default::default()
};
let mut stylesheet = StyleSheet::parse(css, options).map_err(|err| err.into_owned())?;
// We load the browser list from the standard browser list file or use the browserslist default if we don't find any
// settings. Without the browser lists default, lightningcss will default to supporting only the newest versions of
// browsers.
let browsers_list = match Browsers::load_browserslist()? {
Some(browsers) => Some(browsers),
None => {
Browsers::from_browserslist(["defaults"]).expect("borwserslists should have defaults")
}
};
let targets = Targets {
browsers: browsers_list,
..Default::default()
};
stylesheet.minify(MinifyOptions {
targets,
..Default::default()
})?;
let printer = PrinterOptions {
targets,
minify: true,
..Default::default()
};
let res = stylesheet.to_css(printer)?;
Ok(res.code)
}
/// Compile scss with grass
pub(crate) fn compile_scss(
scss_options: &CssAssetOptions,
source: &Path,
) -> anyhow::Result<String> {
let style = match scss_options.minified() {
true => OutputStyle::Compressed,
false => OutputStyle::Expanded,
};
let options = grass::Options::default()
.style(style)
.quiet(false)
.logger(&ScssLogger {});
let css = grass::from_path(source, &options)
.with_context(|| format!("Failed to compile scss file: {}", source.display()))?;
Ok(css)
}
/// Process an scss/sass file into css.
pub(crate) fn process_scss(
scss_options: &CssAssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
let css = compile_scss(scss_options, source)?;
let minified = minify_css(&css)?;
std::fs::write(output_path, minified).with_context(|| {
format!(
"Failed to write css to output location: {}",
output_path.display()
)
})?;
Ok(())
}
/// Logger for Grass that re-uses their StdLogger formatting but with tracing.
#[derive(Debug)]
struct ScssLogger {}
impl grass::Logger for ScssLogger {
fn debug(&self, location: SpanLoc, message: &str) {
tracing::debug!(
"{}:{} DEBUG: {}",
location.file.name(),
location.begin.line + 1,
message
);
}
fn warn(&self, location: SpanLoc, message: &str) {
tracing::warn!(
"Warning: {}\n ./{}:{}:{}",
message,
location.file.name(),
location.begin.line + 1,
location.begin.column + 1
);
}
}
/// Hash the inputs to the scss file
pub(crate) fn hash_scss(
scss_options: &CssAssetOptions,
source: &Path,
hasher: &mut impl Hasher,
) -> anyhow::Result<()> {
// Grass doesn't expose the ast for us to traverse the imports in the file. Instead of parsing scss ourselves
// we just hash the expanded version of the file for now
let css = compile_scss(scss_options, source)?;
// Hash the compiled css
hasher.write(css.as_bytes());
Ok(())
}