vue.config.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. module.exports = {
  26. publicPath: './',
  27. lintOnSave: process.env.NODE_ENV === 'development',
  28. productionSourceMap: false,
  29. devServer: {
  30. proxy: {
  31. '/dev-api': {
  32. target: 'http://your-proxy-path:3000',
  33. secure: false,
  34. ws: true,
  35. pathRewrite: {
  36. '^/dev-api': '',
  37. },
  38. },
  39. },
  40. },
  41. css: {
  42. loaderOptions: {
  43. less: {
  44. modifyVars: {
  45. hack: `true; @import '~@/styles/variable.less';`,
  46. },
  47. }
  48. },
  49. },
  50. chainWebpack(config) {
  51. if (process.env.NODE_ENV === 'production') {
  52. // js和css 使用版本号
  53. config.output.filename(`js/[name].${appVersion}.js`).end()
  54. config.output.chunkFilename(`js/[name].${appVersion}.js`).end()
  55. config
  56. .plugin('extract-css')
  57. .tap((args) => {
  58. args[0].filename = `css/[name].${appVersion}.css`
  59. args[0].chunkFilename = `css/[name].${appVersion}.css`
  60. return args
  61. })
  62. .end()
  63. }
  64. // 删除预加载
  65. config.plugins.delete('preload')
  66. config.plugins.delete('prefetch')
  67. // HtmlWebpackPlugin
  68. config
  69. .plugin('html')
  70. .tap((args) => {
  71. args[0].title = process.env.VUE_APP_TITLE
  72. return args
  73. })
  74. .end()
  75. // DefinePlugin
  76. config
  77. .plugin('define')
  78. .tap((args) => {
  79. args[0]['process.env'].APP_VERSION = appVersion
  80. return args
  81. })
  82. .end()
  83. },
  84. }