ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
770 stars 114 forks source link

v3.0.368 JS invalid output "Lexical declaration cannot appear in a single-statement context" #242

Open aalbert-specialtys opened 2 years ago

aalbert-specialtys commented 2 years ago

Currently using latest version v3.0.368 with .net 6

The minifier produces invalid JS syntax from valid source. The chrome browser console displays the following error: "Lexical declaration cannot appear in a single-statement context". This bug breaks browser parsing and the whole file is ignored.

Here is an excerpt of the offending input and output:

Source

function someFunction(){
  if (CheckSessionExpiration()) {
      return;
  }

  let expirationInterval = window.setInterval(() => {
      if (CheckSessionExpiration()) {
          window.clearInterval(expirationInterval);
      }
  }, 5000);
}

Output

if(!t())let r=window.setInterval(()=>{t()&&window.clearInterval(r)},5e3)}

The problem is that let must be contained in a block { }. I would appreciate help fixing this

washamdev commented 1 year ago

I am experiencing this same issue. We have TypeScript files being rendered to .js files, then we use WebOptimizer to bundle those JS files, while also minifying all those files. After including the generated bundled and minified file on our site, we immediately start getting the following error in the console:

Uncaught SyntaxError: Lexical declaration cannot appear in a single-statement context

Note that this also began occurring after we set "target": "ES6" in our tsconfig.json file.

Is there any way to fix this? We'd like to use WebOptimizer, but can't without a fix for this.