羊蹓狼 发表于 2024-10-16 02:05:51

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

在 Vite 项目中,默认构建 index.html 。但有时候我们需要修改 index.html 为其他文件名,比如 index-{时间戳}.html 。
我们可以如许配置 vite.config.js:
import { defineConfig } from 'vite';
import type { PluginOption } from 'vite';

// 自定义插件
type RenameHtmlPlugin = () => PluginOption;
const renameHtmlPlugin: RenameHtmlPlugin = () => {
    return {
      name: 'rename-index-html',
      enforce: 'post',
      generateBundle(_, bundle) {
            const currentTime = (Date.now() / 1000).toFixed(0);
            const newFileName = `index-${currentTime}.html`;
            bundle['index.html'].fileName = newFileName;
      },
    };
};

export default defineConfig({
    // ... 其他配置
    plugins: [
      // ... 其他插件
      renameHtmlPlugin(),
    ],
});
现在,执行 pnpm build 构建出来的就是 index-{时间戳}.html 啦。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【Vite】修改构建后的 index.html 文件名