vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if (isProduction) {
  48. config.plugins.push(new CompressionWebpackPlugin({
  49. algorithm: 'gzip',
  50. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  51. threshold: 10240,
  52. // deleteOriginalAssets:true, //删除源文件,不建议
  53. minRatio: 0.5
  54. }))
  55. }
  56. // 删除console插件
  57. let plugins = [
  58. new UglifyJsPlugin({
  59. uglifyOptions: {
  60. compress: {
  61. // warnings: false, // `warnings` is not a supported option
  62. drop_console: true,
  63. drop_debugger: true
  64. },
  65. output: {
  66. // 去掉注释内容
  67. comments: true
  68. }
  69. },
  70. cache: true, //启用/禁用文件缓存(类型可布尔也可是字符串)
  71. sourceMap: false,
  72. parallel: true
  73. })
  74. ];
  75. // 只有打包生产环境才需要将console删除
  76. if (isProduction) {
  77. config.plugins = [...config.plugins, ...plugins];
  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. // set svg-sprite-loader
  95. config.module
  96. .rule('svg')
  97. .exclude.add(resolve('src/icons'))
  98. .end()
  99. config.module
  100. .rule('icons')
  101. .test(/\.svg$/)
  102. .include.add(resolve('src/icons'))
  103. .end()
  104. .use('svg-sprite-loader')
  105. .loader('svg-sprite-loader')
  106. .options({
  107. symbolId: 'icon-[name]'
  108. })
  109. .end()
  110. //process.env.NODE_ENV !== 'development'
  111. config
  112. .when(true,
  113. config => {
  114. config
  115. .plugin('ScriptExtHtmlWebpackPlugin')
  116. .after('html')
  117. .use('script-ext-html-webpack-plugin', [{
  118. // `runtime` must same as runtimeChunk name. default is `runtime`
  119. inline: /runtime\..*\.js$/
  120. }])
  121. .end()
  122. config
  123. .optimization.splitChunks({
  124. chunks: 'all',
  125. cacheGroups: {
  126. libs: {
  127. name: 'chunk-libs',
  128. test: /[\\/]node_modules[\\/]/,
  129. priority: 10,
  130. chunks: 'initial' // only package third parties that are initially dependent
  131. },
  132. // elementUI: {
  133. // name: 'chunk-elementUI', // split elementUI into a single package
  134. // priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  135. // test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  136. // },
  137. commons: {
  138. name: 'chunk-commons',
  139. test: resolve('src/components'), // can customize your rules
  140. minChunks: 3, // minimum common number
  141. priority: 5,
  142. reuseExistingChunk: true
  143. }
  144. }
  145. })
  146. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  147. config.optimization.runtimeChunk('single')
  148. }
  149. )
  150. }
  151. }