vue.config.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. port: port,
  33. open: true,
  34. overlay: {
  35. warnings: false,
  36. errors: true
  37. },
  38. },
  39. configureWebpack: config => {
  40. // 入口文件
  41. config.entry.app = ["babel-polyfill", "./src/main.js"];
  42. config.externals = {
  43. 'vue': 'Vue',
  44. axios: "axios",
  45. 'vue-router': 'VueRouter'
  46. };
  47. // 删除console插件
  48. let plugins = [
  49. new UglifyJsPlugin({
  50. uglifyOptions: {
  51. compress: {
  52. // warnings: false, // `warnings` is not a supported option
  53. drop_console: true,
  54. drop_debugger: true
  55. },
  56. output: {
  57. // 去掉注释内容
  58. comments: true
  59. }
  60. },
  61. cache: true, //启用/禁用文件缓存(类型可布尔也可是字符串)
  62. sourceMap: false,
  63. parallel: true
  64. })
  65. ];
  66. // 只有打包生产环境才需要将console删除
  67. if (isProduction) {
  68. config.plugins = [...config.plugins, ...plugins];
  69. }
  70. if (isProduction) {
  71. config.plugins.push(new CompressionWebpackPlugin({
  72. algorithm: 'gzip',
  73. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  74. threshold: 10240,
  75. // deleteOriginalAssets:true, //删除源文件,不建议
  76. minRatio: 0.7
  77. }))
  78. }
  79. },
  80. chainWebpack(config) {
  81. // it can improve the speed of the first screen, it is recommended to turn on preload
  82. // it can improve the speed of the first screen, it is recommended to turn on preload
  83. config.plugin('preload').tap(() => [
  84. {
  85. rel: 'preload',
  86. // to ignore runtime.js
  87. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  88. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  89. include: 'initial'
  90. }
  91. ])
  92. // when there are many pages, it will cause too many meaningless requests
  93. config.plugins.delete('prefetch')
  94. // 解决ie11兼容ES6
  95. config.entry('main').add('babel-polyfill')
  96. // config.module.rule('images')
  97. // .test(/\.(png|jpe?g|gif)(\?.*)?$/)
  98. // .use('image-webpack-loader')
  99. // .loader('image-webpack-loader')
  100. // .options({ bypassOnDebug: true })
  101. // 开启图片压缩
  102. // config.module.rule('images')
  103. // .test(/\.(png|jpe?g|gif)(\?.*)?$/)
  104. // .use('url-loader')
  105. // .loader('url-loader')
  106. // .use('image-webpack-loader')
  107. // .loader('image-webpack-loader')
  108. // .options({ bypassOnDebug: true })
  109. // 开启js、css压缩
  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. transpileDependencies: [
  168. 'biyi-admin', // 指定对第三方依赖包进行babel-polyfill处理
  169. ]
  170. }