Skip to content

Commit e3e663b

Browse files
committed
feat(mangler): initialize crate and integrate into minifier (#4197)
1 parent bbe5ded commit e3e663b

12 files changed

Lines changed: 124 additions & 65 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ oxc_codegen = { version = "0.20.0", path = "crates/oxc_codegen" }
8181
oxc_diagnostics = { version = "0.20.0", path = "crates/oxc_diagnostics" }
8282
oxc_index = { version = "0.20.0", path = "crates/oxc_index" }
8383
oxc_minifier = { version = "0.20.0", path = "crates/oxc_minifier" }
84+
oxc_mangler = { version = "0.20.0", path = "crates/oxc_mangler" }
8485
oxc_parser = { version = "0.20.0", path = "crates/oxc_parser" }
8586
oxc_semantic = { version = "0.20.0", path = "crates/oxc_semantic" }
8687
oxc_span = { version = "0.20.0", path = "crates/oxc_span" }

crates/oxc_codegen/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ oxc_span = { workspace = true }
2525
oxc_allocator = { workspace = true }
2626
oxc_syntax = { workspace = true }
2727
oxc_sourcemap = { workspace = true }
28-
bitflags = { workspace = true }
29-
once_cell = { workspace = true }
30-
daachorse = { workspace = true }
31-
rustc-hash = { workspace = true }
28+
oxc_mangler = { workspace = true }
29+
30+
bitflags = { workspace = true }
31+
once_cell = { workspace = true }
32+
daachorse = { workspace = true }
33+
rustc-hash = { workspace = true }
34+
3235
[dev-dependencies]
3336
oxc_parser = { workspace = true }
3437
base64 = { workspace = true }

crates/oxc_codegen/src/gen.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_allocator::{Box, Vec};
22
#[allow(clippy::wildcard_imports)]
33
use oxc_ast::ast::*;
4-
use oxc_span::GetSpan;
4+
use oxc_span::{CompactStr, GetSpan};
55
use oxc_syntax::{
66
identifier::{LS, PS},
77
keyword::is_reserved_keyword_or_global_object,
@@ -1081,16 +1081,18 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ParenthesizedExpression<'a> {
10811081

10821082
impl<'a, const MINIFY: bool> Gen<MINIFY> for IdentifierReference<'a> {
10831083
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
1084-
// if let Some(mangler) = &p.mangler {
1085-
// if let Some(reference_id) = self.reference_id.get() {
1086-
// if let Some(name) = mangler.get_reference_name(reference_id) {
1087-
// p.print_str(name.clone().as_str());
1088-
// return;
1089-
// }
1090-
// }
1091-
// }
1084+
if let Some(mangler) = &p.mangler {
1085+
if let Some(reference_id) = self.reference_id.get() {
1086+
if let Some(name) = mangler.get_reference_name(reference_id) {
1087+
let name = CompactStr::new(name);
1088+
p.add_source_mapping_for_name(self.span, &name);
1089+
p.print_str(&name);
1090+
return;
1091+
}
1092+
}
1093+
}
10921094
p.add_source_mapping_for_name(self.span, &self.name);
1093-
p.print_str(self.name.as_str());
1095+
p.print_str(&self.name);
10941096
}
10951097
}
10961098

crates/oxc_codegen/src/lib.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ mod sourcemap_builder;
1212

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

15+
use rustc_hash::FxHashMap;
16+
1517
use oxc_ast::{
1618
ast::{BlockStatement, Directive, Expression, Program, Statement},
1719
Comment, Trivias,
1820
};
19-
use oxc_span::Span;
21+
use oxc_mangler::Mangler;
22+
use oxc_span::{CompactStr, Span};
2023
use oxc_syntax::{
2124
identifier::is_identifier_part,
2225
operator::{BinaryOperator, UnaryOperator, UpdateOperator},
2326
precedence::Precedence,
2427
symbol::SymbolId,
2528
};
26-
use rustc_hash::FxHashMap;
2729

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

5658
trivias: Trivias,
5759

60+
mangler: Option<Mangler>,
61+
5862
/// Output Code
5963
code: Vec<u8>,
6064

@@ -111,6 +115,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
111115
comment_options: CommentOptions::default(),
112116
source_text: "",
113117
trivias: Trivias::default(),
118+
mangler: None,
114119
code: vec![],
115120
needs_semicolon: false,
116121
need_space_before_dot: 0,
@@ -127,6 +132,15 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
127132
}
128133
}
129134

135+
/// Initialize the output code buffer to reduce memory reallocation.
136+
/// Minification will reduce by at least half of the original size.
137+
#[must_use]
138+
pub fn with_capacity(mut self, source_text_len: usize) -> Self {
139+
let capacity = if MINIFY { source_text_len / 2 } else { source_text_len };
140+
self.code = Vec::with_capacity(capacity);
141+
self
142+
}
143+
130144
#[must_use]
131145
pub fn enable_comment(
132146
mut self,
@@ -148,12 +162,9 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
148162
self
149163
}
150164

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

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

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

