qidao123.com技术社区-IT企服评测·应用市场

标题: 【Vite】修改构建后的 index.html 文件名 [打印本页]

作者: 羊蹓狼    时间: 2024-10-16 02:05
标题: 【Vite】修改构建后的 index.html 文件名
在 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企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4