vue.config.js 4.9 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 cdn = {
  9. css: {
  10. dev: [
  11. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  12. `./static/iconfont/iconfont.css`
  13. ],
  14. ship: [
  15. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  16. `./static/iconfont/iconfont.css`
  17. ],
  18. product: [
  19. `/cdn/winseaview/index.css`,
  20. "/cdn/element-ui/2.13.2/theme-chalk/index.css",
  21. `./static/iconfont/iconfont.css`
  22. ]
  23. }
  24. }
  25. // If your port is set to 80,
  26. // use administrator privileges to execute the command line.
  27. // For example, Mac: sudo npm run
  28. // You can change the port by the following methods:
  29. // port = 9528 npm run dev OR npm run dev --port = 9528
  30. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  31. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  32. module.exports = {
  33. /**
  34. * You will need to set publicPath if you plan to deploy your site under a sub path,
  35. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  36. * then publicPath should be set to "/bar/".
  37. * In most cases please use '/' !!!
  38. * Detail: https://cli.vuejs.org/config/#publicpath
  39. */
  40. publicPath: '/',
  41. outputDir: 'dist',
  42. assetsDir: 'static',
  43. lintOnSave: process.env.NODE_ENV === 'development',
  44. productionSourceMap: false,
  45. devServer: {
  46. port: port,
  47. disableHostCheck: true,
  48. open: false,
  49. overlay: {
  50. warnings: false,
  51. errors: true
  52. },
  53. proxy: {
  54. '/pb': {
  55. target: 'http://192.168.0.107:8091/',
  56. changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
  57. pathRewrite: { '^/pb': '/' }, //这里重写路径
  58. secure: false,
  59. logLevel: 'debug',
  60. },
  61. },
  62. // https: true
  63. },
  64. configureWebpack: {
  65. // provide the app's title in webpack's name field, so that
  66. // it can be accessed in index.html to inject the correct title.
  67. name: name,
  68. resolve: {
  69. alias: {
  70. '@': resolve('src')
  71. }
  72. }
  73. },
  74. chainWebpack(config) {
  75. // it can improve the speed of the first screen, it is recommended to turn on preload
  76. config.plugin('preload').tap(() => [
  77. {
  78. rel: 'preload',
  79. // to ignore runtime.js
  80. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  81. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  82. include: 'initial'
  83. }
  84. ])
  85. // when there are many pages, it will cause too many meaningless requests
  86. config.plugins.delete('prefetch')
  87. config.plugin('html')
  88. .tap(args => {
  89. args[0].cdn = cdn
  90. return args
  91. })
  92. // set svg-sprite-loader
  93. config.module
  94. .rule('svg')
  95. .exclude.add(resolve('src/icons'))
  96. .end()
  97. config.module
  98. .rule('icons')
  99. .test(/\.svg$/)
  100. .include.add(resolve('src/icons'))
  101. .end()
  102. .use('svg-sprite-loader')
  103. .loader('svg-sprite-loader')
  104. .options({
  105. symbolId: 'icon-[name]'
  106. })
  107. .end()
  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. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  144. config.optimization.runtimeChunk('single')
  145. }
  146. )
  147. }
  148. }