vue.config.js 5.5 KB

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