vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict'
  2. const settings = require('./src/settings.js')
  3. //const CompressionWebpackPlugin = require('compression-webpack-plugin')
  4. const productionGzipExtensions = ['js', 'css','html','img','png','svg','jpe?g','woff'];
  5. const path = require('path');
  6. const CompressionPlugin = require('compression-webpack-plugin');
  7. function resolve (dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. const name = settings.title // page title
  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. // const port = 9527 // dev port
  15. const targetUrl = process.env.VUE_APP_DEV_REQUEST_DOMAIN_PREFIX
  16. const proxyUrl = process.env.VUE_APP_BASE_API
  17. // console.log(process.env.VUE_APP_DEV_REQUEST_DOMAIN_PREFIX)
  18. // console.log(process.env.VUE_APP_BASE_API)
  19. // console.log(process.env.VUE_APP_PROD_REQUEST_DOMAIN_PREFIX)
  20. // console.log(process.env)
  21. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  22. module.exports = {
  23. /**
  24. * You will need to set publicPath if you plan to deploy your site under a sub path,
  25. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  26. * then publicPath should be set to "/bar/".
  27. * In most cases please use '/' !!!
  28. * Detail: https://cli.vuejs.org/config/#publicpath
  29. */
  30. lintOnSave : false,
  31. publicPath: './',
  32. outputDir: process.env.VUE_APP_PROJECT_NAME,
  33. assetsDir: 'static',
  34. //lintOnSave: process.env.NODE_ENV === 'development',
  35. productionSourceMap: false,
  36. devServer: {
  37. // port: port,
  38. port: 9099,
  39. open: true,
  40. overlay: {
  41. warnings: false,
  42. errors: true
  43. },
  44. contentBase: './',
  45. proxy: {
  46. // change xxx-api/login => ≥mock/login
  47. // detail: https://cli.vuejs.org/config/#devserver-proxy
  48. [proxyUrl]: {
  49. target: targetUrl,
  50. changeOrigin: true,
  51. pathRewrite: {
  52. // SpringCloud 项目使用这段配置
  53. //['^' + proxyUrl]: "/imcs"
  54. // SpringBoot 项目 请使用以下的配置
  55. ['^/api/oauth']: '/',
  56. ['^/api/authority']: '/',
  57. ['^/api/file']: '/',
  58. ['^/api/msgs']: '/',
  59. ['^/api/gateway']: '/gateway',
  60. ['^/api/gate']: '/',
  61. }
  62. }
  63. }
  64. },
  65. configureWebpack: {
  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. name: name,
  69. resolve: {
  70. alias: {
  71. '@': resolve('src')
  72. }
  73. },
  74. //压缩JS、css、图片等
  75. plugins: [
  76. new CompressionPlugin({
  77. filename: '[path].gz[query]',
  78. algorithm: 'gzip',
  79. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  80. threshold: 10240,
  81. minRatio: 0.8
  82. })
  83. ]
  84. },
  85. chainWebpack (config) {
  86. config.plugins.delete('preload') // TODO: need test
  87. config.plugins.delete('prefetch') // TODO: need test
  88. // set svg-sprite-loader
  89. config.module
  90. .rule('svg')
  91. .exclude.add(resolve('src/icons'))
  92. .end()
  93. config.module
  94. .rule('icons')
  95. .test(/\.svg$/)
  96. .include.add(resolve('src/icons'))
  97. .end()
  98. .use('svg-sprite-loader')
  99. .loader('svg-sprite-loader')
  100. .options({
  101. symbolId: 'icon-[name]'
  102. })
  103. .end()
  104. // set preserveWhitespace
  105. config.module
  106. .rule('vue')
  107. .use('vue-loader')
  108. .loader('vue-loader')
  109. .tap(options => {
  110. options.compilerOptions.preserveWhitespace = true
  111. return options
  112. })
  113. .end()
  114. //config.optimization.minimize(true)
  115. config
  116. // https://webpack.js.org/configuration/devtool/#development
  117. .when(process.env.NODE_ENV === 'development',
  118. config => config.devtool('cheap-source-map')
  119. )
  120. config
  121. .when(process.env.NODE_ENV !== 'development',
  122. config => {
  123. config
  124. .plugin('ScriptExtHtmlWebpackPlugin')
  125. .after('html')
  126. .use('script-ext-html-webpack-plugin', [{
  127. // `runtime` must same as runtimeChunk name. default is `runtime`
  128. inline: /runtime\..*\.js$/
  129. }])
  130. .end()
  131. config
  132. .optimization.splitChunks({
  133. chunks: 'all',
  134. cacheGroups: {
  135. libs: {
  136. name: 'chunk-libs',
  137. test: /[\\/]node_modules[\\/]/,
  138. priority: 10,
  139. chunks: 'initial' // only package third parties that are initially dependent
  140. },
  141. elementUI: {
  142. name: 'chunk-elementUI', // split elementUI into a single package
  143. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  144. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  145. },
  146. commons: {
  147. name: 'chunk-commons',
  148. test: resolve('src/components'), // can customize your rules
  149. minChunks: 3, // minimum common number
  150. priority: 5,
  151. reuseExistingChunk: true
  152. }
  153. }
  154. })
  155. config.optimization.runtimeChunk('single')
  156. }
  157. )
  158. }
  159. }