vue.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const port = 9528 // dev port
  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: false,
  23. devServer: {
  24. port: port,
  25. open: true,
  26. overlay: {
  27. warnings: false,
  28. errors: true
  29. },
  30. proxy: {
  31. // change xxx-api/login => mock/login
  32. // detail: https://cli.vuejs.org/config/#devserver-proxy
  33. [process.env.VUE_APP_BASE_API]: {
  34. target: `http://localhost:${port}/mock`,
  35. changeOrigin: true,
  36. pathRewrite: {
  37. ['^' + process.env.VUE_APP_BASE_API]: ''
  38. }
  39. }
  40. },
  41. after: require('./mock/mock-server.js')
  42. },
  43. configureWebpack: {
  44. // provide the app's title in webpack's name field, so that
  45. // it can be accessed in index.html to inject the correct title.
  46. name: name,
  47. resolve: {
  48. alias: {
  49. '@': resolve('src')
  50. }
  51. }
  52. },
  53. chainWebpack(config) {
  54. config.plugins.delete('preload') // TODO: need test
  55. config.plugins.delete('prefetch') // TODO: need test
  56. // set svg-sprite-loader
  57. config.module
  58. .rule('svg')
  59. .exclude.add(resolve('src/icons'))
  60. .end()
  61. config.module
  62. .rule('icons')
  63. .test(/\.svg$/)
  64. .include.add(resolve('src/icons'))
  65. .end()
  66. .use('svg-sprite-loader')
  67. .loader('svg-sprite-loader')
  68. .options({
  69. symbolId: 'icon-[name]'
  70. })
  71. .end()
  72. // set preserveWhitespace
  73. config.module
  74. .rule('vue')
  75. .use('vue-loader')
  76. .loader('vue-loader')
  77. .tap(options => {
  78. options.compilerOptions.preserveWhitespace = true
  79. return options
  80. })
  81. .end()
  82. config
  83. // https://webpack.js.org/configuration/devtool/#development
  84. .when(process.env.NODE_ENV === 'development',
  85. // config => config.devtool('cheap-source-map')
  86. config => config.devtool('source-map')
  87. )
  88. config
  89. .when(process.env.NODE_ENV !== 'development',
  90. config => {
  91. config
  92. .plugin('ScriptExtHtmlWebpackPlugin')
  93. .after('html')
  94. .use('script-ext-html-webpack-plugin', [{
  95. // `runtime` must same as runtimeChunk name. default is `runtime`
  96. inline: /runtime\..*\.js$/
  97. }])
  98. .end()
  99. config
  100. .optimization.splitChunks({
  101. chunks: 'all',
  102. cacheGroups: {
  103. libs: {
  104. name: 'chunk-libs',
  105. test: /[\\/]node_modules[\\/]/,
  106. priority: 10,
  107. chunks: 'initial' // only package third parties that are initially dependent
  108. },
  109. elementUI: {
  110. name: 'chunk-elementUI', // split elementUI into a single package
  111. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  112. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  113. },
  114. commons: {
  115. name: 'chunk-commons',
  116. test: resolve('src/components'), // can customize your rules
  117. minChunks: 3, // minimum common number
  118. priority: 5,
  119. reuseExistingChunk: true
  120. }
  121. }
  122. })
  123. config.optimization.runtimeChunk('single')
  124. }
  125. )
  126. }
  127. }