vue.config.js 4.5 KB

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