From 1d5ae817d7ba8642d5f9f7ed986d5ba246e68b0e Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Mon, 23 Dec 2024 16:20:37 +0000 Subject: [PATCH] feat(codegen): minify `const [foo] = bar` -> `const[foo]=bar` (#8079) --- crates/oxc_codegen/src/gen.rs | 3 ++- .../tests/integration/snapshots/minify.snap | 2 +- crates/oxc_codegen/tests/integration/unit.rs | 7 +++++++ tasks/minsize/minsize.snap | 20 +++++++++---------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 49bf97c0317d0..508a1e502ea35 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -684,7 +684,7 @@ impl Gen for VariableDeclaration<'_> { VariableDeclarationKind::AwaitUsing => "await using", }); if !self.declarations.is_empty() { - p.print_hard_space(); + p.print_soft_space(); } p.print_list(&self.declarations, ctx); } @@ -1228,6 +1228,7 @@ impl Gen for IdentifierName<'_> { impl Gen for BindingIdentifier<'_> { fn gen(&self, p: &mut Codegen, _ctx: Context) { let name = p.get_binding_identifier_name(self); + p.print_space_before_identifier(); p.add_source_mapping_for_name(self.span, name); p.print_str(name); } diff --git a/crates/oxc_codegen/tests/integration/snapshots/minify.snap b/crates/oxc_codegen/tests/integration/snapshots/minify.snap index cc86223f995c4..59be2866aae0a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/minify.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/minify.snap @@ -196,4 +196,4 @@ export { default, /* …, */ } from "module-name"; export { default as name16 } from "module-name"; ---------- -import defaultExport from"module-name";import*as name from"module-name";import{export1}from"module-name";import{export1 as alias1}from"module-name";import{default as alias}from"module-name";import{export1,export2}from"module-name";import{export1,export2 as alias2}from"module-name";import{"string name" as alias}from"module-name";import defaultExport,{export1}from"module-name";import defaultExport,*as name from"module-name";import"module-name";import{}from"mod";export let name1,name2;export const name3=1,name4=2;export function functionName(){}export class ClassName{}export function*generatorFunctionName(){}export const {name5,name2:bar}=o;export const [name6,name7]=array;export{name8,name81};export{variable1 as name9,variable2 as name10,name82};export{variable1 as "string name"};export{name1 as default1};export*from"module-name";export*as name11 from"module-name";export{name12,nameN}from"module-name";export{import1 as name13,import2 as name14,name15}from"module-name";export{default}from"module-name";export{default as name16}from"module-name"; +import defaultExport from"module-name";import*as name from"module-name";import{export1}from"module-name";import{export1 as alias1}from"module-name";import{default as alias}from"module-name";import{export1,export2}from"module-name";import{export1,export2 as alias2}from"module-name";import{"string name" as alias}from"module-name";import defaultExport,{export1}from"module-name";import defaultExport,*as name from"module-name";import"module-name";import{}from"mod";export let name1,name2;export const name3=1,name4=2;export function functionName(){}export class ClassName{}export function*generatorFunctionName(){}export const{name5,name2:bar}=o;export const[name6,name7]=array;export{name8,name81};export{variable1 as name9,variable2 as name10,name82};export{variable1 as "string name"};export{name1 as default1};export*from"module-name";export*as name11 from"module-name";export{name12,nameN}from"module-name";export{import1 as name13,import2 as name14,name15}from"module-name";export{default}from"module-name";export{default as name16}from"module-name"; diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index d8b4502a1ec51..24abe8385a157 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -2,6 +2,13 @@ use oxc_codegen::CodegenOptions; use crate::tester::{test, test_minify, test_minify_same, test_options}; +#[test] +fn decl() { + test_minify("const [foo] = bar", "const[foo]=bar;"); + test_minify("const {foo} = bar", "const{foo}=bar;"); + test_minify("const foo = bar", "const foo=bar;"); +} + #[test] fn module_decl() { test("export * as foo from 'foo'", "export * as foo from \"foo\";\n"); diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index d7fa503d83eb5..85b5bc50b1097 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -5,23 +5,23 @@ Original | minified | minified | gzip | gzip | Fixture 173.90 kB | 61.60 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js -287.63 kB | 92.61 kB | 90.07 kB | 32.27 kB | 31.95 kB | jquery.js +287.63 kB | 92.60 kB | 90.07 kB | 32.27 kB | 31.95 kB | jquery.js -342.15 kB | 121.57 kB | 118.14 kB | 44.64 kB | 44.37 kB | vue.js +342.15 kB | 121.56 kB | 118.14 kB | 44.64 kB | 44.37 kB | vue.js -544.10 kB | 73.37 kB | 72.48 kB | 26.14 kB | 26.20 kB | lodash.js +544.10 kB | 73.32 kB | 72.48 kB | 26.13 kB | 26.20 kB | lodash.js -555.77 kB | 276.21 kB | 270.13 kB | 91.16 kB | 90.80 kB | d3.js +555.77 kB | 276.08 kB | 270.13 kB | 91.14 kB | 90.80 kB | d3.js -1.01 MB | 467.13 kB | 458.89 kB | 126.76 kB | 126.71 kB | bundle.min.js +1.01 MB | 467.00 kB | 458.89 kB | 126.74 kB | 126.71 kB | bundle.min.js -1.25 MB | 662.63 kB | 646.76 kB | 164.01 kB | 163.73 kB | three.js +1.25 MB | 661.60 kB | 646.76 kB | 163.94 kB | 163.73 kB | three.js -2.14 MB | 740.53 kB | 724.14 kB | 181.38 kB | 181.07 kB | victory.js +2.14 MB | 740.48 kB | 724.14 kB | 181.35 kB | 181.07 kB | victory.js -3.20 MB | 1.02 MB | 1.01 MB | 332.11 kB | 331.56 kB | echarts.js +3.20 MB | 1.02 MB | 1.01 MB | 332.01 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.55 MB | 3.49 MB | 909.73 kB | 915.50 kB | typescript.js +10.95 MB | 3.54 MB | 3.49 MB | 909.70 kB | 915.50 kB | typescript.js