vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Admin Template' // 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 methods:
  12. // port = 9528 npm run dev OR npm run dev --port = 9528
  13. const port = process.env.port || process.env.npm_config_port || 9528 // 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. proxy: {
  30. '/api': { // /proxy_url 这个用来和根路径 baseURL 进行匹配
  31. target: 'https://xxx.cwqbb.com/', // 这个是填写跨域的请求域名+端口号,也就是要请求的URL(不包含URL路径)
  32. changeOrigin: true, // 是否允许跨域请求,在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  33. pathRewrite: { // 路径重写
  34. '^/api': '/' // 替换target中的请求地址,原请求为 http://127.0.0.1:8000/kuayu 实际请求为 http://127.0.0.1:8000/proxy_url/kuayu
  35. }
  36. }
  37. }
  38. },
  39. // devServer: {
  40. // port: port,
  41. // open: true,
  42. // proxy: window.global.baseURL
  43. // },
  44. // devServer: {
  45. // port: port,
  46. // open: true,
  47. // overlay: {
  48. // warnings: false,
  49. // errors: true
  50. // },
  51. // before: require('./mock/mock-server.js')
  52. // },
  53. configureWebpack: {
  54. // provide the app's title in webpack's name field, so that
  55. // it can be accessed in index.html to inject the correct title.
  56. name: name,
  57. resolve: {
  58. alias: {
  59. '@': resolve('src')
  60. }
  61. }
  62. },
  63. chainWebpack(config) {
  64. // it can improve the speed of the first screen, it is recommended to turn on preload
  65. config.plugin('preload').tap(() => [
  66. {
  67. rel: 'preload',
  68. // to ignore runtime.js
  69. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  70. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  71. include: 'initial'
  72. }
  73. ])
  74. // when there are many pages, it will cause too many meaningless requests
  75. config.plugins.delete('prefetch')
  76. // set svg-sprite-loader
  77. config.module
  78. .rule('svg')
  79. .exclude.add(resolve('src/icons'))
  80. .end()
  81. config.module
  82. .rule('icons')
  83. .test(/\.svg$/)
  84. .include.add(resolve('src/icons'))
  85. .end()
  86. .use('svg-sprite-loader')
  87. .loader('svg-sprite-loader')
  88. .options({
  89. symbolId: 'icon-[name]'
  90. })
  91. .end()
  92. config
  93. .when(process.env.NODE_ENV !== 'development',
  94. config => {
  95. config
  96. .plugin('ScriptExtHtmlWebpackPlugin')
  97. .after('html')
  98. .use('script-ext-html-webpack-plugin', [{
  99. // `runtime` must same as runtimeChunk name. default is `runtime`
  100. inline: /runtime\..*\.js$/
  101. }])
  102. .end()
  103. config
  104. .optimization.splitChunks({
  105. chunks: 'all',
  106. cacheGroups: {
  107. libs: {
  108. name: 'chunk-libs',
  109. test: /[\\/]node_modules[\\/]/,
  110. priority: 10,
  111. chunks: 'initial' // only package third parties that are initially dependent
  112. },
  113. elementUI: {
  114. name: 'chunk-elementUI', // split elementUI into a single package
  115. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  116. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  117. },
  118. commons: {
  119. name: 'chunk-commons',
  120. test: resolve('src/components'), // can customize your rules
  121. minChunks: 3, // minimum common number
  122. priority: 5,
  123. reuseExistingChunk: true
  124. }
  125. }
  126. })
  127. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  128. config.optimization.runtimeChunk('single')
  129. }
  130. )
  131. }
  132. }