vue.config.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. const webpack = require('webpack')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. // npm 提升 @aws-sdk 子包后,babel-loader 缓存仍可能指向
  9. // @aws-sdk/client-s3/node_modules/@aws-sdk/* 等已不存在的嵌套路径,导致 ENOENT。
  10. const DEPS_CACHE_VERSION = 'aws-sdk-hoist-fix-1'
  11. function clearWebpackCacheIfStale() {
  12. const cacheDir = path.join(__dirname, 'node_modules/.cache')
  13. const markerPath = path.join(cacheDir, '.deps-layout-marker')
  14. const lockPath = path.join(__dirname, 'package-lock.json')
  15. let lockId = 'no-lock'
  16. try {
  17. const stat = fs.statSync(lockPath)
  18. lockId = `${stat.mtimeMs}-${stat.size}`
  19. } catch (e) {
  20. // ignore
  21. }
  22. const marker = `${DEPS_CACHE_VERSION}|${lockId}`
  23. let prev = ''
  24. try {
  25. if (fs.existsSync(markerPath)) {
  26. prev = fs.readFileSync(markerPath, 'utf8')
  27. }
  28. } catch (e) {
  29. // ignore
  30. }
  31. if (prev !== marker && fs.existsSync(cacheDir)) {
  32. fs.rmSync(cacheDir, { recursive: true, force: true })
  33. console.log('[vue.config] Cleared webpack cache (dependency layout changed)')
  34. }
  35. try {
  36. fs.mkdirSync(cacheDir, { recursive: true })
  37. fs.writeFileSync(markerPath, marker)
  38. } catch (e) {
  39. // ignore
  40. }
  41. }
  42. clearWebpackCacheIfStale()
  43. const name = process.env.VUE_APP_TITLE || '云联私域管理系统' // 网页标题
  44. const port = process.env.port || process.env.npm_config_port || 80 // 端口
  45. // vue.config.js 配置说明
  46. //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  47. // 这里只列一部分,具体配置参考文档
  48. module.exports = {
  49. // 部署生产环境和开发环境下的URL。
  50. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  51. // 例如 https://www.test.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.test.vip/admin/,则设置 baseUrl 为 /admin/。
  52. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  53. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  54. outputDir: 'dist',
  55. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  56. assetsDir: 'static',
  57. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  58. lintOnSave: process.env.NODE_ENV === 'development',
  59. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  60. productionSourceMap: false,
  61. // CSS 相关配置
  62. css: {
  63. loaderOptions: {
  64. scss: {
  65. // 全局注入共享 mixin,组件无需手动 @import
  66. prependData: `
  67. @import "~@/assets/styles/page-layout.scss";
  68. @import "~@/assets/styles/dialog-layout.scss";
  69. @import "~@/assets/styles/page-transitions.scss";
  70. `
  71. }
  72. }
  73. },
  74. // transpileDependencies: true, // 默认情况下 babel-loader 忽略 node_modules 中的所有文件,启用此选项需配置transpileDependencies
  75. transpileDependencies: [
  76. /@aws-sdk/,
  77. '@aws/lambda-invoke-store',
  78. /@aws/,
  79. /@smithy/,
  80. /@huaweicloud/,
  81. /vod-js-sdk-v6/
  82. ],
  83. // webpack-dev-server 相关配置
  84. devServer: {
  85. host: '0.0.0.0',
  86. port: port,
  87. open: true,
  88. disableHostCheck: true,
  89. proxy: {
  90. // watch模块代理(fs-watch微服务,独立部署)
  91. '/watch-api': {
  92. target: 'http://localhost:8010',
  93. changeOrigin: true,
  94. pathRewrite: {
  95. '^/watch-api': ''
  96. }
  97. },
  98. // 向量知识库 / AI → fs-ai-api (9009)
  99. '/prod-api/qdrant': {
  100. target: 'http://localhost:9009',
  101. changeOrigin: true,
  102. pathRewrite: { '^/prod-api': '' }
  103. },
  104. '/prod-api/ai/': {
  105. target: 'http://localhost:9009',
  106. changeOrigin: true,
  107. pathRewrite: { '^/prod-api': '' }
  108. },
  109. // ===== 默认所有其他请求 → fs-saasadmin(8004) 租户总后台 =====
  110. // saasadminui 严格对应 fs-saasadmin(8004),不跨服务调用
  111. // @Profile("admin") 和 @Profile({"admin","company"}) 的 Controllers 在 8004 可用
  112. // com.fs.company.controller 中的 store/workflow/companyWorkflow/tag 等子包也已放开加载
  113. // 冲突子包(chat/company/course/crm/fastGpt/live/newAdv/qw/stats/transfer)已排除,由 fs-saasadmin 自有 Controller 覆盖
  114. [process.env.VUE_APP_BASE_API]: {
  115. target: 'http://localhost:8004',
  116. changeOrigin: true,
  117. pathRewrite: { ['^' + process.env.VUE_APP_BASE_API]: '' }
  118. }
  119. }
  120. },
  121. configureWebpack: {
  122. name: name,
  123. resolve: {
  124. alias: {
  125. '@': resolve('src')
  126. }
  127. },
  128. plugins: [
  129. // @smithy/@aws-sdk 会 import node:stream 等内置模块,浏览器用空模块替代
  130. new webpack.NormalModuleReplacementPlugin(/^node:/, resource => {
  131. resource.request = resolve('src/utils/node-empty-module.js')
  132. })
  133. ]
  134. },
  135. chainWebpack(config) {
  136. config.plugins.delete('preload') // TODO: need test
  137. config.plugins.delete('prefetch') // TODO: need test
  138. // set svg-sprite-loader
  139. config.module
  140. .rule('svg')
  141. .exclude.add(resolve('src/assets/icons'))
  142. .end()
  143. config.module
  144. .rule('icons')
  145. .test(/\.svg$/)
  146. .include.add(resolve('src/assets/icons'))
  147. .end()
  148. .use('svg-sprite-loader')
  149. .loader('svg-sprite-loader')
  150. .options({
  151. symbolId: 'icon-[name]'
  152. })
  153. .end()
  154. config
  155. .when(process.env.NODE_ENV !== 'development',
  156. config => {
  157. config
  158. .plugin('ScriptExtHtmlWebpackPlugin')
  159. .after('html')
  160. .use('script-ext-html-webpack-plugin', [{
  161. // `runtime` must same as runtimeChunk name. default is `runtime`
  162. inline: /runtime\..*\.js$/
  163. }])
  164. .end()
  165. config
  166. .optimization.splitChunks({
  167. chunks: 'all',
  168. cacheGroups: {
  169. libs: {
  170. name: 'chunk-libs',
  171. test: /[\\/]node_modules[\\/]/,
  172. priority: 10,
  173. chunks: 'initial' // only package third parties that are initially dependent
  174. },
  175. elementUI: {
  176. name: 'chunk-elementUI', // split elementUI into a single package
  177. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  178. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  179. },
  180. commons: {
  181. name: 'chunk-commons',
  182. test: resolve('src/components'), // can customize your rules
  183. minChunks: 3, // minimum common number
  184. priority: 5,
  185. reuseExistingChunk: true
  186. }
  187. }
  188. })
  189. config.optimization.runtimeChunk('single')
  190. // Windows 下 terser 多进程压缩易触发 write UNKNOWN,改为单进程
  191. config.optimization.minimizer('terser').tap((args) => {
  192. const opts = args[0] || {}
  193. opts.parallel = false
  194. args[0] = opts
  195. return args
  196. })
  197. }
  198. )
  199. }
  200. }