diff --git a/server/Cargo.toml b/server/Cargo.toml index 21505695b..92c9921f3 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -19,3 +19,4 @@ swc = "0.289.1" swc_common = "0.40.1" tokio = { version = "1.40.0", features = ["full"] } tower-http = { version = "0.6.1", features = ["compression-full"] } +# oxc = { version = "0.34.0", features = ["codegen", "minifier"] } diff --git a/server/src/website/handlers/_minify.rs b/server/src/website/handlers/_minify.rs new file mode 100644 index 000000000..aa5497b55 --- /dev/null +++ b/server/src/website/handlers/_minify.rs @@ -0,0 +1,41 @@ +// Files are bigger than with SWC, to retest later + +// Source: https://github.com/oxc-project/oxc/blob/main/crates/oxc_minifier/examples/minifier.rs + +use std::{fs, path::Path}; + +use oxc::{ + allocator::Allocator, + codegen::{CodeGenerator, CodegenOptions}, + minifier::{MinifierOptions, MinifierReturn}, + parser::{Parser, ParserReturn}, + span::SourceType, +}; + +// +pub fn minify_js(path: &Path) -> String { + let allocator = Allocator::default(); + + let source_type = SourceType::from_path(path).unwrap(); + + let source_text = fs::read_to_string(path).unwrap(); + + let ParserReturn { mut program, .. } = + Parser::new(&allocator, &source_text, source_type).parse(); + + let minifier = oxc::minifier::Minifier::new(MinifierOptions::default()); + + let MinifierReturn { mangler } = minifier.build(&allocator, &mut program); + + CodeGenerator::new() + .with_options(CodegenOptions { + single_quote: false, + minify: true, + comments: false, + annotation_comments: false, + source_map_path: None, + }) + .with_mangler(mangler) + .build(&program) + .code +}