Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rustc-hash = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_transformer = { workspace = true }

insta = { workspace = true }
javascript-globals = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_minifier/examples/dce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_minifier::{CompressOptions, Compressor};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{TransformOptions, Transformer};
use pico_args::Arguments;

// Instruction:
Expand Down Expand Up @@ -57,6 +59,15 @@ fn main() -> std::io::Result<()> {
fn dce(allocator: &Allocator, source_text: &str, source_type: SourceType, nospace: bool) -> String {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;

// Transform TypeScript to ESNext before minification (minifier only works on esnext)
if source_type.is_typescript() {
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
let transform_options = TransformOptions::from_target("esnext").unwrap();
let _ = Transformer::new(allocator, std::path::Path::new("input"), &transform_options)
.build_with_scoping(scoping, &mut program);
}

Compressor::new(allocator).dead_code_elimination(&mut program, CompressOptions::dce());
Codegen::new()
.with_options(CodegenOptions { minify: nospace, ..CodegenOptions::default() })
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ use oxc_codegen::{Codegen, CodegenOptions, CodegenReturn, CommentOptions};
use oxc_mangler::MangleOptions;
use oxc_minifier::{CompressOptions, Minifier, MinifierOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_sourcemap::SourcemapVisualizer;
use oxc_span::SourceType;
use oxc_transformer::{TransformOptions, Transformer};

// Instruction:
// create a `test.js`,
Expand Down Expand Up @@ -92,6 +94,15 @@ fn minify(
) -> CodegenReturn {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;

// Transform TypeScript to ESNext before minification (minifier only works on esnext)
if source_type.is_typescript() {
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
let transform_options = TransformOptions::from_target("esnext").unwrap();
let _ = Transformer::new(allocator, std::path::Path::new("input"), &transform_options)
.build_with_scoping(scoping, &mut program);
}

let options = MinifierOptions {
mangle: mangle.then(MangleOptions::default),
compress: Some(CompressOptions { max_iterations, ..CompressOptions::smallest() }),
Expand Down
2 changes: 2 additions & 0 deletions tasks/track_memory_allocations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ doctest = false
oxc_allocator = { workspace = true, features = ["track_allocations"] }
oxc_minifier = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
oxc_tasks_common = { workspace = true }
oxc_transformer = { workspace = true }

humansize = { workspace = true }
mimalloc-safe = { workspace = true }
Expand Down
8 changes: 7 additions & 1 deletion tasks/track_memory_allocations/allocs_minifier.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
File | File size || Sys allocs | Sys reallocs || Arena allocs | Arena reallocs | Arena bytes
-------------------------------------------------------------------------------------------------------------------------------------------
RadixUIAdoptionSection.jsx | 2.52 kB || 76 | 4 || 20 | 6 | 688 B
checker.ts | 2.92 MB || 84072 | 14189 || 153630 | 29463 | 5.624 MB

cal.com.tsx | 1.06 MB || 40524 | 3029 || 37065 | 4733 | 1.654 MB

RadixUIAdoptionSection.jsx | 2.52 kB || 82 | 8 || 30 | 6 | 992 B

pdf.mjs | 567.30 kB || 19577 | 2898 || 47403 | 7782 | 1.624 MB

antd.js | 4.12 MB || 115026 | 15149 || 275588 | 60441 | 14.716 MB

binder.ts | 193.08 kB || 4767 | 974 || 7059 | 834 | 201.192 kB

22 changes: 18 additions & 4 deletions tasks/track_memory_allocations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use mimalloc_safe::MiMalloc;
use oxc_allocator::Allocator;
use oxc_minifier::{CompressOptions, MangleOptions, Minifier, MinifierOptions};
use oxc_parser::{ParseOptions, Parser};
use oxc_semantic::SemanticBuilder;
use oxc_tasks_common::{TestFiles, project_root};
use oxc_transformer::{TransformOptions, Transformer};

use std::alloc::{GlobalAlloc, Layout};
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
Expand Down Expand Up @@ -145,10 +147,15 @@ pub fn run() -> Result<(), io::Error> {
let mut parsed = Parser::new(&allocator, &file.source_text, file.source_type)
.with_options(parse_options)
.parse();
// Minifier cannot process TypeScript
if file.source_type.is_javascript() {
Minifier::new(minifier_options.clone()).minify(&allocator, &mut parsed.program);
}

// Transform TypeScript to ESNext before minifying (minifier only works on esnext)
let scoping = SemanticBuilder::new().build(&parsed.program).semantic.into_scoping();
let transform_options = TransformOptions::from_target("esnext").unwrap();
let _ =
Transformer::new(&allocator, std::path::Path::new(&file.file_name), &transform_options)
.build_with_scoping(scoping, &mut parsed.program);

Minifier::new(minifier_options.clone()).minify(&allocator, &mut parsed.program);
}

for file in files.files() {
Expand All @@ -173,6 +180,13 @@ pub fn run() -> Result<(), io::Error> {

let before_minify_stats = record_stats(&allocator);

// Transform TypeScript to ESNext before minifying (minifier only works on esnext)
let scoping = SemanticBuilder::new().build(&parsed.program).semantic.into_scoping();
let transform_options = TransformOptions::from_target("esnext").unwrap();
let _ =
Transformer::new(&allocator, std::path::Path::new(&file.file_name), &transform_options)
.build_with_scoping(scoping, &mut parsed.program);

// Minifier cannot process TypeScript
if !file.source_type.is_javascript() {
continue;
Expand Down