vue.config.js 3.9 KB

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