vue.config.js 5.8 KB

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