diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 5fb02ce4306b6..9c15e0f9fea66 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -589,9 +589,9 @@ impl<'a> Codegen<'a> { fn print_quoted_utf16(&mut self, s: &str, allow_backtick: bool) { let quote = if self.options.minify { - let mut single_cost: u32 = 0; - let mut double_cost: u32 = 0; - let mut backtick_cost: u32 = 0; + let mut single_cost: i32 = 0; + let mut double_cost: i32 = 0; + let mut backtick_cost: i32 = 0; let mut bytes = s.as_bytes().iter().peekable(); while let Some(b) = bytes.next() { match b { @@ -642,7 +642,13 @@ impl<'a> Codegen<'a> { '\u{8}' => self.print_str("\\b"), // \b '\u{b}' => self.print_str("\\v"), // \v '\u{c}' => self.print_str("\\f"), // \f - '\n' => self.print_str("\\n"), + '\n' => { + if quote == b'`' { + self.print_ascii_byte(b'\n'); + } else { + self.print_str("\\n"); + } + } '\r' => self.print_str("\\r"), '\x1B' => self.print_str("\\x1B"), '\\' => self.print_str("\\\\"), diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index a05ef1f408bf4..007e41c80da27 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -39,11 +39,6 @@ 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_minify_same(r#"({"http://a\r\" \n<'b:b@c\r\nd/e?f":{}});"#); } @@ -438,3 +433,12 @@ fn getter_setter() { test_minify("({ get [foo]() {} })", "({get[foo](){}});"); test_minify("({ set [foo]() {} })", "({set[foo](){}});"); } + +#[test] +fn string() { + test_minify( + r#";'eval("\'\\vstr\\ving\\v\'") === "\\vstr\\ving\\v"'"#, + r#";`eval("'\\vstr\\ving\\v'") === "\\vstr\\ving\\v"`;"#, + ); + test_minify(r#"foo("\n")"#, "foo(`\n`);"); +} diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 05892fab0ea2f..fcc5fd5eb42f5 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -1,27 +1,27 @@ | Oxc | ESBuild | Oxc | ESBuild | Original | minified | minified | gzip | gzip | Fixture ------------------------------------------------------------------------------------- -72.14 kB | 23.74 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js +72.14 kB | 23.72 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js -173.90 kB | 60.16 kB | 59.82 kB | 19.48 kB | 19.33 kB | moment.js +173.90 kB | 60.15 kB | 59.82 kB | 19.50 kB | 19.33 kB | moment.js -287.63 kB | 90.57 kB | 90.07 kB | 32.18 kB | 31.95 kB | jquery.js +287.63 kB | 90.57 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js -342.15 kB | 118.59 kB | 118.14 kB | 44.53 kB | 44.37 kB | vue.js +342.15 kB | 118.54 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js -544.10 kB | 72.02 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js +544.10 kB | 72.00 kB | 72.48 kB | 26.20 kB | 26.20 kB | lodash.js -555.77 kB | 273.89 kB | 270.13 kB | 91.18 kB | 90.80 kB | d3.js +555.77 kB | 273.88 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js -1.01 MB | 461.04 kB | 458.89 kB | 126.89 kB | 126.71 kB | bundle.min.js +1.01 MB | 461.00 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js -1.25 MB | 656.62 kB | 646.76 kB | 164.13 kB | 163.73 kB | three.js +1.25 MB | 653.54 kB | 646.76 kB | 164.05 kB | 163.73 kB | three.js -2.14 MB | 727.99 kB | 724.14 kB | 180.35 kB | 181.07 kB | victory.js +2.14 MB | 727.91 kB | 724.14 kB | 180.39 kB | 181.07 kB | victory.js -3.20 MB | 1.01 MB | 1.01 MB | 332.24 kB | 331.56 kB | echarts.js +3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js -6.69 MB | 2.32 MB | 2.31 MB | 493.24 kB | 488.28 kB | antd.js +6.69 MB | 2.32 MB | 2.31 MB | 493.25 kB | 488.28 kB | antd.js -10.95 MB | 3.51 MB | 3.49 MB | 910.83 kB | 915.50 kB | typescript.js +10.95 MB | 3.51 MB | 3.49 MB | 910.90 kB | 915.50 kB | typescript.js