vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = 9528 // 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. disableHostCheck: true, // webpack4.0 开启热更新
  34. proxy: {
  35. // change xxx-api/login => mock/login
  36. // detail: https://cli.vuejs.org/config/#devserver-proxy
  37. [process.env.VUE_APP_BASE_API]: {
  38. target: `http://127.0.0.1:${port}/mock`,
  39. changeOrigin: true,
  40. pathRewrite: {
  41. ['^' + process.env.VUE_APP_BASE_API]: ''
  42. }
  43. }
  44. }
  45. // after: require('./mock/mock-server.js')
  46. },
  47. configureWebpack: {
  48. // provide the app's title in webpack's name field, so that
  49. // it can be accessed in index.html to inject the correct title.
  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: 15, // 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. chunks: 'all',
  117. reuseExistingChunk: true,
  118. enforce: true
  119. },
  120. echarts: {
  121. name: 'chunk-echarts',
  122. test: /[\\/]node_modules[\\/]echarts[\\/]/,
  123. chunks: 'all',
  124. priority: 10,
  125. reuseExistingChunk: true,
  126. enforce: true
  127. },
  128. demo: {
  129. name: 'chunk-demo',
  130. test: /[\\/]src[\\/]views[\\/]/,
  131. chunks: 'all',
  132. priority: 20,
  133. reuseExistingChunk: true,
  134. enforce: true
  135. }
  136. }
  137. })
  138. config.optimization.runtimeChunk('single')
  139. }
  140. )
  141. }
  142. }