vue.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // console.log(module.exports.devServer.proxy)
  15. console.log('# NODE_ENV:', process.env.NODE_ENV)
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: './',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. // lintOnSave: process.env.NODE_ENV === 'development', // 否使用eslint
  29. lintOnSave: false, // 否使用eslint
  30. productionSourceMap: false, // 如果您不需要生产时的源映射,那么将此设置为false可以加速生产构建
  31. devServer: {
  32. port: port,
  33. open: false, // 配置自动启动浏览器
  34. overlay: {
  35. warnings: true,
  36. errors: true
  37. },
  38. // before: require('./mock/mock-server.js'),
  39. proxy: {
  40. // 登陆单独代理
  41. '/v1/user/login': {
  42. target: process.env.VUE_APP_LOGIN_URL,
  43. changeOrigin: true,
  44. logLevel: 'debug',
  45. secure: true,
  46. },
  47. [process.env.VUE_APP_BASE_API]: { // 这里是公共部分,在调用接口时后面接不相同的部分,/api就相当于http://192.168.0.199:8926/api这一段
  48. target: process.env.VUE_APP_BASE_URL, // 这里写的是访问接口的域名和端口号
  49. changeOrigin: true, // 必须加上这个才能跨域请求
  50. logLevel: 'debug',
  51. pathRewrite: { // 重命名
  52. ['^' + process.env.VUE_APP_BASE_API]: ''
  53. }
  54. }
  55. }
  56. },
  57. configureWebpack: {
  58. // provide the app's title in webpack's name field, so that
  59. // it can be accessed in index.html to inject the correct title.
  60. name: name,
  61. resolve: {
  62. alias: {
  63. '@': resolve('src')
  64. }
  65. }
  66. },
  67. chainWebpack(config) {
  68. // it can improve the speed of the first screen, it is recommended to turn on preload
  69. // it can improve the speed of the first screen, it is recommended to turn on preload
  70. config.plugin('preload').tap(() => [
  71. {
  72. rel: 'preload',
  73. // to ignore runtime.js
  74. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  75. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  76. include: 'initial'
  77. }
  78. ])
  79. // when there are many pages, it will cause too many meaningless requests
  80. config.plugins.delete('prefetch')
  81. // set svg-sprite-loader
  82. config.module
  83. .rule('svg')
  84. .exclude.add(resolve('src/icons'))
  85. .end()
  86. config.module
  87. .rule('icons')
  88. .test(/\.svg$/)
  89. .include.add(resolve('src/icons'))
  90. .end()
  91. .use('svg-sprite-loader')
  92. .loader('svg-sprite-loader')
  93. .options({
  94. symbolId: 'icon-[name]'
  95. })
  96. .end()
  97. config
  98. .when(process.env.NODE_ENV !== 'development',
  99. config => {
  100. config
  101. .plugin('ScriptExtHtmlWebpackPlugin')
  102. .after('html')
  103. .use('script-ext-html-webpack-plugin', [{
  104. // `runtime` must same as runtimeChunk name. default is `runtime`
  105. inline: /runtime\..*\.js$/
  106. }])
  107. .end()
  108. config
  109. .optimization.splitChunks({
  110. chunks: 'all',
  111. cacheGroups: {
  112. libs: {
  113. name: 'chunk-libs',
  114. test: /[\\/]node_modules[\\/]/,
  115. priority: 10,
  116. chunks: 'initial' // only package third parties that are initially dependent
  117. },
  118. elementUI: {
  119. name: 'chunk-elementUI', // split elementUI into a single package
  120. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  121. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  122. },
  123. commons: {
  124. name: 'chunk-commons',
  125. test: resolve('src/components'), // can customize your rules
  126. minChunks: 3, // minimum common number
  127. priority: 5,
  128. reuseExistingChunk: true
  129. }
  130. }
  131. })
  132. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  133. config.optimization.runtimeChunk('single')
  134. }
  135. )
  136. }
  137. }