400411
#[allow(clippy::needless_pass_by_value)]
401-
fn print_symbol(&mut self, span: Span, _symbol_id: Option<SymbolId>, fallback: &str) {
402-
// if let Some(mangler) = &self.mangler {
403-
// if let Some(symbol_id) = symbol_id {
404-
// let name = mangler.get_symbol_name(symbol_id);
405-
// self.print_str(name.clone());
406-
// return;
407-
// }
408-
// }
412+
fn print_symbol(&mut self, span: Span, symbol_id: Option<SymbolId>, fallback: &str) {
413+
if let Some(mangler) = &self.mangler {
414+
if let Some(symbol_id) = symbol_id {
415+
let name = mangler.get_symbol_name(symbol_id);
416+
let name = CompactStr::new(name);
417+
self.add_source_mapping_for_name(span, &name);
418+
self.print_str(&name);
419+
return;
420+
}
421+
}
409422
self.add_source_mapping_for_name(span, fallback);
410423
self.print_str(fallback);
411424
}

crates/oxc_mangler/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "oxc_mangler"
3+
version = "0.20.0"
4+
publish = true
5+
authors.workspace = true
6+
description.workspace = true
7+
edition.workspace = true
8+
homepage.workspace = true
9+
keywords.workspace = true
10+
license.workspace = true
11+
repository.workspace = true
12+
rust-version.workspace = true
13+
categories.workspace = true
14+
include = ["/examples", "/src"]
15+
16+
[lints]
17+
workspace = true
18+
19+
[lib]
20+
test = false
21+
doctest = false
22+
23+
[dependencies]
24+
oxc_span = { workspace = true }
25+
oxc_ast = { workspace = true }
26+
oxc_semantic = { workspace = true }
27+
oxc_index = { workspace = true }
28+
itertools = { workspace = true }

crates/oxc_minifier/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ oxc_span = { workspace = true }
2626
oxc_ast = { workspace = true }
2727
oxc_semantic = { workspace = true }
2828
oxc_syntax = { workspace = true }
29-
oxc_index = { workspace = true }
3029
oxc_parser = { workspace = true }
3130
oxc_diagnostics = { workspace = true }
31+
oxc_codegen = { workspace = true }
32+
oxc_mangler = { workspace = true }
3233

3334
num-bigint = { workspace = true }
34-
itertools = { workspace = true }
3535
num-traits = { workspace = true }
3636

3737
[dev-dependencies]
38-
oxc_parser = { workspace = true }
39-
oxc_codegen = { workspace = true }
38+
oxc_parser = { workspace = true }
4039

4140
insta = { workspace = true }
4241
walkdir = { workspace = true }

crates/oxc_minifier/examples/minifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace:
4040
let ret = Parser::new(&allocator, source_text, source_type).parse();
4141
let program = allocator.alloc(ret.program);
4242
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
43-
Minifier::new(options).build(&allocator, program);
43+
let ret = Minifier::new(options).build(&allocator, program);
4444
if whitespace {
45-
WhitespaceRemover::new().build(program)
45+
CodeGenerator::new().with_mangler(ret.mangler).build(program)
4646
} else {
47-
CodeGenerator::new().build(program)
47+
WhitespaceRemover::new().with_mangler(ret.mangler).build(program)
4848
}
4949
.source_text
5050
}

crates/oxc_minifier/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
mod ast_passes;
55
mod compressor;
66
mod folder;
7-
mod mangler;
87

98
use oxc_allocator::Allocator;
109
use oxc_ast::ast::Program;
10+
use oxc_mangler::{Mangler, ManglerBuilder};
1111

1212
pub use crate::{
1313
ast_passes::{RemoveDeadCode, RemoveParens, ReplaceGlobalDefines, ReplaceGlobalDefinesConfig},
1414
compressor::{CompressOptions, Compressor},
15-
mangler::ManglerBuilder,
1615
};
1716

1817
#[derive(Debug, Clone, Copy)]
@@ -27,6 +26,10 @@ impl Default for MinifierOptions {
2726
}
2827
}
2928

29+
pub struct MinifierReturn {
30+
pub mangler: Option<Mangler>,
31+
}
32+
3033
pub struct Minifier {
3134
options: MinifierOptions,
3235
}
@@ -36,11 +39,9 @@ impl Minifier {
3639
Self { options }
3740
}
3841

39-
pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) {
42+
pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) -> MinifierReturn {
4043
Compressor::new(allocator, self.options.compress).build(program);
41-
// if self.options.mangle {
42-
// let mangler = ManglerBuilder.build(program);
43-
// printer.with_mangler(mangler);
44-
// }
44+
let mangler = self.options.mangle.then(|| ManglerBuilder.build(program));
45+
MinifierReturn { mangler }
4546
}
4647
}

0 commit comments

Comments
 (0)