vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const env = ['develop', undefined].includes(process.env.BUILD_ENV) ? require('./config/dev.env') : require('./config/prod.env')
  5. function resolve (dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || '管理后台' // page title
  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: '/',
  19. outputDir: 'dist',
  20. assetsDir: 'static',
  21. // lintOnSave: process.env.NODE_ENV === 'development',
  22. productionSourceMap: process.env.BUILD_ENV === 'develop',
  23. devServer: {
  24. port: 8080,
  25. open: true,
  26. host: '0.0.0.0',
  27. overlay: {
  28. warnings: false,
  29. errors: true
  30. },
  31. // before: require('./mock/mock-server.js')
  32. proxy: {
  33. '/api/auth': {
  34. target: 'http://swago-admin.codedreamit.com',
  35. changeOrigin: true,
  36. pathRewrite: {
  37. '^/api/auth': '/api/auth'
  38. }
  39. },
  40. '/api/admin': {
  41. target: 'http://swago-admin.codedreamit.com',
  42. changeOrigin: true,
  43. pathRewrite: {
  44. '^/api/admin': '/api/admin'
  45. }
  46. }
  47. }
  48. },
  49. configureWebpack: {
  50. // provide the app's title in webpack's name field, so that
  51. // it can be accessed in index.html to inject the correct title.
  52. name: name,
  53. resolve: {
  54. alias: {
  55. '@': resolve('src')
  56. }
  57. }
  58. },
  59. css: {
  60. loaderOptions: {
  61. sass: {
  62. prependData: `@import "@/styles/var.scss";`
  63. }
  64. }
  65. },
  66. chainWebpack (config) {
  67. // it can improve the speed of the first screen, it is recommended to turn on preload
  68. config.plugin('preload').tap(() => [
  69. {
  70. rel: 'preload',
  71. // to ignore runtime.js
  72. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  73. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  74. include: 'initial'
  75. }
  76. ])
  77. config.plugin('define').tap(args => {
  78. args[0]['process.env'] = Object.assign({}, args[0]['process.env'], env)
  79. return args
  80. })
  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. config
  100. .when(process.env.NODE_ENV !== 'development',
  101. config => {
  102. // config
  103. // .plugin('ScriptExtHtmlWebpackPlugin')
  104. // .after('html')
  105. // .use('script-ext-html-webpack-plugin', [{
  106. // // `runtime` must same as runtimeChunk name. default is `runtime`
  107. // inline: /runtime\..*\.js$/
  108. // }])
  109. // .end()
  110. config
  111. .optimization.splitChunks({
  112. chunks: 'all',
  113. cacheGroups: {
  114. libs: {
  115. name: 'chunk-libs',
  116. test: /[\\/]node_modules[\\/]/,
  117. priority: 10,
  118. chunks: 'initial' // only package third parties that are initially dependent
  119. },
  120. elementUI: {
  121. name: 'chunk-elementUI', // split elementUI into a single package
  122. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  123. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  124. },
  125. commons: {
  126. name: 'chunk-commons',
  127. test: resolve('src/components'), // can customize your rules
  128. minChunks: 3, // minimum common number
  129. priority: 5,
  130. reuseExistingChunk: true
  131. }
  132. }
  133. })
  134. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  135. config.optimization.runtimeChunk('single')
  136. }
  137. )
  138. }
  139. }