vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. // const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || 'vue Admin Template' // page title
  9. // If your port is set to 80,
  10. // use administrator privileges to execute the command line.
  11. // For example, Mac: sudo npm run
  12. const port = 9528 // dev port
  13. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  14. module.exports = {
  15. /**
  16. * You will need to set publicPath if you plan to deploy your site under a sub path,
  17. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  18. * then publicPath should be set to "/bar/".
  19. * In most cases please use '/' !!!
  20. * Detail: https://cli.vuejs.org/config/#publicpath
  21. */
  22. publicPath: '/',
  23. outputDir: 'dist',
  24. assetsDir: 'static',
  25. lintOnSave: process.env.NODE_ENV === 'development',
  26. productionSourceMap: false,
  27. devServer: {
  28. port: port,
  29. open: true,
  30. overlay: {
  31. warnings: false,
  32. errors: true
  33. },
  34. disableHostCheck: true, // webpack4.0 开启热更新
  35. proxy: {
  36. // change xxx-api/login => mock/login
  37. // detail: https://cli.vuejs.org/config/#devserver-proxy
  38. [process.env.VUE_APP_BASE_API]: {
  39. target: `http://127.0.0.1:${port}/mock`,
  40. changeOrigin: true,
  41. pathRewrite: {
  42. ['^' + process.env.VUE_APP_BASE_API]: ''
  43. }
  44. }
  45. }
  46. // after: require('./mock/mock-server.js')
  47. },
  48. configureWebpack: {
  49. // provide the app's title in webpack's name field, so that
  50. // it can be accessed in index.html to inject the correct title.
  51. name: name,
  52. resolve: {
  53. alias: {
  54. '@': resolve('src')
  55. }
  56. }
  57. // plugins: [
  58. // new CompressionWebpackPlugin({ // 构建压缩加速
  59. // test: /\.js$|\.html$|\.css/
  60. // })
  61. // ]
  62. },
  63. chainWebpack(config) {
  64. config.plugins.delete('preload') // TODO: need test
  65. config.plugins.delete('prefetch') // TODO: need test
  66. // set svg-sprite-loader
  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. // set preserveWhitespace
  83. config.module
  84. .rule('vue')
  85. .use('vue-loader')
  86. .loader('vue-loader')
  87. .tap(options => {
  88. options.compilerOptions.preserveWhitespace = true
  89. return options
  90. })
  91. .end()
  92. config
  93. // https://webpack.js.org/configuration/devtool/#development
  94. .when(process.env.NODE_ENV === 'development',
  95. config => config.devtool('cheap-source-map')
  96. )
  97. config
  98. .when(process.env.NODE_ENV !== 'development',
  99. config => {
  100. config
  101. .plugin('ScriptExtHtmlWebpackPlugin')
  102. .after('html')
  103. .use('script-ext-html-webpack-plugin', [{
  104. // `runtime` must same as runtimeChunk name. default is `runtime`
  105. inline: /runtime\..*\.js$/
  106. }])
  107. .end()
  108. config
  109. .optimization.splitChunks({
  110. chunks: 'all',
  111. cacheGroups: {
  112. styles: {
  113. name: 'styles',
  114. test: /\.css$/,
  115. chunks: 'all',
  116. enforce: true // 忽略splitChunks的其他配置,强制将匹配到的缓存组中的文件合并为一个styles.css文件
  117. },
  118. libs: {
  119. name: 'chunk-libs',
  120. test: /[\\/]node_modules[\\/]/,
  121. priority: -10,
  122. chunks: 'initial' // only package third parties that are initially dependent
  123. }
  124. // elementUI: {
  125. // name: 'chunk-elementUI', // split elementUI into a single package
  126. // priority: 15, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  127. // test: /[\\/]node_modules[\\/]element-ui[\\/]/, // in order to adapt to cnpm
  128. // chunks: 'all',
  129. // reuseExistingChunk: true,
  130. // enforce: true
  131. // },
  132. // echarts: {
  133. // name: 'chunk-echarts',
  134. // test: /[\\/]node_modules[\\/]echarts[\\/]/,
  135. // chunks: 'all',
  136. // priority: 10,
  137. // reuseExistingChunk: true,
  138. // enforce: true
  139. // },
  140. // demo: {
  141. // name: 'chunk-demo',
  142. // test: /[\\/]src[\\/]views[\\/]/,
  143. // chunks: 'all',
  144. // priority: 20,
  145. // reuseExistingChunk: true,
  146. // enforce: true
  147. // }
  148. }
  149. })
  150. config.optimization.runtimeChunk('single')
  151. }
  152. )
  153. }
  154. }