vue.config.js 4.4 KB

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