kangax / html-minifier

Javascript-based HTML compressor/minifier (with Node.js support)
http://kangax.github.io/html-minifier/
MIT License
4.94k stars 571 forks source link

js + html fail if compress in one single line #1148

Open berto-dev opened 11 months ago

berto-dev commented 11 months ago

Hello!

If the problem has already been pointed out, I apologize in advance.

I'll try to contribute anyway...

During a runtime minify requests I realized that html and js together failed, I investigated a possible solution and noticed that, when the code is planed, it is first built leaving the js comments that return an error.

I solved it this way but, perhaps, the system should first remove the comments and then align the js code.

const opts = {
  minifyHTML: true,
  minifyJS: true,
  minifyCSS: true,
  processScripts:true,
  removeComments: true,
  removeScriptTypeAttributes: true,
  includeAutoGeneratedTags: true,
  removeAttributeQuotes: true,
  removeRedundantAttributes: true,
  removeStyleLinkTypeAttributes: true,
  preserveLineBreaks:false,
  collapseWhitespace: true,
  continueOnParseError: true,
  // ignoreCustomFragments: [/\n/]
}

minify( MyUTF8Output.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ''), opts )

In this way the html+js output minify will be complete, compressed and in a single line

in other case need .replace(/(?<!<pre>|\`)>(\s*)</g, '><') for remove every newline between tags (exluded inner pre and preformatted ``)

I don't get why go into strange confict in my config.

I hope that this post can suggest something to someone.