vue.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. const port = 9528 // dev port
  9. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  10. module.exports = {
  11. /**
  12. * You will need to set publicPath 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 publicPath 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',
  22. productionSourceMap: false,
  23. devServer: {
  24. port: port,
  25. open: true,
  26. overlay: {
  27. warnings: false,
  28. errors: true
  29. },
  30. proxy: {
  31. // change xxx-api/login => mock/login
  32. // detail: https://cli.vuejs.org/config/#devserver-proxy
  33. [process.env.VUE_APP_BASE_API]: {
  34. target: `http://localhost:${port}/mock`,
  35. changeOrigin: true,
  36. pathRewrite: {
  37. ['^' + process.env.VUE_APP_BASE_API]: ''
  38. }
  39. }
  40. },
  41. after: require('./mock/mock-server.js')
  42. },
  43. configureWebpack: {
  44. // provide the app's title in webpack's name field, so that
  45. // it can be accessed in index.html to inject the correct title.
  46. name: name,
  47. resolve: {
  48. alias: {
  49. '@': resolve('src')
  50. }
  51. },
  52. externals: {
  53. vue: 'Vue',
  54. 'element-ui': 'ELEMENT'
  55. }
  56. },
  57. chainWebpack(config) {
  58. const cdn = {
  59. css: [
  60. // element-ui css
  61. 'https://unpkg.com/element-ui/lib/theme-chalk/index.css'
  62. ],
  63. js: [
  64. // vue must at first!
  65. 'https://unpkg.com/vue/dist/vue.js',
  66. // element-ui js
  67. 'https://unpkg.com/element-ui/lib/index.js'
  68. ]
  69. }
  70. config.plugin('html')
  71. .tap(args => {
  72. args[0].cdn = cdn
  73. return args
  74. })
  75. config.plugins.delete('preload') // TODO: need test
  76. config.plugins.delete('prefetch') // TODO: need test
  77. // set svg-sprite-loader
  78. config.module
  79. .rule('svg')
  80. .exclude.add(resolve('src/icons'))
  81. .end()
  82. config.module
  83. .rule('icons')
  84. .test(/\.svg$/)
  85. .include.add(resolve('src/icons'))
  86. .end()
  87. .use('svg-sprite-loader')
  88. .loader('svg-sprite-loader')
  89. .options({
  90. symbolId: 'icon-[name]'
  91. })
  92. .end()
  93. // set preserveWhitespace
  94. config.module
  95. .rule('vue')
  96. .use('vue-loader')
  97. .loader('vue-loader')
  98. .tap(options => {
  99. options.compilerOptions.preserveWhitespace = true
  100. return options
  101. })
  102. .end()
  103. config
  104. // https://webpack.js.org/configuration/devtool/#development
  105. .when(process.env.NODE_ENV === 'development',
  106. config => config.devtool('cheap-source-map')
  107. )
  108. config
  109. .when(process.env.NODE_ENV !== 'development',
  110. config => {
  111. config
  112. .plugin('ScriptExtHtmlWebpackPlugin')
  113. .after('html')
  114. .use('script-ext-html-webpack-plugin', [{
  115. // `runtime` must same as runtimeChunk name. default is `runtime`
  116. inline: /runtime\..*\.js$/
  117. }])
  118. .end()
  119. config
  120. .optimization.splitChunks({
  121. chunks: 'all',
  122. cacheGroups: {
  123. libs: {
  124. name: 'chunk-libs',
  125. test: /[\\/]node_modules[\\/]/,
  126. priority: 10,
  127. chunks: 'initial' // only package third parties that are initially dependent
  128. },
  129. elementUI: {
  130. name: 'chunk-elementUI', // split elementUI into a single package
  131. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  132. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  133. },
  134. commons: {
  135. name: 'chunk-commons',
  136. test: resolve('src/components'), // can customize your rules
  137. minChunks: 3, // minimum common number
  138. priority: 5,
  139. reuseExistingChunk: true
  140. }
  141. }
  142. })
  143. config.optimization.runtimeChunk('single')
  144. }
  145. )
  146. }
  147. }