vue.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: 'http://tingbang-api.codedreamit.com',
  44. ws: true,
  45. changeOrigin: true,
  46. pathRewrite: {
  47. '^/api': '/api'
  48. }
  49. }
  50. },
  51. overlay: {
  52. warnings: false,
  53. errors: false
  54. },
  55. before (app) {
  56. }
  57. },
  58. configureWebpack: {
  59. resolve: {
  60. alias: {
  61. '@': resolve('src')
  62. }
  63. },
  64. output: {
  65. filename: 'js/[name].[hash].js' + timestamp,
  66. chunkFilename: 'js/[name].[hash].js' + timestamp
  67. },
  68. plugins: process.env.NODE_ENV !== 'development' ? [
  69. new FileManagerPlugin({
  70. onEnd: [
  71. {
  72. delete: ['./dist/*/favicon.ico', './dist/*/index.html']
  73. }
  74. ]
  75. })
  76. ] : [],
  77. performance: {
  78. hints: 'warning',
  79. // 入口起点的最大体积 整数类型(以字节为单位)
  80. maxEntrypointSize: 5000000,
  81. // 生成文件的最大体积 整数类型(以字节为单位 300k)
  82. maxAssetSize: 3000000,
  83. // 只给出 js 文件的性能提示
  84. assetFilter: function (assetFilename) {
  85. return assetFilename.endsWith('.js')
  86. }
  87. }
  88. },
  89. chainWebpack: config => {
  90. const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
  91. types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)))
  92. config.plugin('define').tap(args => {
  93. args[0]['process.env'] = Object.assign({}, args[0]['process.env'], env)
  94. return args
  95. })
  96. config.plugins.delete('preload')
  97. config.plugins.delete('prefetch')
  98. },
  99. css: process.env.NODE_ENV !== 'development' ? {
  100. extract: {
  101. filename: 'css/[name].[hash].css' + timestamp,
  102. chunkFilename: 'css/[name].[hash].css' + timestamp,
  103. ignoreOrder: true
  104. },
  105. sourceMap: false
  106. } : {}
  107. }