vue.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const env = ['develop', undefined].includes(process.env.BUILD_ENV) ? require('./config/dev.env') : require('./config/prod.env')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || '管理后台' // page title
  9. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  10. module.exports = {
  11. /**
  12. * You will need to set publicPath if you plan to deploy your site under a sub path,
  13. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  14. * then publicPath should be set to "/bar/".
  15. * In most cases please use '/' !!!
  16. * Detail: https://cli.vuejs.org/config/#publicpath
  17. */
  18. publicPath: '/',
  19. outputDir: 'dist',
  20. assetsDir: 'static',
  21. // lintOnSave: process.env.NODE_ENV === 'development',
  22. productionSourceMap: process.env.BUILD_ENV === 'develop',
  23. devServer: {
  24. port: 8080,
  25. open: true,
  26. host: '0.0.0.0',
  27. overlay: {
  28. warnings: false,
  29. errors: true
  30. },
  31. // before: require('./mock/mock-server.js')
  32. proxy: {
  33. '/api/auth': {
  34. target: 'http://shining-admin.codedreamit.com',
  35. changeOrigin: true,
  36. pathRewrite: {
  37. '^/api/auth': '/api/auth'
  38. }
  39. },
  40. '/api/admin': {
  41. target: 'http://shining-admin.codedreamit.com',
  42. changeOrigin: true,
  43. pathRewrite: {
  44. '^/api/admin': '/api/admin'
  45. }
  46. },
  47. '/api/upload': {
  48. target: 'http://shining-admin.codedreamit.com',
  49. changeOrigin: true,
  50. pathRewrite: {
  51. '^/api/upload': '/api/upload'
  52. }
  53. },
  54. '/api/system': {
  55. target: 'http://shining-admin.codedreamit.com',
  56. changeOrigin: true,
  57. pathRewrite: {
  58. '^/api/system': '/api/system'
  59. }
  60. },
  61. '/api/shop': {
  62. target: 'https://shining.codedreamit.com',
  63. changeOrigin: true,
  64. pathRewrite: {
  65. '^/api/shop': '/api/shop'
  66. }
  67. }
  68. }
  69. },
  70. configureWebpack: {
  71. // provide the app's title in webpack's name field, so that
  72. // it can be accessed in index.html to inject the correct title.
  73. name: name,
  74. resolve: {
  75. alias: {
  76. '@': resolve('src')
  77. }
  78. }
  79. },
  80. css: {
  81. loaderOptions: {
  82. sass: {
  83. prependData: `@import "@/styles/var.scss";`
  84. }
  85. }
  86. },
  87. chainWebpack(config) {
  88. // it can improve the speed of the first screen, it is recommended to turn on preload
  89. config.plugin('preload').tap(() => [
  90. {
  91. rel: 'preload',
  92. // to ignore runtime.js
  93. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  94. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  95. include: 'initial'
  96. }
  97. ])
  98. config.plugin('define').tap(args => {
  99. args[0]['process.env'] = Object.assign({}, args[0]['process.env'], env)
  100. return args
  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. 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. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  156. config.optimization.runtimeChunk('single')
  157. }
  158. )
  159. }
  160. }