vue.config.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 defaultSettings = require('./src/settings.js')
  7. const isProduction = process.env.NODE_ENV === 'production';
  8. function resolve(dir) {
  9. return path.join(__dirname, dir)
  10. }
  11. const name = defaultSettings.title || '采销咨询平台' // page title
  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: process.env.NODE_ENV === 'development',
  31. productionSourceMap: false,
  32. devServer: {
  33. // proxy: {
  34. // "/admin": {
  35. // target: process.env.NODE_ENV,
  36. // ws: true,
  37. // secure: false, // 如果是https接口,需要配置这个参数
  38. // changeOrigin: true, // 是否跨域
  39. // // pathRewrite: {
  40. // // // 路径重写
  41. // // "/admin": "/admin" // 这个意思就是以api开头的,定向到哪里, 如果你的后边还有路径的话, 会自动拼接上
  42. // // }
  43. // }
  44. // },
  45. port: port,
  46. open: true,
  47. overlay: {
  48. warnings: false,
  49. errors: true
  50. },
  51. // before: require('./mock/mock-server.js')
  52. },
  53. // configureWebpack:
  54. // // config => {
  55. // // // 入口文件
  56. // // config.entry.app = ["babel-polyfill", "./src/main.js"];
  57. // // config.externals = {
  58. // // 'vue': 'Vue',
  59. // // 'vuex': 'vuex',
  60. // // axios: "axios",
  61. // // "element-ui": "ELEMENT",
  62. // // 'vue-router': 'VueRouter'
  63. // // };
  64. // // },
  65. // {
  66. // // provide the app's title in webpack's name field, so that
  67. // // it can be accessed in index.html to inject the correct title.
  68. // // entry:{
  69. // // app : ["babel-polyfill", "./src/main.js"],
  70. // // },
  71. // name: name,
  72. // resolve: {
  73. // alias: {
  74. // '@': resolve('src')
  75. // }
  76. // }
  77. // },
  78. configureWebpack: config => {
  79. // 入口文件
  80. config.entry.app = ["babel-polyfill", "./src/main.js"];
  81. config.externals = {
  82. 'vue': 'Vue',
  83. // 'vuex': 'vuex',
  84. axios: "axios",
  85. "element-ui": "ELEMENT",
  86. 'vue-router': 'VueRouter'
  87. };
  88. if (isProduction) {
  89. config.plugins.push(new CompressionWebpackPlugin({
  90. algorithm: 'gzip',
  91. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  92. threshold: 10240,
  93. // deleteOriginalAssets:true, //删除源文件,不建议
  94. minRatio: 0.8
  95. }))
  96. }
  97. // 删除console插件
  98. let plugins = [
  99. new UglifyJsPlugin({
  100. uglifyOptions: {
  101. compress: {
  102. // warnings: false, // `warnings` is not a supported option
  103. drop_console: true,
  104. drop_debugger: true
  105. },
  106. output: {
  107. // 去掉注释内容
  108. comments: true
  109. }
  110. },
  111. cache: true, //启用/禁用文件缓存(类型可布尔也可是字符串)
  112. sourceMap: false,
  113. parallel: true
  114. })
  115. ];
  116. // 只有打包生产环境才需要将console删除
  117. if (isProduction) {
  118. config.plugins = [...config.plugins, ...plugins];
  119. }
  120. },
  121. chainWebpack(config) {
  122. // it can improve the speed of the first screen, it is recommended to turn on preload
  123. // it can improve the speed of the first screen, it is recommended to turn on preload
  124. config.plugin('preload').tap(() => [
  125. {
  126. rel: 'preload',
  127. // to ignore runtime.js
  128. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  129. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  130. include: 'initial'
  131. }
  132. ])
  133. // when there are many pages, it will cause too many meaningless requests
  134. config.plugins.delete('prefetch')
  135. // set svg-sprite-loader
  136. config.module
  137. .rule('svg')
  138. .exclude.add(resolve('src/icons'))
  139. .end()
  140. config.module
  141. .rule('icons')
  142. .test(/\.svg$/)
  143. .include.add(resolve('src/icons'))
  144. .end()
  145. .use('svg-sprite-loader')
  146. .loader('svg-sprite-loader')
  147. .options({
  148. symbolId: 'icon-[name]'
  149. })
  150. .end()
  151. config
  152. .when(process.env.NODE_ENV !== 'development',
  153. config => {
  154. config
  155. .plugin('ScriptExtHtmlWebpackPlugin')
  156. .after('html')
  157. .use('script-ext-html-webpack-plugin', [{
  158. // `runtime` must same as runtimeChunk name. default is `runtime`
  159. inline: /runtime\..*\.js$/
  160. }])
  161. .end()
  162. config
  163. .optimization.splitChunks({
  164. chunks: 'all',
  165. cacheGroups: {
  166. libs: {
  167. name: 'chunk-libs',
  168. test: /[\\/]node_modules[\\/]/,
  169. priority: 10,
  170. chunks: 'initial' // only package third parties that are initially dependent
  171. },
  172. elementUI: {
  173. name: 'chunk-elementUI', // split elementUI into a single package
  174. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  175. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  176. },
  177. commons: {
  178. name: 'chunk-commons',
  179. test: resolve('src/components'), // can customize your rules
  180. minChunks: 3, // minimum common number
  181. priority: 5,
  182. reuseExistingChunk: true
  183. }
  184. }
  185. })
  186. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  187. config.optimization.runtimeChunk('single')
  188. }
  189. )
  190. }
  191. }