diff options
author | Taym Haddadi <haddadi.taym@gmail.com> | 2024-10-30 12:12:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-30 11:12:20 +0000 |
commit | ee68dc2589283d9ef37b578b83258d421ec29bcc (patch) | |
tree | 91d7eea5a80652d14ce1e26a635d07bf94b8395a /components/script/unminify.rs | |
parent | bac1101163a436096fe0b71bed79094658bba3b2 (diff) | |
download | servo-ee68dc2589283d9ef37b578b83258d421ec29bcc.tar.gz servo-ee68dc2589283d9ef37b578b83258d421ec29bcc.zip |
Support persisting unminified external stylesheets (#33919)
* Support local tweaking of external stylesheets
Signed-off-by: Taym <haddadi.taym@gmail.com>
* Remove duplicated code between unminify_css and unminify_js
Signed-off-by: Taym <haddadi.taym@gmail.com>
* Add License
Signed-off-by: Taym <haddadi.taym@gmail.com>
* Use js-beautify instead of npx
Signed-off-by: Taym <haddadi.taym@gmail.com>
* Fix clippy warning
Signed-off-by: Taym <haddadi.taym@gmail.com>
---------
Signed-off-by: Taym <haddadi.taym@gmail.com>
Diffstat (limited to 'components/script/unminify.rs')
-rw-r--r-- | components/script/unminify.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/components/script/unminify.rs b/components/script/unminify.rs new file mode 100644 index 00000000000..5292e68fedb --- /dev/null +++ b/components/script/unminify.rs @@ -0,0 +1,100 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use std::fs::{create_dir_all, File}; +use std::io::{Error, ErrorKind}; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use servo_url::ServoUrl; +use tempfile::NamedTempFile; +use uuid::Uuid; + +pub fn create_temp_files() -> Option<(NamedTempFile, File)> { + // Write the minified code to a temporary file and pass its path as an argument + // to js-beautify to read from. Meanwhile, redirect the process' stdout into + // another temporary file and read that into a string. This avoids some hangs + // observed on macOS when using direct input/output pipes with very large + // unminified content. + let (input, output) = (NamedTempFile::new(), tempfile::tempfile()); + if let (Ok(input), Ok(output)) = (input, output) { + Some((input, output)) + } else { + log::warn!("Error creating input and output temp files"); + None + } +} + +#[derive(Debug)] +pub enum BeautifyFileType { + Css, + Js, +} + +pub fn execute_js_beautify(input: &Path, output: File, file_type: BeautifyFileType) -> bool { + let mut cmd = Command::new("js-beautify"); + match file_type { + BeautifyFileType::Js => (), + BeautifyFileType::Css => { + cmd.arg("--type").arg("css"); + }, + } + match cmd.arg(input).stdout(output).status() { + Ok(status) => status.success(), + _ => { + log::warn!( + "Failed to execute js-beautify --type {:?}, Will store unmodified script", + file_type + ); + false + }, + } +} + +pub fn create_output_file( + unminified_dir: Option<String>, + url: &ServoUrl, + external: Option<bool>, +) -> Result<File, Error> { + let path = match unminified_dir { + Some(unminified_dir) => PathBuf::from(unminified_dir), + None => { + warn!("Unminified file directory not found"); + return Err(Error::new( + ErrorKind::NotFound, + "Unminified file directory not found", + )); + }, + }; + + let (base, has_name) = match url.as_str().ends_with('/') { + true => ( + path.join(&url[url::Position::BeforeHost..]) + .as_path() + .to_owned(), + false, + ), + false => ( + path.join(&url[url::Position::BeforeHost..]) + .parent() + .unwrap() + .to_owned(), + true, + ), + }; + + create_dir_all(&base)?; + + let path = if external.unwrap_or(true) && has_name { + // External. + path.join(&url[url::Position::BeforeHost..]) + } else { + // Inline file or url ends with '/' + base.join(Uuid::new_v4().to_string()) + }; + + debug!("Unminified files will be stored in {:?}", path); + + File::create(path) +} |