Skip to content

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

License

Notifications You must be signed in to change notification settings

liuhean2021/vite-plugin-add-version

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published