Skip to content

Commit d97eacf

Browse files
a2aaronStranger6667
authored andcommitted
feat: Make loading from local files be gated behind a feature as well.
The file loading machinery in `std` is also pretty large--it adds about 500 kB (or about 100 kB compressed) of file size. Like with loading external URLs, this functionality can be unneeded in some applications (such as with wasm binaries, which typically don't have file system access). Allowing external file loading to be disabled avoids incurring this overhead.
1 parent 137e3ff commit d97eacf

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

bindings/python/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ built = { version = "0.5", features = ["chrono"] }
1717
path = "../../css-inline"
1818
version = "*"
1919
default-features = false
20-
features = ["http"]
20+
features = ["http", "file"]
2121

2222
[dependencies]
2323
url = "2"

bindings/wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ crate-type = ["cdylib"]
2020
path = "../../css-inline"
2121
version = "*"
2222
default-features = false
23-
features = ["http"]
23+
features = ["http", "file"]
2424

2525
[dependencies]
2626
url = "2"

css-inline/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ categories = ["web-programming"]
1515
name = "css-inline"
1616

1717
[features]
18-
default = ["cli", "http"]
18+
default = ["cli", "http", "file"]
1919
cli = ["pico-args", "rayon"]
2020
http = ["attohttpc"]
21+
file = []
2122

2223
[dependencies]
2324
cssparser = "0.29"

css-inline/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use indexmap::IndexMap;
4040
use smallvec::{smallvec, SmallVec};
4141
use std::{
4242
borrow::Cow,
43-
fs,
4443
io::{ErrorKind, Write},
4544
};
4645

@@ -358,12 +357,22 @@ fn load_external(location: &str) -> Result<String> {
358357
)))
359358
}
360359
} else {
361-
fs::read_to_string(location).map_err(|error| match error.kind() {
362-
ErrorKind::NotFound => InlineError::MissingStyleSheet {
363-
path: location.to_string(),
364-
},
365-
_ => InlineError::IO(error),
366-
})
360+
#[cfg(feature = "file")]
361+
{
362+
std::fs::read_to_string(location).map_err(|error| match error.kind() {
363+
ErrorKind::NotFound => InlineError::MissingStyleSheet {
364+
path: location.to_string(),
365+
},
366+
_ => InlineError::IO(error),
367+
})
368+
}
369+
#[cfg(not(feature = "file"))]
370+
{
371+
Err(InlineError::IO(std::io::Error::new(
372+
ErrorKind::Unsupported,
373+
"Loading local files requires the `file` feature",
374+
)))
375+
}
367376
}
368377
}
369378

0 commit comments

Comments
 (0)