vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict'
  2. const path = require('path')
  3. require('babel-polyfill')
  4. const productionGzipExtensions = ['js', 'css']
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  6. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  7. const defaultSettings = require('./src/settings.js')
  8. const isProduction = process.env.NODE_ENV === 'production'
  9. function resolve(dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. const name = defaultSettings.title || '采销平台' // page title
  13. // If your port is set to 80,
  14. // use administrator privileges to execute the command line.
  15. // For example, Mac: sudo npm run
  16. // You can change the port by the following method:
  17. // port = 8080 npm run dev OR npm run dev --port = 8080
  18. const port = process.env.port || process.env.npm_config_port || 8080 // dev port
  19. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  20. module.exports = {
  21. /**
  22. * You will need to set publicPath if you plan to deploy your site under a sub path,
  23. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  24. * then publicPath should be set to "/bar/".
  25. * In most cases please use '/' !!!
  26. * Detail: https://cli.vuejs.org/config/#publicpath
  27. */
  28. publicPath: '/',
  29. outputDir: 'dist',
  30. assetsDir: 'static',
  31. lintOnSave: false, // process.env.NODE_ENV === 'development'
  32. productionSourceMap: false,
  33. devServer: {
  34. headers: {
  35. 'max-http-header-size': 1024000
  36. },
  37. // proxy: {
  38. // "/admin": {
  39. // target: process.env.NODE_ENV,
  40. // ws: true,
  41. // secure: false, // 如果是https接口,需要配置这个参数
  42. // changeOrigin: true, // 是否跨域
  43. // // pathRewrite: {
  44. // // // 路径重写
  45. // // "/admin": "/admin" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
  46. // // }
  47. // }
  48. // },
  49. port: port,
  50. open: true,
  51. overlay: {
  52. warnings: false,
  53. errors: true
  54. }
  55. // before: require('./mock/mock-server.js')
  56. },
  57. configureWebpack: (config) => {
  58. // 入口文件
  59. config.entry.app = ['babel-polyfill', './src/main.js']
  60. config.externals = {
  61. vue: 'Vue',
  62. // 'vuex': 'Vuex',
  63. axios: 'axios',
  64. 'vue-router': 'VueRouter'
  65. }
  66. if (isProduction) {
  67. config.plugins.push(
  68. new CompressionWebpackPlugin({
  69. algorithm: 'gzip',
  70. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  71. threshold: 10240,
  72. // deleteOriginalAssets:true, //删除源文件,不建议
  73. minRatio: 0.7
  74. })
  75. )
  76. }
  77. // 删除console插件
  78. const plugins = [
  79. new UglifyJsPlugin({
  80. uglifyOptions: {
  81. compress: {
  82. // warnings: false, // `warnings` is not a supported option
  83. drop_console: true,
  84. drop_debugger: true
  85. },
  86. output: {
  87. // 去掉注释内容
  88. comments: true
  89. }
  90. },
  91. cache: true, // 启用/禁用文件缓存(类型可布尔也可是字符串)
  92. sourceMap: false,
  93. parallel: true
  94. })
  95. ]
  96. // 只有打包生产环境才需要将console删除
  97. if (isProduction) {
  98. config.plugins = [...config.plugins, ...plugins]
  99. }
  100. },
  101. chainWebpack(config) {
  102. // it can improve the speed of the first screen, it is recommended to turn on preload
  103. // it can improve the speed of the first screen, it is recommended to turn on preload
  104. config.plugin('preload').tap(() => [
  105. {
  106. rel: 'preload',
  107. // to ignore runtime.js
  108. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  109. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  110. include: 'initial'
  111. }
  112. ])
  113. // when there are many pages, it will cause too many meaningless requests
  114. config.plugins.delete('prefetch')
  115. // set svg-sprite-loader
  116. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  117. config.module
  118. .rule('icons')
  119. .test(/\.svg$/)
  120. .include.add(resolve('src/icons'))
  121. .end()
  122. .use('svg-sprite-loader')
  123. .loader('svg-sprite-loader')
  124. .options({
  125. symbolId: 'icon-[name]'
  126. })
  127. .end()
  128. // process.env.NODE_ENV !== 'development'
  129. config.when(true, (config) => {
  130. config
  131. .plugin('ScriptExtHtmlWebpackPlugin')
  132. .after('html')
  133. .use('script-ext-html-webpack-plugin', [
  134. {
  135. // `runtime` must same as runtimeChunk name. default is `runtime`
  136. inline: /runtime\..*\.js$/
  137. }
  138. ])
  139. .end()
  140. config.optimization.splitChunks({
  141. chunks: 'all',
  142. cacheGroups: {
  143. libs: {
  144. name: 'chunk-libs',
  145. test: /[\\/]node_modules[\\/]/,
  146. priority: 10,
  147. chunks: 'initial' // only package third parties that are initially dependent
  148. },
  149. elementUI: {
  150. name: 'chunk-elementUI', // split elementUI into a single package
  151. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  152. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  153. },
  154. commons: {
  155. name: 'chunk-commons',
  156. test: resolve('src/components'), // can customize your rules
  157. minChunks: 3, // minimum common number
  158. priority: 5,
  159. reuseExistingChunk: true
  160. }
  161. }
  162. })
  163. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  164. config.optimization.runtimeChunk('single')
  165. })
  166. }
  167. }