vue.config.js 4.1 KB

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