vue.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict'
  2. const path = require('path')
  3. const pkg = require('./package.json')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const name = pkg.name || 'vue-element-admin' // page title
  8. const port = 9528 // dev port
  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: process.env.VUE_APP_CONTEXT,
  19. outputDir: 'dist',
  20. assetsDir: 'static',
  21. lintOnSave: process.env.NODE_ENV === 'development' ? 'error' : false,
  22. productionSourceMap: false,
  23. devServer: {
  24. port: port,
  25. open: false,
  26. overlay: {
  27. warnings: false,
  28. errors: true
  29. },
  30. proxy: {
  31. '/pb/mock': {
  32. target: 'http://127.0.0.1:4523/mock/349485',//目标地址
  33. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  34. pathRewrite: {
  35. '^/pb/mock': '/'
  36. }, //这里重写路径
  37. logLevel: 'debug',
  38. },
  39. '/pb': {
  40. target: 'http://192.168.1.110:8090/',
  41. // target: 'https://standard-dev.winsea.com/',
  42. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  43. pathRewrite: {
  44. '^/pb': '/'
  45. }, //这里重写路径
  46. secure: false,
  47. logLevel: 'debug',
  48. },
  49. },
  50. after(app) {
  51. require('@babel/register')
  52. const bodyParser = require('body-parser')
  53. // parse app.body
  54. // http://expressjs.com/en/4x/api.html#req.body
  55. app.use(bodyParser.json())
  56. app.use(
  57. bodyParser.urlencoded({
  58. extended: true
  59. })
  60. )
  61. // const { default: mocks } = require('./mock')
  62. // for (const mock of mocks) {
  63. // app[mock.type](mock.url, mock.response)
  64. // }
  65. }
  66. },
  67. configureWebpack: {
  68. // provide the app's title in webpack's name field, so that
  69. // it can be accessed in index.html to inject the correct title.
  70. name: name,
  71. resolve: {
  72. alias: {
  73. '@': resolve('src')
  74. }
  75. }
  76. },
  77. chainWebpack(config) {
  78. config.plugins.delete('preload') // TODO: need test
  79. config.plugins.delete('prefetch') // TODO: need test
  80. // set svg-sprite-loader
  81. config.module
  82. .rule('svg')
  83. .exclude.add(resolve('src/assets/icons'))
  84. .end()
  85. config.module
  86. .rule('icons')
  87. .test(/\.svg$/)
  88. .include.add(resolve('src/assets/icons'))
  89. .end()
  90. .use('svg-sprite-loader')
  91. .loader('svg-sprite-loader')
  92. .options({
  93. symbolId: 'icon-[name]'
  94. })
  95. .end()
  96. // set preserveWhitespace
  97. config.module
  98. .rule('vue')
  99. .use('vue-loader')
  100. .loader('vue-loader')
  101. .tap(options => {
  102. options.compilerOptions.preserveWhitespace = true
  103. return options
  104. })
  105. .end()
  106. config.when(process.env.NODE_ENV === 'development', config =>
  107. config.devtool('cheap-source-map')
  108. )
  109. config.when(process.env.NODE_ENV !== 'development', config => {
  110. // config
  111. // .plugin('ScriptExtHtmlWebpackPlugin')
  112. // .after('html')
  113. // .use('script-ext-html-webpack-plugin', [{
  114. // // `runtime` must same as runtimeChunk name. default is `runtime`
  115. // inline: /runtime\..*\.js$/
  116. // }])
  117. // .end()
  118. config.optimization.splitChunks({
  119. chunks: 'all',
  120. cacheGroups: {
  121. libs: {
  122. name: 'chunk-libs',
  123. test: /[\\/]node_modules[\\/]/,
  124. priority: 10,
  125. chunks: 'initial' // only package third parties that are initially dependent
  126. },
  127. elementUI: {
  128. name: 'chunk-elementUI', // split elementUI into a single package
  129. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  130. test: /[\\/]node_modules[\\/]element-ui[\\/]/
  131. },
  132. commons: {
  133. name: 'chunk-commons',
  134. test: resolve('src/components'), // can customize your rules
  135. minChunks: 3, // minimum common number
  136. priority: 5,
  137. reuseExistingChunk: true
  138. }
  139. }
  140. })
  141. config.optimization.runtimeChunk('single')
  142. })
  143. }
  144. }