【Vite】修改构建后的 index.html 文件名

打印 上一主题 下一主题

主题 886|帖子 886|积分 2658

在 Vite 项目中,默认构建 index.html 。但有时候我们需要修改 index.html 为其他文件名,比如 index-{时间戳}.html 。
我们可以如许配置 vite.config.js:
  1. import { defineConfig } from 'vite';
  2. import type { PluginOption } from 'vite';
  3. // 自定义插件
  4. type RenameHtmlPlugin = () => PluginOption;
  5. const renameHtmlPlugin: RenameHtmlPlugin = () => {
  6.     return {
  7.         name: 'rename-index-html',
  8.         enforce: 'post',
  9.         generateBundle(_, bundle) {
  10.             const currentTime = (Date.now() / 1000).toFixed(0);
  11.             const newFileName = `index-${currentTime}.html`;
  12.             bundle['index.html'].fileName = newFileName;
  13.         },
  14.     };
  15. };
  16. export default defineConfig({
  17.     // ... 其他配置
  18.     plugins: [
  19.         // ... 其他插件
  20.         renameHtmlPlugin(),
  21.     ],
  22. });
复制代码
现在,执行 pnpm build 构建出来的就是 index-{时间戳}.html 啦。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

羊蹓狼

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表