refactor(vite): 重构 Vite 插件加载机制

This commit is contained in:
严浩
2025-10-23 12:30:24 +08:00
parent f1536ed24c
commit 7b9dee68cc
27 changed files with 383 additions and 258 deletions

View File

@@ -0,0 +1,28 @@
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[];