Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mangler): initialize crate and integrate into minifier #4197

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
15 changes: 13 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ oxc_codegen = { version = "0.20.0", path = "crates/oxc_codegen" }
oxc_diagnostics = { version = "0.20.0", path = "crates/oxc_diagnostics" }
oxc_index = { version = "0.20.0", path = "crates/oxc_index" }
oxc_minifier = { version = "0.20.0", path = "crates/oxc_minifier" }
oxc_mangler = { version = "0.20.0", path = "crates/oxc_mangler" }
oxc_parser = { version = "0.20.0", path = "crates/oxc_parser" }
oxc_semantic = { version = "0.20.0", path = "crates/oxc_semantic" }
oxc_span = { version = "0.20.0", path = "crates/oxc_span" }
Expand Down
11 changes: 7 additions & 4 deletions crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ oxc_span = { workspace = true }
oxc_allocator = { workspace = true }
oxc_syntax = { workspace = true }
oxc_sourcemap = { workspace = true }
bitflags = { workspace = true }
once_cell = { workspace = true }
daachorse = { workspace = true }
rustc-hash = { workspace = true }
oxc_mangler = { workspace = true }

bitflags = { workspace = true }
once_cell = { workspace = true }
daachorse = { workspace = true }
rustc-hash = { workspace = true }

[dev-dependencies]
oxc_parser = { workspace = true }
base64 = { workspace = true }
22 changes: 12 additions & 10 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_allocator::{Box, Vec};
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_span::GetSpan;
use oxc_span::{CompactStr, GetSpan};
use oxc_syntax::{
identifier::{LS, PS},
keyword::is_reserved_keyword_or_global_object,
Expand Down Expand Up @@ -1081,16 +1081,18 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ParenthesizedExpression<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for IdentifierReference<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
// if let Some(mangler) = &p.mangler {
// if let Some(reference_id) = self.reference_id.get() {
// if let Some(name) = mangler.get_reference_name(reference_id) {
// p.print_str(name.clone().as_str());
// return;
// }
// }
// }
if let Some(mangler) = &p.mangler {
if let Some(reference_id) = self.reference_id.get() {
if let Some(name) = mangler.get_reference_name(reference_id) {
let name = CompactStr::new(name);
p.add_source_mapping_for_name(self.span, &name);
p.print_str(&name);
return;
}
}
}
p.add_source_mapping_for_name(self.span, &self.name);
p.print_str(self.name.as_str());
p.print_str(&self.name);
}
}

Expand Down
45 changes: 29 additions & 16 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ mod sourcemap_builder;

use std::{borrow::Cow, ops::Range};

use rustc_hash::FxHashMap;

use oxc_ast::{
ast::{BlockStatement, Directive, Expression, Program, Statement},
Comment, Trivias,
};
use oxc_span::Span;
use oxc_mangler::Mangler;
use oxc_span::{CompactStr, Span};
use oxc_syntax::{
identifier::is_identifier_part,
operator::{BinaryOperator, UnaryOperator, UpdateOperator},
precedence::Precedence,
symbol::SymbolId,
};
use rustc_hash::FxHashMap;

pub use crate::{
context::Context,
Expand Down Expand Up @@ -55,6 +57,8 @@ pub struct Codegen<'a, const MINIFY: bool> {

trivias: Trivias,

mangler: Option<Mangler>,

/// Output Code
code: Vec<u8>,

Expand Down Expand Up @@ -111,6 +115,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
comment_options: CommentOptions::default(),
source_text: "",
trivias: Trivias::default(),
mangler: None,
code: vec![],
needs_semicolon: false,
need_space_before_dot: 0,
Expand All @@ -127,6 +132,15 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
}
}

/// Initialize the output code buffer to reduce memory reallocation.
/// Minification will reduce by at least half of the original size.
#[must_use]
pub fn with_capacity(mut self, source_text_len: usize) -> Self {
let capacity = if MINIFY { source_text_len / 2 } else { source_text_len };
self.code = Vec::with_capacity(capacity);
self
}

#[must_use]
pub fn enable_comment(
mut self,
Expand All @@ -148,12 +162,9 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
self
}

/// Initialize the output code buffer to reduce memory reallocation.
/// Minification will reduce by at least half of the original size.
#[must_use]
pub fn with_capacity(mut self, source_text_len: usize) -> Self {
let capacity = if MINIFY { source_text_len / 2 } else { source_text_len };
self.code = Vec::with_capacity(capacity);
pub fn with_mangler(mut self, mangler: Option<Mangler>) -> Self {
self.mangler = mangler;
self
}

Expand All @@ -180,7 +191,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
self.code.push(ch);
}

/// Push a single character into the buffer
/// Push str into the buffer
#[inline]
pub fn print_str(&mut self, s: &str) {
self.code.extend(s.as_bytes());
Expand Down Expand Up @@ -398,14 +409,16 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
}

