aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/unminify.rs
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2024-11-12 08:06:55 -0500
committerGitHub <noreply@github.com>2024-11-12 13:06:55 +0000
commitae029242f82459233a683c7623121dde6cae3f5b (patch)
treecebea157beec88969d821fe8fd68b62226cd0033 /components/script/unminify.rs
parent672b37dd9c4b27fb9892ce7148d163444bf10de0 (diff)
downloadservo-ae029242f82459233a683c7623121dde6cae3f5b.tar.gz
servo-ae029242f82459233a683c7623121dde6cae3f5b.zip
Unminify module scripts. (#34206)
* script: Unminify module scripts. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Fix clippy. Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components/script/unminify.rs')
-rw-r--r--components/script/unminify.rs59
1 files changed, 47 insertions, 12 deletions
diff --git a/components/script/unminify.rs b/components/script/unminify.rs
index 5292e68fedb..81bb5beeac9 100644
--- a/components/script/unminify.rs
+++ b/components/script/unminify.rs
@@ -2,15 +2,27 @@
* 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::env;
use std::fs::{create_dir_all, File};
-use std::io::{Error, ErrorKind};
+use std::io::{Error, Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
+use std::rc::Rc;
use servo_url::ServoUrl;
use tempfile::NamedTempFile;
use uuid::Uuid;
+use crate::dom::bindings::str::DOMString;
+
+pub(crate) trait ScriptSource {
+ fn unminified_dir(&self) -> Option<String>;
+ fn extract_bytes(&self) -> &[u8];
+ fn rewrite_source(&mut self, source: Rc<DOMString>);
+ fn url(&self) -> ServoUrl;
+ fn is_external(&self) -> bool;
+}
+
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
@@ -53,20 +65,11 @@ pub fn execute_js_beautify(input: &Path, output: File, file_type: BeautifyFileTy
}
pub fn create_output_file(
- unminified_dir: Option<String>,
+ unminified_dir: 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 path = PathBuf::from(unminified_dir);
let (base, has_name) = match url.as_str().ends_with('/') {
true => (
@@ -98,3 +101,35 @@ pub fn create_output_file(
File::create(path)
}
+
+pub(crate) fn unminify_js(script: &mut dyn ScriptSource) {
+ let Some(unminified_dir) = script.unminified_dir() else {
+ return;
+ };
+
+ if let Some((mut input, mut output)) = create_temp_files() {
+ input.write_all(script.extract_bytes()).unwrap();
+
+ if execute_js_beautify(
+ input.path(),
+ output.try_clone().unwrap(),
+ BeautifyFileType::Js,
+ ) {
+ let mut script_content = String::new();
+ output.seek(std::io::SeekFrom::Start(0)).unwrap();
+ output.read_to_string(&mut script_content).unwrap();
+ script.rewrite_source(Rc::new(DOMString::from(script_content)));
+ }
+ }
+
+ match create_output_file(unminified_dir, &script.url(), Some(script.is_external())) {
+ Ok(mut file) => file.write_all(script.extract_bytes()).unwrap(),
+ Err(why) => warn!("Could not store script {:?}", why),
+ }
+}
+
+pub(crate) fn unminified_path(dir: &str) -> String {
+ let mut path = env::current_dir().unwrap();
+ path.push(dir);
+ path.into_os_string().into_string().unwrap()
+}