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
}
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:
minified code, formatted:
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:
To this:
tested with minify v11.0.1