Skip to content

Commit

Permalink
refactor: improvements in javascript minify
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Jun 16, 2024
1 parent d902cdf commit d072c82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
18 changes: 12 additions & 6 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(())
}

Expand All @@ -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(())
}

Expand All @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::{
async fn main() -> Result<(), Box<dyn Error>> {
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());
Expand Down
24 changes: 8 additions & 16 deletions src/minix/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &regex::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: &regex::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()
}

Expand Down
4 changes: 2 additions & 2 deletions src/minix/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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+";
Expand Down

0 comments on commit d072c82

Please sign in to comment.