vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CopyPlugin = require('copy-webpack-plugin')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || '售前管理系统' // 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. // You can change the port by the following methods:
  13. // port = 9528 npm run dev OR npm run dev --port = 9528
  14. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  15. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  16. // vue.config.js 配置说明
  17. // 官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  18. // 这里只列一部分,具体配置参考文档
  19. module.exports = {
  20. /**
  21. * You will need to set publicPath if you plan to deploy your site under a sub path,
  22. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  23. * then publicPath should be set to "/bar/".
  24. * In most cases please use '/' !!!
  25. * Detail: https://cli.vuejs.org/config/#publicpath
  26. */
  27. // 部署生产环境和开发环境下的URL。
  28. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  29. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。
  30. // 例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  31. publicPath: '/',
  32. outputDir: 'dist',
  33. assetsDir: 'static',
  34. lintOnSave: process.env.NODE_ENV === 'development',
  35. productionSourceMap: false,
  36. devServer: {
  37. port: port,
  38. open: true,
  39. overlay: {
  40. warnings: false,
  41. errors: true
  42. },
  43. // before: require('./mock/mock-server.js'),//连后台环境时注释掉
  44. // 配置转发代理
  45. proxy: {
  46. // detail: https://cli.vuejs.org/config/#devserver-proxy
  47. [process.env.VUE_APP_BASE_API]: {
  48. target: `https://localhost:444`, // 本地环境
  49. // target: `https://123.60.57.251:443`, // 测试环境
  50. // target: `https://baidu.com`,
  51. changeOrigin: true,
  52. pathRewrite: {
  53. ['^' + process.env.VUE_APP_BASE_API]: ''
  54. }
  55. }
  56. }
  57. // disableHostCheck: true
  58. },
  59. configureWebpack: {
  60. // provide the app's title in webpack's name field, so that
  61. // it can be accessed in index.html to inject the correct title.
  62. name: name,
  63. resolve: {
  64. alias: {
  65. '@': resolve('src')
  66. }
  67. },
  68. plugins: [
  69. new CopyPlugin([
  70. {
  71. from: path.resolve(__dirname, './public/robots.txt'), // 防爬虫文件
  72. to: './' // 到根目录下
  73. }
  74. ])
  75. ]
  76. },
  77. chainWebpack(config) {
  78. // it can improve the speed of the first screen, it is recommended to turn on preload
  79. config.plugin('preload').tap(() => [
  80. {
  81. rel: 'preload',
  82. // to ignore runtime.js
  83. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  84. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  85. include: 'initial'
  86. }
  87. ])
  88. // when there are many pages, it will cause too many meaningless requests
  89. config.plugins.delete('prefetch')
  90. // set svg-sprite-loader
  91. config.module
  92. .rule('svg')
  93. .exclude.add(resolve('src/icons'))
  94. .end()
  95. config.module
  96. .rule('icons')
  97. .test(/\.svg$/)
  98. .include.add(resolve('src/icons'))
  99. .end()
  100. .use('svg-sprite-loader')
  101. .loader('svg-sprite-loader')
  102. .options({
  103. symbolId: 'icon-[name]'
  104. })
  105. .end()
  106. config
  107. .when(process.env.NODE_ENV !== 'development',
  108. config => {
  109. config
  110. .plugin('ScriptExtHtmlWebpackPlugin')
  111. .after('html')
  112. .use('script-ext-html-webpack-plugin', [{
  113. // `runtime` must same as runtimeChunk name. default is `runtime`
  114. inline: /runtime\..*\.js$/
  115. }])
  116. .end()
  117. config
  118. .optimization.splitChunks({
  119. chunks: 'all',
  120. cacheGroups: {
  121. libs: {
  122. name: 'chunk-libs',
  123. test: /[\\/]node_modules[\\/]/,
  124. priority: 10,
  125. chunks: 'initial' // only package third parties that are initially dependent
  126. },
  127. elementUI: {
  128. name: 'chunk-elementUI', // split elementUI into a single package
  129. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  130. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  131. },
  132. commons: {
  133. name: 'chunk-commons',
  134. test: resolve('src/components'), // can customize your rules
  135. minChunks: 3, // minimum common number
  136. priority: 5,
  137. reuseExistingChunk: true
  138. }
  139. }
  140. })
  141. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  142. config.optimization.runtimeChunk('single')
  143. }
  144. )
  145. }
  146. }