vue.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = 9529 // dev port
  12. const port = 8082 // 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. allowedHosts: ['tmrwatch.cn'],
  29. port: port,
  30. open: true,
  31. overlay: {
  32. warnings: false,
  33. errors: true
  34. },
  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. '/authdata': {
  46. target: 'http://tmrwatch.cn:8082',
  47. changeOrigin: true, // 改变请求头中的host为目标服务器
  48. pathRewrite: { '^/authdata': '/authdata' }, // 路径重写
  49. },
  50. },
  51. after: require('./mock/mock-server.js')
  52. },
  53. configureWebpack: {
  54. // provide the app's title in webpack's name field, so that
  55. // it can be accessed in index.html to inject the correct title.
  56. devtool: 'source-map',
  57. name: name,
  58. resolve: {
  59. alias: {
  60. '@': resolve('src')
  61. }
  62. }
  63. },
  64. chainWebpack(config) {
  65. config.plugins.delete('preload') // TODO: need test
  66. config.plugins.delete('prefetch') // TODO: need test
  67. // set svg-sprite-loader
  68. config.module
  69. .rule('svg')
  70. .exclude.add(resolve('src/icons'))
  71. .end()
  72. config.module
  73. .rule('icons')
  74. .test(/\.svg$/)
  75. .include.add(resolve('src/icons'))
  76. .end()
  77. .use('svg-sprite-loader')
  78. .loader('svg-sprite-loader')
  79. .options({
  80. symbolId: 'icon-[name]'
  81. })
  82. .end()
  83. // set preserveWhitespace
  84. config.module
  85. .rule('vue')
  86. .use('vue-loader')
  87. .loader('vue-loader')
  88. .tap(options => {
  89. options.compilerOptions.preserveWhitespace = true
  90. return options
  91. })
  92. .end()
  93. config
  94. // https://webpack.js.org/configuration/devtool/#development
  95. .when(process.env.NODE_ENV === 'development',
  96. config => config.devtool('cheap-source-map')
  97. )
  98. config
  99. .when(process.env.NODE_ENV !== 'development',
  100. config => {
  101. config
  102. .plugin('ScriptExtHtmlWebpackPlugin')
  103. .after('html')
  104. .use('script-ext-html-webpack-plugin', [{
  105. // `runtime` must same as runtimeChunk name. default is `runtime`
  106. inline: /runtime\..*\.js$/
  107. }])
  108. .end()
  109. config
  110. .optimization.splitChunks({
  111. chunks: 'all',
  112. cacheGroups: {
  113. libs: {
  114. name: 'chunk-libs',
  115. test: /[\\/]node_modules[\\/]/,
  116. priority: 10,
  117. chunks: 'initial' // only package third parties that are initially dependent
  118. },
  119. elementUI: {
  120. name: 'chunk-elementUI', // split elementUI into a single package
  121. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  122. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  123. },
  124. commons: {
  125. name: 'chunk-commons',
  126. test: resolve('src/components'), // can customize your rules
  127. minChunks: 3, // minimum common number
  128. priority: 5,
  129. reuseExistingChunk: true
  130. }
  131. }
  132. })
  133. config.optimization.runtimeChunk('single')
  134. }
  135. )
  136. }
  137. }