vue.config.js 4.0 KB

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