Skip to content

Commit 445dbeb

Browse files
cynecxJoshua Nelson
authored and
Joshua Nelson
committed
Extract vendored css from style.css into a separate file vendored.css (Fixes #992)
1 parent 944e7fc commit 445dbeb

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

build.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
);
1616

1717
// Don't rerun anytime a single change is made
18+
println!("cargo:rerun-if-changed=templates/style/vendored.scss");
1819
println!("cargo:rerun-if-changed=templates/style/base.scss");
1920
println!("cargo:rerun-if-changed=templates/style/_rustdoc.scss");
2021
println!("cargo:rerun-if-changed=templates/style/_vars.scss");
@@ -57,30 +58,54 @@ fn get_git_hash() -> Option<String> {
5758
})
5859
}
5960

60-
fn compile_sass() -> Result<(), Box<dyn Error>> {
61+
fn compile_sass_file(
62+
name: &str,
63+
target: &str,
64+
include_paths: &[String],
65+
) -> Result<(), Box<dyn Error>> {
6166
use sass_rs::{Context, Options, OutputStyle};
6267

6368
const STYLE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/templates/style");
6469

65-
let mut context = Context::new_file(format!("{}/base.scss", STYLE_DIR))?;
70+
let include_paths = {
71+
let mut paths = vec![STYLE_DIR.to_owned()];
72+
paths.extend_from_slice(include_paths);
73+
paths
74+
};
75+
76+
// Compile base.scss
77+
let mut context = Context::new_file(format!("{}/{}.scss", STYLE_DIR, name))?;
6678
context.set_options(Options {
6779
output_style: OutputStyle::Compressed,
68-
include_paths: vec![
69-
STYLE_DIR.to_owned(),
70-
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
71-
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
72-
],
80+
include_paths,
7381
..Default::default()
7482
});
7583

7684
let css = context.compile()?;
77-
let dest_path = Path::new(&env::var("OUT_DIR")?).join("style.css");
85+
let dest_path = Path::new(&env::var("OUT_DIR")?).join(format!("{}.css", target));
7886
let mut file = File::create(&dest_path)?;
7987
file.write_all(css.as_bytes())?;
8088

8189
Ok(())
8290
}
8391

92+
fn compile_sass() -> Result<(), Box<dyn Error>> {
93+
// Compile base.scss -> style.css
94+
compile_sass_file("base", "style", &[])?;
95+
96+
// Compile vendored.scss -> vendored.css
97+
compile_sass_file(
98+
"vendored",
99+
"vendored",
100+
&[
101+
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
102+
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
103+
],
104+
)?;
105+
106+
Ok(())
107+
}
108+
84109
fn copy_js() {
85110
["menu.js", "index.js"].iter().for_each(|path| {
86111
let source_path =

src/web/metrics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ mod tests {
132132
("/robots.txt", "static resource"),
133133
("/sitemap.xml", "static resource"),
134134
("/-/static/style.css", "static resource"),
135+
("/-/static/vendored.css", "static resource"),
135136
];
136137

137138
wrapper(|env| {

src/web/statics.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use mime_guess::MimeGuess;
1010
use router::Router;
1111
use std::{ffi::OsStr, fs, path::Path};
1212

13+
const VENDORED_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/vendored.css"));
1314
const STYLE_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
1415
const MENU_JS: &str = include_str!(concat!(env!("OUT_DIR"), "/menu.js"));
1516
const INDEX_JS: &str = include_str!(concat!(env!("OUT_DIR"), "/index.js"));
@@ -20,6 +21,7 @@ pub(crate) fn static_handler(req: &mut Request) -> IronResult<Response> {
2021
let file = cexpect!(req, router.find("file"));
2122

2223
match file {
24+
"vendored.css" => serve_resource(VENDORED_CSS, ContentType("text/css".parse().unwrap())),
2325
"style.css" => serve_resource(STYLE_CSS, ContentType("text/css".parse().unwrap())),
2426
"index.js" => serve_resource(
2527
INDEX_JS,
@@ -119,7 +121,7 @@ pub(super) fn ico_handler(req: &mut Request) -> IronResult<Response> {
119121

120122
#[cfg(test)]
121123
mod tests {
122-
use super::{INDEX_JS, MENU_JS, STATIC_SEARCH_PATHS, STYLE_CSS};
124+
use super::{INDEX_JS, MENU_JS, STATIC_SEARCH_PATHS, STYLE_CSS, VENDORED_CSS};
123125
use crate::test::wrapper;
124126
use std::fs;
125127

@@ -141,6 +143,24 @@ mod tests {
141143
});
142144
}
143145

146+
#[test]
147+
fn vendored_css() {
148+
wrapper(|env| {
149+
let web = env.frontend();
150+
151+
let resp = web.get("/-/static/vendored.css").send()?;
152+
assert!(resp.status().is_success());
153+
assert_eq!(
154+
resp.headers().get("Content-Type"),
155+
Some(&"text/css".parse().unwrap()),
156+
);
157+
assert_eq!(resp.content_length().unwrap(), VENDORED_CSS.len() as u64);
158+
assert_eq!(resp.text()?, VENDORED_CSS);
159+
160+
Ok(())
161+
});
162+
}
163+
144164
#[test]
145165
fn index_js() {
146166
wrapper(|env| {

templates/base.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{%- block meta -%}{%- endblock meta -%}
1111

1212
{# Docs.rs styles #}
13+
<link rel="stylesheet" href="/-/static/vendored.css?{{ docsrs_version() | slugify }}" type="text/css" media="all" />
1314
<link rel="stylesheet" href="/normalize-{{ rustc_resource_suffix() }}.css" type="text/css" media="all" />
1415
<link rel="stylesheet" href="/rustdoc-{{ rustc_resource_suffix() }}.css" type="text/css" media="all" />
1516
<link rel="stylesheet" href="/light-{{ rustc_resource_suffix() }}.css" type="text/css" media="all" />

templates/style/base.scss

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// FIXME: Use modules
2-
@import "vars", "rustdoc", "utils", "navbar", "fontawesome", "regular", "solid", "brands", "pure-min",
3-
"grids-responsive-min";
2+
@import "vars", "rustdoc", "utils", "navbar";
43

54
html,
65
input,

templates/style/vendored.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@import "fontawesome", "regular", "solid", "brands", "pure-min",
2+
"grids-responsive-min";
3+

0 commit comments

Comments
 (0)