From d072c82052615ea228ad9457ceceeec3ddcd060b Mon Sep 17 00:00:00 2001 From: Kremilly Date: Sun, 16 Jun 2024 12:41:04 -0300 Subject: [PATCH] refactor: improvements in javascript minify --- Cargo.toml | 4 ++-- src/engine/engine.rs | 18 ++++++++++++------ src/main.rs | 4 ++-- src/minix/minify.rs | 24 ++++++++---------------- src/minix/regex.rs | 4 ++-- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bc3ba8a..549f942 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "minix" license = "MIT" -version = "0.1.4" +version = "0.1.5" edition = "2021" -author = ["kremilly"] +authors = ["kremilly"] keywords = ["rust", "javascript", "css", "minify", "compression"] description = "A straightforward minifier for JavaScript and CSS files, developed using Rust. This tool efficiently reduces the size of JS and CSS files by removing unnecessary characters, whitespace, and comments without affecting functionality." repository = "https://github.com/kremilly/Minix" diff --git a/src/engine/engine.rs b/src/engine/engine.rs index 7cd0ddb..2a99bcb 100644 --- a/src/engine/engine.rs +++ b/src/engine/engine.rs @@ -1,12 +1,18 @@ +extern crate colored; + +use colored::*; use crate::minix::minify::Minify; use std::{ - error::Error, - fs::{self, File}, io::Write, path::Path, -}; + error::Error, + fs::{ + self, + File + }, +}; pub struct Engine; @@ -71,7 +77,7 @@ impl Engine { let mut file = File::create(output)?; file.write_all(content_minified.as_bytes())?; - println!("File minified from {} to {} successfully!", input, output); + println!("-> File minified from {} to {} was successfully!", input.blue(), output.green()); Ok(()) } @@ -85,7 +91,7 @@ impl Engine { let mut file = File::create(output)?; file.write_all(content_minified.as_bytes())?; - println!("File minified from {} to {} successfully!", input, output); + println!("-> File minified from {} to {} was successfully!", input.blue(), output.green()); Ok(()) } @@ -94,7 +100,7 @@ impl Engine { let filter: Vec<&str> = input.split("*.").collect(); Self::scan_path(filter[0], filter[1], output)?; } else { - Self::write(input, output.unwrap_or(""))?; // Dereference the output parameter + Self::write(input, output.unwrap_or(""))?; } Ok(()) diff --git a/src/main.rs b/src/main.rs index 6b7ce54..891a116 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,8 @@ use crate::{ async fn main() -> Result<(), Box> { let flags = Flags::parse(); - let author = "@Kremilly"; - let homepage = "https://kremilly.com"; + let author = env!("CARGO_PKG_AUTHORS"); + let homepage = env!("CARGO_PKG_HOMEPAGE"); if let Some(title) = FIGfont::standard().unwrap().convert("Minix") { println!("{}", title.to_string().bold().blue()); diff --git a/src/minix/minify.rs b/src/minix/minify.rs index 16a1095..26d1340 100644 --- a/src/minix/minify.rs +++ b/src/minix/minify.rs @@ -12,32 +12,24 @@ impl Minify { pub fn js(code: &str) -> String { let code = Utils::preserve_strings(code); let code = Utils::remove_single_line_comments(&code); + let code = Regex::new(RegExp::MIN_JS_REMOVE_MULTI_LINE_COMMENT).unwrap().replace_all(&code.trim(), ""); let code = Regex::new(RegExp::MIN_JS_STRING_LITERAL).unwrap().replace_all(&code, "$0"); let code = Regex::new(RegExp::MIN_JS_REMOVE_OPERATORS_KEYWORDS).unwrap().replace_all(&code, "$1"); - let code = Regex::new(RegExp::MIN_JS_REMOVE_SPACES).unwrap().replace_all(&code, "$1"); // (BROKEN) - - let code = Regex::new( - &format!(r"\b({})\b", RegExp::MIN_JS_KEYWORDS) - ).unwrap().replace_all( - &code, "$0" - ); - + let code = Regex::new(RegExp::MIN_JS_REMOVE_SPACES).unwrap().replace_all(&code, "$1"); + let code = Regex::new(&format!(r"\b({})\b", RegExp::MIN_JS_KEYWORDS)).unwrap().replace_all(&code, "$0"); let code = Regex::new(RegExp::MIN_JS_DUPLICATE_SPACES).unwrap().replace_all(&code, " "); let code = Regex::new(RegExp::MIN_JS_LOGICAL_OPERATORS).unwrap().replace_all(&code, "||"); let code = Regex::new(RegExp::MIN_JS_WHITESPACE_TRIM).unwrap().replace_all(&code, "$1"); let code = Regex::new(RegExp::MIN_JS_REMOVE_SPACES_AFTER_PAREN_REGEX).unwrap().replace_all(&code, "("); + + let code = Regex::new(RegExp::MIN_JS_DOUBLE_QUOTED_STRING).unwrap().replace_all(&code, |caps: ®ex::Captures| { + let inner = &caps[0][1..caps[0].len() - 1]; + format!("\"{}\"", inner.replace("\\\"", "\"")) + }); - let code = Regex::new(RegExp::MIN_JS_DOUBLE_QUOTED_STRING).unwrap().replace_all( - &code, |caps: ®ex::Captures| { - let inner = &caps[0][1..caps[0].len() - 1]; - format!("\"{}\"", inner.replace("\\\"", "\"")) - } - ); - let code = Utils::remove_empty_lines(&code); let code = Utils::remove_line_break_after_semicolon(&code); - code.to_string() } diff --git a/src/minix/regex.rs b/src/minix/regex.rs index c176e36..a14d46e 100644 --- a/src/minix/regex.rs +++ b/src/minix/regex.rs @@ -4,17 +4,17 @@ impl RegExp { pub const MIN_JS_REMOVE_MULTI_LINE_COMMENT: &'static str = r"/\*.*?\*/"; pub const MIN_JS_STRING_LITERAL: &'static str = r#""(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`"#; - pub const MIN_JS_REMOVE_OPERATORS_KEYWORDS: &'static str = r"(==|===|!==|!=|\+|-|\*|/|&&|\|\||\(|\)|\{|}|=|;)"; pub const MIN_JS_EMPTY_LINES_REGEX: &'static str = r"(?m)^\s*$\n?"; pub const MIN_JS_SINGLE_LINE_COMMENT_REGEX: &'static str = r"//.*$"; pub const MIN_JS_LINE_END_SEMICOLON_REGEX: &'static str = r";\n"; pub const MIN_JS_REMOVE_SPACES: &'static str = r"\s*([=\{\)\]\}])\s*"; - pub const MIN_JS_KEYWORDS: &'static str = r"\b(if|else|for|while|do|switch|case|break|continue|return|function|var|let|pub const)\b"; pub const MIN_JS_DUPLICATE_SPACES: &'static str = r"\s{2,}"; pub const MIN_JS_LOGICAL_OPERATORS: &'static str = r"\s*\|\|\s*"; pub const MIN_JS_DOUBLE_QUOTED_STRING: &'static str = r#""(?:\\.|[^"\\])*""#; pub const MIN_JS_WHITESPACE_TRIM: &'static str = r#"\s*([,+{}:])\s*"#; pub const MIN_JS_REMOVE_SPACES_AFTER_PAREN_REGEX: &'static str = r#"\(\s+"#; + pub const MIN_JS_KEYWORDS: &'static str = r"var|let|const|if|else|for|while|function|return|class|new"; + pub const MIN_JS_REMOVE_OPERATORS_KEYWORDS: &'static str = r"\s*(=|\{|\}|:|,|\[|\]|\(|\)|<|>|\+|\-|\*|/|&|\||\^|!|~|\?|;|\.\.\.)\s*"; pub const MIN_CSS_REMOVE_MULTI_LINE_COMMENT: &'static str = r"/\*.*?\*/"; pub const MIN_CSS_REMOVE_WHITESPACE: &'static str = r"\s+";