Files
vue-ts-example-2025/vite-plugins/09.index-html-plugin.ts
严浩 7d2d4675fc
Some checks failed
CI/CD Pipeline / playwright (push) Successful in 4m30s
CI/CD Pipeline / build-and-deploy (push) Has been cancelled
feat(layout): 调整基础布局结构与样式
2025-10-23 23:06:37 +08:00

29 lines
796 B
TypeScript

import type { PluginOption } from 'vite';
import { minify as minifyHtml } from 'html-minifier-terser';
function IndexHtmlPlugin(): PluginOption {
return {
name: 'index-html-plugin',
apply: 'build',
async transformIndexHtml(html) {
console.time('minifyHtml');
// 压缩 HTML
const minifiedHtml = await minifyHtml(html, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true,
});
console.log();
console.timeEnd('minifyHtml');
return minifiedHtml;
},
};
}
export default [IndexHtmlPlugin()] satisfies PluginOption[];