vue2 自定义webpack plugin 添加版本文件到public目录下/vue-config全局环境变量

在项目根目录创建version-plugin.js文件

// 引进node fs 模块
const fs = require('fs')

// 引进node path 模块
const path = require('path')

// webpack plugin
class VersionPlugin {
   

	// 实例化传的参数
  constructor (options = {
    }) {
   
    this.options = options
    this.isFinish = false
  }

	// plugin 勾子
  apply (compiler) {
   
  	// plugin 执行完成
    compiler.hooks.done.tap('VersionPlugin', () => {
   
    // 判断已经加载过结束
      if (this.isFinish) return false
		
		// 写入文件到public目录下
      fs.writeFileSync(path.join(__dirname, 'public', 'version.json'), JSON.stringify(this.options))
      this.isFinish = true
    })
  }
}

module.exports = VersionPlugin

vue-config.js文件

const webpack = require('webpack')
const VersionPlugin = require('./version-plugin')
const now = Date.now()
module.exports = {
   
configureWebpack: {
   
		plugins: [
			// 实例自定义plugin 并传入时间缀
	      	new VersionPlugin({
    version: now }),
			
			// 定义全局环境变量
	      	new webpack.DefinePlugin({
   
	        'process.env.VUE_APP_VERSION': now
      })]
	}
}

在router跳转前可以做其它操作

import axios from 'axios'
// 在生产环境上
if (process.env.NODE_ENV === 'development') return false
// 通过axios 获取版本信息
const res = await axios.get(location.origin + '/version.json' + '?timeStamp=' + Date.now())

// 判断信息不同,操作其它逻辑
if (res.data.version !== process.env.VUE_APP_VERSION) {
   }

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 06:22:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 06:22:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 06:22:04       87 阅读
  4. Python语言-面向对象

    2024-02-01 06:22:04       96 阅读

热门阅读

  1. tomcat

    tomcat

    2024-02-01 06:22:04      41 阅读
  2. prometheus-altermanager之钉钉webhook

    2024-02-01 06:22:04       71 阅读
  3. SVN单个项目迁移到Gitlab,保留历史提交记录

    2024-02-01 06:22:04       58 阅读
  4. git 提交的文件压缩机制

    2024-02-01 06:22:04       51 阅读
  5. tomcat中不同应用session共享

    2024-02-01 06:22:04       41 阅读
  6. VTK 交互事件

    2024-02-01 06:22:04       48 阅读
  7. KAFKA鉴权设计以及相关探讨

    2024-02-01 06:22:04       65 阅读
  8. 优雅管理多线程异步任务 - 永动异步任务

    2024-02-01 06:22:04       51 阅读
  9. Jenkins插件安装推荐

    2024-02-01 06:22:04       53 阅读
  10. 2024.1.31

    2024-02-01 06:22:04       39 阅读