vite打包如何压缩html文件
时间: 2024-05-09 17:15:49 浏览: 449
在 Vite 中,可以使用 `html-minifier` 库来压缩 HTML 文件。首先,需要安装该库:
```bash
npm install html-minifier -D
```
然后,在 `vite.config.js` 文件中配置 HTML 压缩插件:
```js
const htmlMinifier = require('html-minifier')
module.exports = {
// ...
build: {
// ...
rollupOptions: {
output: {
// 压缩 HTML 文件
manualChunks(id) {
if (id.includes('.html')) {
return 'html'
}
},
plugins: [
{
name: 'html-minify',
transform(code, id) {
if (id.endsWith('.html')) {
return htmlMinifier.minify(code, {
collapseWhitespace: true,
removeComments: true
})
}
}
}
]
}
}
}
}
```
在上述配置中,我们使用 `manualChunks` 方法将所有 HTML 文件打包到一个名为 `html` 的 chunk 中。然后,使用 `plugins` 数组配置 `html-minifier` 插件来压缩 `html` chunk 中的 HTML 文件。其中,`collapseWhitespace` 选项用于删除 HTML 中的空格,`removeComments` 选项用于删除注释。
最后,在执行 `npm run build` 命令时,Vite 将自动压缩 HTML 文件。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)