vue.config.js 5.4 KB

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