vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const name = defaultSettings.title || 'vue Element Admin' // page title
  8. // If your port is set to 80,
  9. // use administrator privileges to execute the command line.
  10. // For example, Mac: sudo npm run
  11. // You can change the port by the following method:
  12. // port = 9527 npm run dev OR npm run dev --port = 9527
  13. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  14. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  15. module.exports = {
  16. /**
  17. * You will need to set publicPath if you plan to deploy your site under a sub path,
  18. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  19. * then publicPath should be set to "/bar/".
  20. * In most cases please use '/' !!!
  21. * Detail: https://cli.vuejs.org/config/#publicpath
  22. */
  23. publicPath: '/',
  24. outputDir: 'dist',
  25. assetsDir: 'static',
  26. lintOnSave: process.env.NODE_ENV === 'development',
  27. productionSourceMap: false,
  28. devServer: {
  29. port: port,
  30. open: true,
  31. overlay: {
  32. warnings: false,
  33. errors: true
  34. },
  35. proxy: {
  36. '/dev-api': {
  37. target: 'http://127.0.0.1:8088',
  38. ws: false,
  39. pathRewrite: {
  40. '^/dev-api': '/'
  41. }
  42. },
  43. '/stage-api': {
  44. target: 'http://127.0.0.1:8088',
  45. ws: false,
  46. pathRewrite: {
  47. '^/stage-api': '/'
  48. }
  49. },
  50. '/prod-api': {
  51. target: 'http://127.0.0.1:8088',
  52. ws: false,
  53. pathRewrite: {
  54. '^/prod-api': '/'
  55. }
  56. },
  57. '/': {
  58. target: 'http://127.0.0.1:8088',
  59. ws: false,
  60. pathRewrite: {
  61. '^/': '/'
  62. }
  63. }
  64. // v2.7.0 不需在配置前端代理
  65. }// ,
  66. // before: require('./mock/mock-server.js')
  67. },
  68. configureWebpack: {
  69. // provide the app's title in webpack's name field, so that
  70. // it can be accessed in index.html to inject the correct title.
  71. name: name,
  72. resolve: {
  73. alias: {
  74. '@': resolve('src')
  75. }
  76. }
  77. },
  78. chainWebpack(config) {
  79. // it can improve the speed of the first screen, it is recommended to turn on preload
  80. // config.plugins.delete('preload')
  81. // when there are many pages, it will cause too many meaningless requests
  82. config.plugins.delete('prefetch')
  83. // set svg-sprite-loader
  84. config.module
  85. .rule('svg')
  86. .exclude.add(resolve('src/icons'))
  87. .end()
  88. config.module
  89. .rule('icons')
  90. .test(/\.svg$/)
  91. .include.add(resolve('src/icons'))
  92. .end()
  93. .use('svg-sprite-loader')
  94. .loader('svg-sprite-loader')
  95. .options({
  96. symbolId: 'icon-[name]'
  97. })
  98. .end()
  99. // set preserveWhitespace
  100. config.module
  101. .rule('vue')
  102. .use('vue-loader')
  103. .loader('vue-loader')
  104. .tap(options => {
  105. options.compilerOptions.preserveWhitespace = true
  106. return options
  107. })
  108. .end()
  109. config
  110. .when(process.env.NODE_ENV !== 'development',
  111. config => {
  112. config
  113. .plugin('ScriptExtHtmlWebpackPlugin')
  114. .after('html')
  115. .use('script-ext-html-webpack-plugin', [{
  116. // `runtime` must same as runtimeChunk name. default is `runtime`
  117. inline: /runtime\..*\.js$/
  118. }])
  119. .end()
  120. config
  121. .optimization.splitChunks({
  122. chunks: 'all',
  123. cacheGroups: {
  124. libs: {
  125. name: 'chunk-libs',
  126. test: /[\\/]node_modules[\\/]/,
  127. priority: 10,
  128. chunks: 'initial' // only package third parties that are initially dependent
  129. },
  130. elementUI: {
  131. name: 'chunk-elementUI', // split elementUI into a single package
  132. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  133. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  134. },
  135. commons: {
  136. name: 'chunk-commons',
  137. test: resolve('src/components'), // can customize your rules
  138. minChunks: 3, // minimum common number
  139. priority: 5,
  140. reuseExistingChunk: true
  141. }
  142. }
  143. })
  144. config.optimization.runtimeChunk('single')
  145. }
  146. )
  147. }
  148. }