You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.4 KiB

3 years ago
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CopyWebpackPlugin = require('copy-webpack-plugin')
  5. const webpack = require('webpack')
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || '若依管理系统' // 标题
  10. const port = process.env.port || process.env.npm_config_port || 80 // 端口
  11. // vue.config.js 配置说明
  12. //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  13. // 这里只列一部分,具体配置参考文档
  14. module.exports = {
  15. // 部署生产环境和开发环境下的URL。
  16. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  17. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  18. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  19. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  20. outputDir: 'dist',
  21. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  22. assetsDir: 'static',
  23. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  24. lintOnSave: process.env.NODE_ENV === 'development',
  25. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  26. productionSourceMap: false,
  27. // webpack-dev-server 相关配置
  28. devServer: {
  29. host: '0.0.0.0',
  30. port: port,
  31. open: true,
  32. proxy: {
  33. // detail: https://cli.vuejs.org/config/#devserver-proxy
  34. [process.env.VUE_APP_BASE_API]: {
  35. target: `http://127.0.0.1:8501/`,
  36. changeOrigin: true,
  37. pathRewrite: {
  38. ['^' + process.env.VUE_APP_BASE_API]: ''
  39. }
  40. }
  41. },
  42. disableHostCheck: true
  43. },
  44. configureWebpack: {
  45. name: name,
  46. resolve: {
  47. alias: {
  48. '@': resolve('src')
  49. }
  50. }
  51. },
  52. chainWebpack(config) {
  53. config.plugins.delete('preload') // TODO: need test
  54. config.plugins.delete('prefetch') // TODO: need test
  55. // set svg-sprite-loader
  56. config.module
  57. .rule('svg')
  58. .exclude.add(resolve('src/assets/icons'))
  59. .end()
  60. config.module
  61. .rule('icons')
  62. .test(/\.svg$/)
  63. .include.add(resolve('src/assets/icons'))
  64. .end()
  65. .use('svg-sprite-loader')
  66. .loader('svg-sprite-loader')
  67. .options({
  68. symbolId: 'icon-[name]'
  69. })
  70. .end()
  71. // set preserveWhitespace
  72. config.module
  73. .rule('vue')
  74. .use('vue-loader')
  75. .loader('vue-loader')
  76. .tap(options => {
  77. options.compilerOptions.preserveWhitespace = true
  78. return options
  79. })
  80. .end()
  81. config
  82. .when(process.env.NODE_ENV !== 'development',
  83. config => {
  84. config
  85. .plugin('ScriptExtHtmlWebpackPlugin')
  86. .after('html')
  87. .use('script-ext-html-webpack-plugin', [{
  88. // `runtime` must same as runtimeChunk name. default is `runtime`
  89. inline: /runtime\..*\.js$/
  90. }])
  91. .end()
  92. config
  93. .optimization.splitChunks({
  94. chunks: 'all',
  95. cacheGroups: {
  96. libs: {
  97. name: 'chunk-libs',
  98. test: /[\\/]node_modules[\\/]/,
  99. priority: 10,
  100. chunks: 'initial' // only package third parties that are initially dependent
  101. },
  102. elementUI: {
  103. name: 'chunk-elementUI', // split elementUI into a single package
  104. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  105. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  106. },
  107. commons: {
  108. name: 'chunk-commons',
  109. test: resolve('src/components'), // can customize your rules
  110. minChunks: 3, // minimum common number
  111. priority: 5,
  112. reuseExistingChunk: true
  113. }
  114. }
  115. })
  116. config.optimization.runtimeChunk('single'),
  117. {
  118. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  119. to: './', //到根目录下
  120. }
  121. }
  122. )
  123. }
  124. }