1.1.1 • Published 2 years ago

vite-plugin-add-version v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

vite-plugin-add-version

vite构建发布包过程中给js和css添加自定义版本号防止缓存

用法

  1. 安装依赖包
npm i vite-plugin-add-version -D
  1. vite.config.js里调用
    // vite.config.js
    import addVersion from 'vite-plugin-add-version';

    const version = require('./package.json')['version'];
    const buildVersion = `${version.replace(/\./g, '_')}_${(new Date()).getTime()}`; //customer_version
    export default defineConfig({
       ...
       plugins: [
         addVersion(buildVersion),
       ]
       ...
    });
  1. vite build成功后dist/index.html里的css和js就会看到追加了自定义的版本号
<!DOCTYPE html>
<html lang="zh">
<head>
  ...
  <link rel=modulepreload href="app.css?v=1_0_0_1658715338398">
  <script type=text/javascript defer src="bundle.js?v=1_0_0_1658715338398" charset=utf-8></script>
</head>
<body>
 ...
</body>
</html>