vue.config.js 5.9 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. // proxy: {
  35. // "/admin": {
  36. // target: process.env.NODE_ENV,
  37. // ws: true,
  38. // secure: false, // 如果是https接口,需要配置这个参数
  39. // changeOrigin: true, // 是否跨域
  40. // // pathRewrite: {
  41. // // // 路径重写
  42. // // "/admin": "/admin" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
  43. // // }
  44. // }
  45. // },
  46. port: port,
  47. open: true,
  48. overlay: {
  49. warnings: false,
  50. errors: true
  51. },
  52. // before: require('./mock/mock-server.js')
  53. },
  54. configureWebpack: config => {
  55. // 入口文件
  56. config.entry.app = ["babel-polyfill", "./src/main.js"];
  57. config.externals = {
  58. 'vue': 'Vue',
  59. // 'vuex': 'Vuex',
  60. axios: "axios",
  61. 'vue-router': 'VueRouter'
  62. };
  63. if (isProduction) {
  64. config.plugins.push(new CompressionWebpackPlugin({
  65. algorithm: 'gzip',
  66. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  67. threshold: 10240,
  68. // deleteOriginalAssets:true, //删除源文件,不建议
  69. minRatio: 0.7
  70. }))
  71. }
  72. // 删除console插件
  73. let plugins = [
  74. new UglifyJsPlugin({
  75. uglifyOptions: {
  76. compress: {
  77. // warnings: false, // `warnings` is not a supported option
  78. drop_console: true,
  79. drop_debugger: true
  80. },
  81. output: {
  82. // 去掉注释内容
  83. comments: true
  84. }
  85. },
  86. cache: true, //启用/禁用文件缓存(类型可布尔也可是字符串)
  87. sourceMap: false,
  88. parallel: true
  89. })
  90. ];
  91. // 只有打包生产环境才需要将console删除
  92. if (isProduction) {
  93. config.plugins = [...config.plugins, ...plugins];
  94. }
  95. },
  96. chainWebpack(config) {
  97. // it can improve the speed of the first screen, it is recommended to turn on preload
  98. // it can improve the speed of the first screen, it is recommended to turn on preload
  99. config.plugin('preload').tap(() => [
  100. {
  101. rel: 'preload',
  102. // to ignore runtime.js
  103. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  104. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  105. include: 'initial'
  106. }
  107. ])
  108. // when there are many pages, it will cause too many meaningless requests
  109. config.plugins.delete('prefetch')
  110. // set svg-sprite-loader
  111. config.module
  112. .rule('svg')
  113. .exclude.add(resolve('src/icons'))
  114. .end()
  115. config.module
  116. .rule('icons')
  117. .test(/\.svg$/)
  118. .include.add(resolve('src/icons'))
  119. .end()
  120. .use('svg-sprite-loader')
  121. .loader('svg-sprite-loader')
  122. .options({
  123. symbolId: 'icon-[name]'
  124. })
  125. .end()
  126. //process.env.NODE_ENV !== 'development'
  127. config
  128. .when(true,
  129. config => {
  130. config
  131. .plugin('ScriptExtHtmlWebpackPlugin')
  132. .after('html')
  133. .use('script-ext-html-webpack-plugin', [{
  134. // `runtime` must same as runtimeChunk name. default is `runtime`
  135. inline: /runtime\..*\.js$/
  136. }])
  137. .end()
  138. config
  139. .optimization.splitChunks({
  140. chunks: 'all',
  141. cacheGroups: {
  142. libs: {
  143. name: 'chunk-libs',
  144. test: /[\\/]node_modules[\\/]/,
  145. priority: 10,
  146. chunks: 'initial' // only package third parties that are initially dependent
  147. },
  148. elementUI: {
  149. name: 'chunk-elementUI', // split elementUI into a single package
  150. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  151. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  152. },
  153. commons: {
  154. name: 'chunk-commons',
  155. test: resolve('src/components'), // can customize your rules
  156. minChunks: 3, // minimum common number
  157. priority: 5,
  158. reuseExistingChunk: true
  159. }
  160. }
  161. })
  162. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  163. config.optimization.runtimeChunk('single')
  164. }
  165. )
  166. }
  167. }