terser / html-minifier-terser

actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code
https://terser.org/html-minifier-terser
MIT License
376 stars 30 forks source link

Browser Version #108

Closed toomanylogins closed 2 years ago

toomanylogins commented 2 years ago

Is there a build of this that can be run client side in the browser thanks

DanielRuf commented 2 years ago

Sure, see https://terser.org/html-minifier-terser and the relevant files at https://github.com/terser/html-minifier-terser/tree/master/demo

sibiraj-s commented 2 years ago

Starting v7 browser builds are distributed to npm. See https://unpkg.com/browse/html-minifier-terser@7.0.0-alpha.1/dist

Screenshot 2022-01-30 at 1 53 32 PM

Prior to v7, bundles are not distributed to npm. You can use any cdn that can import files from github. You can find the files in the dist/ directory https://github.com/terser/html-minifier-terser/tree/v6.1.0/dist

toomanylogins commented 2 years ago

Thank you for reply. Apologies if this seems trivial. I dev db applications (4d.com). Running example code in a headless browser I cant get past this error in the web inspector. VM216:3 ReferenceError: minify is not defined at :1:5 try{minify('

foo

', { removeAttributeQuotes: true, });}catch(e){console.error(e);}

Is the minify() the correct function to call with the source html as the first param ? Thanks

sibiraj-s commented 2 years ago

Minify is not added to global variables. If you are using via UMD builds, you will need to access via UMD namespace.

<body>
  <script src="https://cdn.jsdelivr.net/npm/html-minifier-terser@7.0.0-alpha.1/dist/htmlminifier.umd.bundle.js"></script>
  <script>
    const minify = window.HTMLMinifier.minify;
  </script>
</body>

Or For modules.

<script type="module">
  import HTMLMinifier from 'https://cdn.skypack.dev/html-minifier-terser';

  const minify = HTMLMinifier.minify
</script>
toomanylogins commented 2 years ago

Thank you for trying to help. This is offline desktop application. Therefore I think i need to use something like rollup to output a script without dependencies. Is that possible ?