vue.config.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const path = require('path')
  2. const rm = require('rimraf')
  3. // 监听打包完成
  4. const FileManagerPlugin = require('filemanager-webpack-plugin')
  5. const date = new Date()
  6. const y = date.getFullYear()
  7. const m = (date.getMonth() + 1).toString().padStart(2, '0')
  8. const d = date.getDate().toString().padStart(2, '0')
  9. const h = date.getHours().toString().padStart(2, '0')
  10. const timestamp = `?${y}${m}${d}${h}`
  11. const env = ['develop', undefined].includes(process.env.BUILD_ENV) ? require('./config/dev.env') : (process.env.BUILD_ENV === 'release' ? require('./config/release.env') : require('./config/prod.env'))
  12. const resolve = (dir) => {
  13. return path.join(__dirname, dir)
  14. }
  15. const addStyleResource = (rule) => {
  16. rule.use('style-resource')
  17. .loader('style-resources-loader')
  18. .options({
  19. patterns: [
  20. path.resolve(__dirname, './src/assets/styles/variable.scss')
  21. ]
  22. })
  23. }
  24. if (process.env.NODE_ENV !== 'development') {
  25. rm(resolve('./dist'), err => {
  26. if (err) throw err
  27. })
  28. }
  29. module.exports = {
  30. publicPath: '/',
  31. outputDir: './dist', // 构建文件目录,默认dist
  32. filenameHashing: false,
  33. productionSourceMap: process.env.BUILD_ENV === 'develop',
  34. devServer: {
  35. hot: true,
  36. hotOnly: true,
  37. host: '0.0.0.0',
  38. port: 8080,
  39. open: true,
  40. clientLogLevel: 'warning',
  41. proxy: {
  42. '/api': {
  43. target: 'https://www.baidu.com',
  44. ws: true,
  45. changeOrigin: true
  46. }
  47. },
  48. overlay: {
  49. warnings: true,
  50. errors: false
  51. },
  52. before (app) {
  53. }
  54. },
  55. configureWebpack: {
  56. resolve: {
  57. alias: {
  58. '@': resolve('src')
  59. }
  60. },
  61. output: {
  62. filename: 'js/[name].js' + timestamp,
  63. chunkFilename: 'js/[name].js' + timestamp
  64. },
  65. plugins: process.env.NODE_ENV !== 'development' ? [
  66. new FileManagerPlugin({
  67. onEnd: [
  68. {
  69. delete: ['./dist/*/favicon.ico', './dist/*/index.html']
  70. }
  71. ]
  72. })
  73. ] : []
  74. },
  75. chainWebpack: config => {
  76. const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
  77. types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)))
  78. config.plugin('define').tap(args => {
  79. args[0]['process.env'] = Object.assign({}, args[0]['process.env'], env)
  80. return args
  81. })
  82. config.plugins.delete('preload')
  83. config.plugins.delete('prefetch')
  84. },
  85. css: {
  86. extract: {
  87. filename: 'css/[name].css' + timestamp,
  88. chunkFilename: 'css/[name].css' + timestamp
  89. },
  90. sourceMap: false
  91. }
  92. }