Skip to content

Commit

Permalink
feat(codegen): minify string with backtick when needed (#8095)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 24, 2024
1 parent ad2a620 commit 0562830
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
48 changes: 31 additions & 17 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,31 +1375,27 @@ fn print_unquoted_str(s: &str, quote: u8, p: &mut Codegen) {
}
'\'' => {
if quote == b'\'' {
p.print_str("\\'");
} else {
p.print_str("'");
p.print_ascii_byte(b'\\');
}
p.print_ascii_byte(b'\'');
}
'\"' => {
if quote == b'"' {
p.print_str("\\\"");
} else {
p.print_str("\"");
p.print_ascii_byte(b'\\');
}
p.print_ascii_byte(b'"');
}
'`' => {
if quote == b'`' {
p.print_str("\\`");
} else {
p.print_str("`");
p.print_ascii_byte(b'\\');
}
p.print_ascii_byte(b'`');
}
'$' => {
if chars.peek().is_some_and(|&next| next == '{') {
p.print_str("\\$");
} else {
p.print_str("$");
if chars.peek() == Some(&'{') {
p.print_ascii_byte(b'\\');
}
p.print_ascii_byte(b'$');
}
// Allow `U+2028` and `U+2029` in string literals
// <https://tc39.es/proposal-json-superset>
Expand All @@ -1424,22 +1420,40 @@ impl Gen for StringLiteral<'_> {
let quote = if p.options.minify {
let mut single_cost: u32 = 0;
let mut double_cost: u32 = 0;
for b in s.as_bytes() {
let mut backtick_cost: u32 = 0;
let mut bytes = s.as_bytes().iter().peekable();
while let Some(b) = bytes.next() {
match b {
b'\n' if p.options.minify => {
backtick_cost = backtick_cost.saturating_sub(1);
}
b'\'' => {
single_cost += 1;
}
b'"' => {
double_cost += 1;
}
b'`' => {
backtick_cost += 1;
}
b'$' => {
if bytes.peek() == Some(&&b'{') {
backtick_cost += 1;
}
}
_ => {}
}
}
let mut quote = b'"';
if double_cost > single_cost {
b'\''
} else {
b'"'
quote = b'\'';
if single_cost > backtick_cost {
quote = b'`';
}
} else if double_cost > backtick_cost {
quote = b'`';
}
quote
} else {
p.quote
};
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ fn expr() {

test("delete 2e308", "delete (0, Infinity);\n");
test_minify("delete 2e308", "delete(1/0);");

test_minify(
r#";'eval("\'\\vstr\\ving\\v\'") === "\\vstr\\ving\\v"'"#,
r#";`eval("'\\vstr\\ving\\v'") === "\\vstr\\ving\\v"`;"#,
);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Original | minified | minified | gzip | gzip | Fixture

173.90 kB | 61.55 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js

287.63 kB | 92.60 kB | 90.07 kB | 32.27 kB | 31.95 kB | jquery.js
287.63 kB | 92.59 kB | 90.07 kB | 32.28 kB | 31.95 kB | jquery.js

342.15 kB | 121.55 kB | 118.14 kB | 44.64 kB | 44.37 kB | vue.js

Expand All @@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Fixture

3.20 MB | 1.02 MB | 1.01 MB | 332.00 kB | 331.56 kB | echarts.js

6.69 MB | 2.39 MB | 2.31 MB | 495.63 kB | 488.28 kB | antd.js
6.69 MB | 2.39 MB | 2.31 MB | 495.62 kB | 488.28 kB | antd.js

10.95 MB | 3.54 MB | 3.49 MB | 909.72 kB | 915.50 kB | typescript.js
10.95 MB | 3.54 MB | 3.49 MB | 909.73 kB | 915.50 kB | typescript.js

0 comments on commit 0562830

Please sign in to comment.