coderaiser / minify

Minifier of js, css, html and img
https://coderaiser.github.io/minify
MIT License
228 stars 29 forks source link

Order of operations not preserved when using classes or switch statement #119

Closed rOzzy1987 closed 8 months ago

rOzzy1987 commented 8 months ago

Using classes: Considering the following code which works fine on its own, minifier merges the variable declarations, and produces (a well deserved) error in runtime:

original code:

var a = "foo";
var b = 123.4;
class C {
    f() {
    }
    constructor() {
        this.d = a;
        this.e = b;
    }
}
var g = new C();
globalThis.h = g;
globalThis.i = g;

minified code, formatted:

var a='foo',
    b=123.4,
    g=new C();
class C{
  f(){}
  constructor(){
    this.d=a;
    this.e=b
  }
}
globalThis.h=g;
globalThis.i=g;

Notice that instantiation of 'c' jumped before the class declaration, thus throwing a runtime error.

Using switch statement: The switch statement case is similar.

Fom this:

var a = "x";
switch (a) {
    case "a":
        var b = "foo";
        console.log(b);
        break;
    case "x":
        var c = "bar";
        console.log(c);
        break;
}

To this:

var a='x';
switch(a) {
  case 'a':
    var b='foo',
        c='bar';
    console.log(b);
    break;
  case 'x':
    console.log(c);
    break
}

tested with minify v11.0.1

coderaiser commented 8 months ago

It should be fixed in @putout/plugin-minify

coderaiser commented 8 months ago

Just fixed in @putout/minify@3.8.0 🎉. Please re-isntall Minify. Is it works for you?

rOzzy1987 commented 8 months ago

Verified the fix, Thanks for the fast response!

Cheers mate!