vue.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict'
  2. const path = require('path')
  3. function resolve(dir) {
  4. return path.join(__dirname, dir)
  5. }
  6. const name = 'vue-element-admin'
  7. const port = 9527 // dev port
  8. // Explanation of each configuration item You can find it in https://cli.vuejs.org/config/
  9. module.exports = {
  10. /**
  11. * You can set by yourself according to actual condition
  12. * You will need to set this 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 assetsPublicPath 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' ? 'error' : false,
  22. productionSourceMap: false,
  23. devServer: {
  24. port: port,
  25. open: true,
  26. overlay: {
  27. warnings: false,
  28. errors: true
  29. },
  30. proxy: {
  31. '/api': {
  32. target: `http://localhost:${port}/mock`,
  33. changeOrigin: true,
  34. pathRewrite: {
  35. '^/api': ''
  36. }
  37. }
  38. },
  39. after(app) {
  40. require('@babel/register')
  41. const bodyParser = require('body-parser')
  42. // parse app.body
  43. // http://expressjs.com/en/4x/api.html#req.body
  44. app.use(bodyParser.json())
  45. app.use(bodyParser.urlencoded({
  46. extended: true
  47. }))
  48. const { default: mocks } = require('./mock')
  49. for (const mock of mocks) {
  50. app[mock.type](mock.url, mock.response)
  51. }
  52. }
  53. },
  54. configureWebpack: {
  55. // We provide the app's title in Webpack's name field, so that
  56. // it can be accessed in index.html to inject the correct title.
  57. name: name,
  58. resolve: {
  59. alias: {
  60. '@': resolve('src')
  61. }
  62. }
  63. },
  64. chainWebpack(config) {
  65. config.plugins.delete('preload') // TODO: need test
  66. config.plugins.delete('prefetch') // TODO: need test
  67. config.module
  68. .rule('svg')
  69. .exclude.add(resolve('src/icons'))
  70. .end()
  71. config.module
  72. .rule('icons')
  73. .test(/\.svg$/)
  74. .include.add(resolve('src/icons'))
  75. .end()
  76. .use('svg-sprite-loader')
  77. .loader('svg-sprite-loader')
  78. .options({
  79. symbolId: 'icon-[name]'
  80. })
  81. .end()
  82. config.module
  83. .rule('vue')
  84. .use('vue-loader')
  85. .loader('vue-loader')
  86. .tap(options => {
  87. options.compilerOptions.preserveWhitespace = true
  88. return options
  89. })
  90. .end()
  91. config
  92. .when(process.env.NODE_ENV === 'development',
  93. config => config.devtool('cheap-source-map')
  94. )
  95. config
  96. .when(process.env.NODE_ENV !== 'development',
  97. config => {
  98. config
  99. // .plugin('ScriptExtHtmlWebpackPlugin')
  100. // .after('html')
  101. // .use('script-ext-html-webpack-plugin', [{
  102. // // `runtime` must same as runtimeChunk name. default is `runtime`
  103. // inline: /runtime\..*\.js$/
  104. // }])
  105. // .end()
  106. config
  107. .optimization.splitChunks({
  108. chunks: 'all',
  109. cacheGroups: {
  110. libs: {
  111. name: 'chunk-libs',
  112. test: /[\\/]node_modules[\\/]/,
  113. priority: 10,
  114. chunks: 'initial' // 只打包初始时依赖的第三方
  115. },
  116. elementUI: {
  117. name: 'chunk-elementUI', // 单独将 elementUI 拆包
  118. priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
  119. test: /[\\/]node_modules[\\/]element-ui[\\/]/
  120. },
  121. commons: {
  122. name: 'chunk-commons',
  123. test: resolve('src/components'), // 可自定义拓展你的规则
  124. minChunks: 3, // 最小公用次数
  125. priority: 5,
  126. reuseExistingChunk: true
  127. }
  128. }
  129. })
  130. config.optimization.runtimeChunk('single')
  131. }
  132. )
  133. }
  134. }