#[allow(clippy::needless_pass_by_value)]
fn print_symbol(&mut self, span: Span, _symbol_id: Option<SymbolId>, fallback: &str) {
// if let Some(mangler) = &self.mangler {
// if let Some(symbol_id) = symbol_id {
// let name = mangler.get_symbol_name(symbol_id);
// self.print_str(name.clone());
// return;
// }
// }
fn print_symbol(&mut self, span: Span, symbol_id: Option<SymbolId>, fallback: &str) {
if let Some(mangler) = &self.mangler {
if let Some(symbol_id) = symbol_id {
let name = mangler.get_symbol_name(symbol_id);
let name = CompactStr::new(name);
self.add_source_mapping_for_name(span, &name);
self.print_str(&name);
return;
}
}
self.add_source_mapping_for_name(span, fallback);
self.print_str(fallback);
}
Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_mangler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "oxc_mangler"
version = "0.20.0"
publish = true
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
categories.workspace = true
include = ["/examples", "/src"]

[lints]
workspace = true

[lib]
test = false
doctest = false

[dependencies]
oxc_span = { workspace = true }
oxc_ast = { workspace = true }
oxc_semantic = { workspace = true }
oxc_index = { workspace = true }
itertools = { workspace = true }
File renamed without changes.
7 changes: 3 additions & 4 deletions crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ oxc_span = { workspace = true }
oxc_ast = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_index = { workspace = true }
oxc_parser = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_codegen = { workspace = true }
oxc_mangler = { workspace = true }

num-bigint = { workspace = true }
itertools = { workspace = true }
num-traits = { workspace = true }

[dev-dependencies]
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }
oxc_parser = { workspace = true }

insta = { workspace = true }
walkdir = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace:
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
Minifier::new(options).build(&allocator, program);
let ret = Minifier::new(options).build(&allocator, program);
if whitespace {
WhitespaceRemover::new().build(program)
CodeGenerator::new().with_mangler(ret.mangler).build(program)
} else {
CodeGenerator::new().build(program)
WhitespaceRemover::new().with_mangler(ret.mangler).build(program)
}
.source_text
}
15 changes: 8 additions & 7 deletions crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
mod ast_passes;
mod compressor;
mod folder;
mod mangler;

use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_mangler::{Mangler, ManglerBuilder};

pub use crate::{
ast_passes::{RemoveDeadCode, RemoveParens, ReplaceGlobalDefines, ReplaceGlobalDefinesConfig},
compressor::{CompressOptions, Compressor},
mangler::ManglerBuilder,
};

#[derive(Debug, Clone, Copy)]
Expand All @@ -27,6 +26,10 @@ impl Default for MinifierOptions {
}
}

pub struct MinifierReturn {
pub mangler: Option<Mangler>,
}

pub struct Minifier {
options: MinifierOptions,
}
Expand All @@ -36,11 +39,9 @@ impl Minifier {
Self { options }
}

pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) {
pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) -> MinifierReturn {
Compressor::new(allocator, self.options.compress).build(program);
// if self.options.mangle {
// let mangler = ManglerBuilder.build(program);
// printer.with_mangler(mangler);
// }
let mangler = self.options.mangle.then(|| ManglerBuilder.build(program));
MinifierReturn { mangler }
}
}
24 changes: 12 additions & 12 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Original | Minified | Gzip

72.14 kB | 39.57 kB | 10.82 kB react.development.js
72.14 kB | 24.32 kB | 8.79 kB react.development.js

173.90 kB | 95.45 kB | 24.43 kB moment.js
173.90 kB | 61.92 kB | 19.66 kB moment.js

287.63 kB | 140.27 kB | 39.91 kB jquery.js
287.63 kB | 93.13 kB | 32.47 kB jquery.js

342.15 kB | 190.52 kB | 55.60 kB vue.js
342.15 kB | 123.60 kB | 45.79 kB vue.js

544.10 kB | 144.01 kB | 35.54 kB lodash.js
544.10 kB | 75.13 kB | 26.47 kB lodash.js

555.77 kB | 390.70 kB | 103.02 kB d3.js
555.77 kB | 274.87 kB | 91.58 kB d3.js

977.19 kB | 603.59 kB | 138.72 kB bundle.min.js
977.19 kB | 458.03 kB | 124.54 kB bundle.min.js

1.25 MB | 925.16 kB | 189.58 kB three.js
1.25 MB | 674.62 kB | 166.81 kB three.js

2.14 MB | 1.41 MB | 217.82 kB victory.js
2.14 MB | 745.86 kB | 182.68 kB victory.js

3.20 MB | 1.73 MB | 427.25 kB echarts.js
3.20 MB | 1.03 MB | 334.72 kB echarts.js

6.69 MB | 4.37 MB | 619.40 kB antd.js
6.69 MB | 2.43 MB | 504.04 kB antd.js

10.82 MB | 5.71 MB | 1.10 MB typescript.js
10.82 MB | 3.52 MB | 910.41 kB typescript.js

15 changes: 8 additions & 7 deletions tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ pub fn run() -> Result<(), io::Error> {
fn minify_twice(file: &TestFile) -> String {
let source_type = SourceType::from_path(&file.file_name).unwrap();
let options = MinifierOptions {
mangle: true,
compress: CompressOptions { evaluate: false, ..CompressOptions::default() },
..MinifierOptions::default()
};
let source_text1 = minify(&file.source_text, source_type, options);
let source_text2 = minify(&source_text1, source_type, options);
assert!(source_text1 == source_text2, "Minification failed for {}", &file.file_name);
source_text2
// let source_text1 = minify(&file.source_text, source_type, options);
// let source_text2 = minify(&source_text1, source_type, options);
// assert!(source_text1 == source_text2, "Minification failed for {}", &file.file_name);
// source_text2
minify(&file.source_text, source_type, options)
}

fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
Minifier::new(options).build(&allocator, program);
WhitespaceRemover::new().build(program).source_text
let ret = Minifier::new(options).build(&allocator, program);
WhitespaceRemover::new().with_mangler(ret.mangler).build(program).source_text
}

fn gzip_size(s: &str) -> usize {
Expand Down
Loading