Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/vite-plugin-simple-html. Vite plugin for HTML processing and minification
Something went wrong. Try again.
1.3 kB · 50 lines
TypeScript
at benchmark
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { minify } from 'html-minifier-terser';
import type { HtmlTagDescriptor, PluginOption } from 'vite';import type { Options as HtmlMinifierTerserOptions } from 'html-minifier-terser';
type Options = { inject?: { data?: Record<string, string>; tags?: HtmlTagDescriptor[]; }; minify?: boolean | HtmlMinifierTerserOptions;};
export const defaultMinifyOptions: HtmlMinifierTerserOptions = { collapseWhitespace: true, keepClosingSlash: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, useShortDoctype: true, minifyCSS: true,};
export default function simpleHtmlPlugin({ minify: minifyOptions = false }: Options = {}): { name: string; transformIndexHtml: { order: 'post'; handler: (html: string) => Promise<string>; };} { return { name: 'vite:simple-html:minify', transformIndexHtml: { order: 'post', handler: async (html: string) => { let result = html;
if (minifyOptions) { result = await minify( result, minifyOptions === true ? defaultMinifyOptions : minifyOptions, ); }
return result; }, }, } satisfies PluginOption;}