aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/hooks.server.ts
blob: 3abdfb3342b6438fefe49aafa361ea86406ede40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
      }
    },
  })
}