vue.config.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const path = require('path')
  2. /**
  3. * resolve
  4. * @param {*} dir
  5. * @returns
  6. */
  7. function resolve(dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. /**
  11. * 生成版本号(时间戳 按小时)
  12. * @returns {String}
  13. */
  14. function getTimeStampVersion() {
  15. const now = new Date()
  16. const year = now.getFullYear()
  17. const month = now.getMonth() + 1
  18. const date = now.getDate()
  19. const hour = now.getHours()
  20. const minutes = now.getMinutes()
  21. const padZero = (n) => String(n).padStart(2, '0')
  22. return `${year}${padZero(month)}${padZero(date)}${padZero(hour)}${padZero(minutes)}`
  23. }
  24. const appVersion = getTimeStampVersion()
  25. const assetsDir = 'assets'
  26. module.exports = {
  27. publicPath: './',
  28. assetsDir: assetsDir,
  29. lintOnSave: process.env.NODE_ENV === 'development',
  30. productionSourceMap: false,
  31. devServer: {},
  32. css: {
  33. loaderOptions: {
  34. less: {
  35. modifyVars: {
  36. hack: `true; @import '~@/styles/variable.less';`,
  37. },
  38. },
  39. },
  40. },
  41. chainWebpack(config) {
  42. if (process.env.NODE_ENV === 'production') {
  43. // js和css 使用版本号
  44. config.output.filename(`${assetsDir}/js/[name].${appVersion}.js`).end()
  45. config.output.chunkFilename(`${assetsDir}/js/[name].${appVersion}.js`).end()
  46. config
  47. .plugin('extract-css')
  48. .tap((args) => {
  49. args[0].filename = `${assetsDir}/css/[name].${appVersion}.css`
  50. args[0].chunkFilename = `${assetsDir}/css/[name].${appVersion}.css`
  51. return args
  52. })
  53. .end()
  54. }
  55. // 删除预加载
  56. config.plugins.delete('preload')
  57. config.plugins.delete('prefetch')
  58. // HtmlWebpackPlugin
  59. config
  60. .plugin('html')
  61. .tap((args) => {
  62. args[0].title = process.env.VUE_APP_TITLE
  63. return args
  64. })
  65. .end()
  66. // DefinePlugin
  67. config
  68. .plugin('define')
  69. .tap((args) => {
  70. args[0]['process.env'].APP_VERSION = appVersion
  71. return args
  72. })
  73. .end()
  74. },
  75. }