vite.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { viteSingleFile } from 'vite-plugin-singlefile'
  4. import fs from 'fs'
  5. import path from 'path'
  6. // 人设图谱数据路径由 Python 通过环境变量传入
  7. const personaDataPath = process.env.GRAPH_DATA_PATH
  8. if (!personaDataPath) {
  9. console.error('错误: 请设置 GRAPH_DATA_PATH 环境变量')
  10. process.exit(1)
  11. }
  12. console.log('人设图谱数据文件:', personaDataPath)
  13. // 读取人设图谱 JSON 数据
  14. const personaGraphData = JSON.parse(fs.readFileSync(personaDataPath, 'utf-8'))
  15. console.log('人设节点数:', Object.keys(personaGraphData.nodes || {}).length)
  16. // 帖子图谱数据路径(可选,目录)
  17. const postGraphDir = process.env.POST_GRAPH_DIR
  18. let postGraphList = []
  19. // 推导图谱目录(与 post_graph 同级的 node_origin_analysis 目录)
  20. const derivationGraphDir = postGraphDir ? path.join(path.dirname(postGraphDir), 'node_origin_analysis') : null
  21. if (postGraphDir && fs.existsSync(postGraphDir)) {
  22. console.log('帖子图谱目录:', postGraphDir)
  23. const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json'))
  24. console.log('帖子图谱文件数:', files.length)
  25. // 读取推导图谱(如果存在)
  26. const derivationGraphs = new Map()
  27. if (derivationGraphDir && fs.existsSync(derivationGraphDir)) {
  28. console.log('推导图谱目录:', derivationGraphDir)
  29. const derivationFiles = fs.readdirSync(derivationGraphDir).filter(f => f.endsWith('_推导图谱.json'))
  30. console.log('推导图谱文件数:', derivationFiles.length)
  31. for (const file of derivationFiles) {
  32. const filePath = path.join(derivationGraphDir, file)
  33. const derivationData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  34. const postId = derivationData.meta?.postId
  35. if (postId) {
  36. derivationGraphs.set(postId, derivationData)
  37. }
  38. }
  39. }
  40. // 读取所有帖子图谱
  41. for (const file of files) {
  42. const filePath = path.join(postGraphDir, file)
  43. const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  44. // 合并推导图谱(如果存在)
  45. const postId = postData.meta?.postId
  46. if (postId && derivationGraphs.has(postId)) {
  47. const derivation = derivationGraphs.get(postId)
  48. console.log(`合并推导图谱到帖子 ${postId}:`, derivation.meta?.stats)
  49. // 合并节点(组合节点)
  50. for (const [nodeId, node] of Object.entries(derivation.nodes || {})) {
  51. if (!postData.nodes[nodeId]) {
  52. postData.nodes[nodeId] = node
  53. }
  54. }
  55. // 合并边(推导边和组成边)
  56. for (const [edgeId, edge] of Object.entries(derivation.edges || {})) {
  57. if (!postData.edges[edgeId]) {
  58. postData.edges[edgeId] = edge
  59. }
  60. }
  61. }
  62. postGraphList.push(postData)
  63. }
  64. // 按创建时间降序排序
  65. postGraphList.sort((a, b) => {
  66. const dateA = a.meta?.postDetail?.create_time || 0
  67. const dateB = b.meta?.postDetail?.create_time || 0
  68. return dateB - dateA
  69. })
  70. } else {
  71. console.log('未设置帖子图谱目录或目录不存在')
  72. }
  73. export default defineConfig({
  74. plugins: [vue(), viteSingleFile()],
  75. define: {
  76. // 将数据注入为全局常量
  77. __GRAPH_DATA__: JSON.stringify(personaGraphData),
  78. __POST_GRAPH_LIST__: JSON.stringify(postGraphList)
  79. },
  80. build: {
  81. target: 'esnext',
  82. outDir: 'dist',
  83. assetsInlineLimit: 100000000,
  84. chunkSizeWarningLimit: 100000000,
  85. cssCodeSplit: false,
  86. rollupOptions: {
  87. output: {
  88. inlineDynamicImports: true
  89. }
  90. }
  91. }
  92. })