aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/minifyHTMLVitePlugin.ts
blob: 7c08c488d72c4916d4187364c25301cef7641626 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { minify, type Options as TerserOptions } from 'html-minifier-terser';
import type { PluginOption } from 'vite';

export default function minifyHTMLVitePlugin(
  options?: TerserOptions | undefined,
): PluginOption {
  return {
    name: 'minify-html',
    apply: 'build',
    enforce: 'post',
    transformIndexHtml(html) {
      return minify(html, {
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        minifyCSS: true,
        removeComments: true,
        removeAttributeQuotes: true,
        removeRedundantAttributes: true,
        sortAttributes: true,
        ...(options ?? {}),
      });
    },
  };
}