import { minify } from 'html-minifier-terser'
import { building } from '$app/environment'
const minification_options = {
  collapseBooleanAttributes: true,
  collapseWhitespace: true,
  conservativeCollapse: true,
  decodeEntities: true,
  html5: true,
  ignoreCustomComments: [/^#/],
  minifyCSS: true,
  minifyJS: true,
  minifyURLs: true,
  removeAttributeQuotes: true,
  removeComments: false, // some hydration code needs comments, so leave them in
  removeOptionalTags: true,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  sortAttributes: true,
  sortClassName: true,
}
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
  let page = ''
  return resolve(event, {
    transformPageChunk: ({ html, done }) => {
      page += html
      if (done) {
        return building ? minify(page, minification_options) : page
      }
    },
  })
}