You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vue.config.js 6.5 KiB

2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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: false,
  31. overlay: {
  32. warnings: false,
  33. errors: true
  34. },
  35. // allowedHosts: ['192.168.0.104:8080', '192.168.0.104:8081'],
  36. // headers: {
  37. // 'Access-Control-Allow-Origin': '192.168.0.104:8080, 192.168.0.104:8081',
  38. // 'Access-Control-Allow-Headers': '*',
  39. // 'Access-Control-Allow-Methods': '*',
  40. // 'Access-Control-Allow-Credentials': 'true'
  41. // },
  42. // before: require('./mock/mock-server.js'),
  43. // 这里写入需要代理的api和对应的目标地址
  44. proxy: {
  45. '/api': {
  46. target: 'http://localhost:8080',
  47. // target: 'http://192.168.0.198:8080',
  48. // target: 'http://192.168.0.127:8080',
  49. // target: 'http://192.168.0.165:8080',
  50. // target: 'http://192.168.43.105:8080',
  51. // target: 'http://127.0.0.1:8080',
  52. // target: 'http://192.168.0.70:8080',
  53. // target: 'http://192.168.0.199:8080',
  54. ws: true,
  55. changeOrigin: true,
  56. pathRewrite: {
  57. '^/portapi': ''
  58. }
  59. // 使用了proxy 就需要开启下面的选项 (http-proxy-middleware会将post请求给parse了 所以需要再次进行string化)
  60. // onProxyReq: (proxyReq, req, res, options) => {
  61. // if (req.body) {
  62. // const bodyData = JSON.stringify(req.body)
  63. // // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
  64. // proxyReq.setHeader('Content-Type', 'application/json')
  65. // proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
  66. // // stream the content
  67. // proxyReq.write(bodyData)
  68. // }
  69. // }
  70. },
  71. '/spc': {
  72. // target: 'http://192.168.0.180:8080',
  73. target: 'http://58.210.206.230:8000',
  74. // target: 'http://192.168.0.187:8080',
  75. // target: 'http://192.168.43.105:8080',
  76. // target: 'http://127.0.0.1:8080',
  77. ws: true,
  78. changeOrigin: true,
  79. pathRewrite: {
  80. '^/spc': ''
  81. }
  82. // 使用了proxy 就需要开启下面的选项 (http-proxy-middleware会将post请求给parse了 所以需要再次进行string化)
  83. // onProxyReq: (proxyReq, req, res, options) => {
  84. // if (req.body) {
  85. // const bodyData = JSON.stringify(req.body)
  86. // // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
  87. // proxyReq.setHeader('Content-Type', 'application/json')
  88. // proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
  89. // // stream the content
  90. // proxyReq.write(bodyData)
  91. // }
  92. // }
  93. }
  94. }
  95. },
  96. configureWebpack: {
  97. // provide the app's title in webpack's name field, so that
  98. // it can be accessed in index.html to inject the correct title.
  99. name: name,
  100. resolve: {
  101. alias: {
  102. '@': resolve('src')
  103. }
  104. }
  105. },
  106. css: {
  107. loaderOptions: {
  108. // vue cli默认使用node-sass作为sass预处理器,使用下面的选项可以使用sass包作为预处理器
  109. // 用于解决node-sass需要从外网下载依赖的卡顿问题
  110. sass: {
  111. implementation: require('sass')
  112. }
  113. }
  114. },
  115. chainWebpack(config) {
  116. config.plugins.delete('preload') // TODO: need test
  117. config.plugins.delete('prefetch') // TODO: need test
  118. // set svg-sprite-loader
  119. config.module
  120. .rule('svg')
  121. .exclude.add(resolve('src/icons'))
  122. .end()
  123. config.module
  124. .rule('icons')
  125. .test(/\.svg$/)
  126. .include.add(resolve('src/icons'))
  127. .end()
  128. .use('svg-sprite-loader')
  129. .loader('svg-sprite-loader')
  130. .options({
  131. symbolId: 'icon-[name]'
  132. })
  133. .end()
  134. // set preserveWhitespace
  135. config.module
  136. .rule('vue')
  137. .use('vue-loader')
  138. .loader('vue-loader')
  139. .tap(options => {
  140. options.compilerOptions.preserveWhitespace = true
  141. return options
  142. })
  143. .end()
  144. config
  145. .when(process.env.NODE_ENV !== 'development',
  146. config => {
  147. config
  148. .plugin('ScriptExtHtmlWebpackPlugin')
  149. .after('html')
  150. .use('script-ext-html-webpack-plugin', [{
  151. // `runtime` must same as runtimeChunk name. default is `runtime`
  152. inline: /runtime\..*\.js$/
  153. }])
  154. .end()
  155. config
  156. .optimization.splitChunks({
  157. chunks: 'all',
  158. cacheGroups: {
  159. libs: {
  160. name: 'chunk-libs',
  161. test: /[\\/]node_modules[\\/]/,
  162. priority: 10,
  163. chunks: 'initial' // only package third parties that are initially dependent
  164. },
  165. elementUI: {
  166. name: 'chunk-elementUI', // split elementUI into a single package
  167. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  168. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  169. },
  170. commons: {
  171. name: 'chunk-commons',
  172. test: resolve('src/components'), // can customize your rules
  173. minChunks: 3, // minimum common number
  174. priority: 5,
  175. reuseExistingChunk: true
  176. }
  177. }
  178. })
  179. config.optimization.runtimeChunk('single')
  180. }
  181. )
  182. }
  183. }