vue.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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': {
  34. target: 'http://shining-admin.codedreamit.com',
  35. changeOrigin: true,
  36. pathRewrite: {
  37. '^/api': '/api'
  38. }
  39. }
  40. }
  41. },
  42. configureWebpack: {
  43. // provide the app's title in webpack's name field, so that
  44. // it can be accessed in index.html to inject the correct title.
  45. name: name,
  46. resolve: {
  47. alias: {
  48. '@': resolve('src')
  49. }
  50. }
  51. },
  52. css: {
  53. loaderOptions: {
  54. sass: {
  55. prependData: `@import "@/styles/var.scss";`
  56. }
  57. }
  58. },
  59. chainWebpack(config) {
  60. // it can improve the speed of the first screen, it is recommended to turn on preload
  61. config.plugin('preload').tap(() => [
  62. {
  63. rel: 'preload',
  64. // to ignore runtime.js
  65. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  66. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  67. include: 'initial'
  68. }
  69. ])
  70. config.plugin('define').tap(args => {
  71. args[0]['process.env'] = Object.assign({}, args[0]['process.env'], env)
  72. return args
  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. }