vite.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // 推导图谱数据:{ postId: { v3: data, v4: data, ... } }
  22. const derivationGraphs = {}
  23. // 所有可用的版本
  24. const derivationVersions = new Set()
  25. if (postGraphDir && fs.existsSync(postGraphDir)) {
  26. console.log('帖子图谱目录:', postGraphDir)
  27. const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json'))
  28. console.log('帖子图谱文件数:', files.length)
  29. // 读取所有推导图谱(所有版本)
  30. if (derivationGraphDir && fs.existsSync(derivationGraphDir)) {
  31. console.log('推导图谱目录:', derivationGraphDir)
  32. // 匹配所有推导图谱文件:*_推导图谱.json 或 *_推导图谱_v*.json
  33. const derivationFiles = fs.readdirSync(derivationGraphDir).filter(f => f.includes('_推导图谱') && f.endsWith('.json'))
  34. console.log('推导图谱文件数:', derivationFiles.length)
  35. for (const file of derivationFiles) {
  36. const filePath = path.join(derivationGraphDir, file)
  37. const derivationData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  38. const postId = derivationData.meta?.postId
  39. const version = derivationData.meta?.version || 'default'
  40. if (postId) {
  41. if (!derivationGraphs[postId]) {
  42. derivationGraphs[postId] = {}
  43. }
  44. derivationGraphs[postId][version] = derivationData
  45. derivationVersions.add(version)
  46. console.log(`加载推导图谱: ${postId} [${version}]`, derivationData.meta?.stats)
  47. }
  48. }
  49. }
  50. // 读取所有帖子图谱(不合并推导数据,由前端动态合并)
  51. for (const file of files) {
  52. const filePath = path.join(postGraphDir, file)
  53. const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  54. postGraphList.push(postData)
  55. }
  56. // 按创建时间降序排序
  57. postGraphList.sort((a, b) => {
  58. const dateA = a.meta?.postDetail?.create_time || 0
  59. const dateB = b.meta?.postDetail?.create_time || 0
  60. return dateB - dateA
  61. })
  62. console.log('推导图谱版本:', Array.from(derivationVersions).sort())
  63. } else {
  64. console.log('未设置帖子图谱目录或目录不存在')
  65. }
  66. export default defineConfig({
  67. plugins: [vue(), viteSingleFile()],
  68. define: {
  69. // 将数据注入为全局常量
  70. __GRAPH_DATA__: JSON.stringify(personaGraphData),
  71. __POST_GRAPH_LIST__: JSON.stringify(postGraphList),
  72. __DERIVATION_GRAPHS__: JSON.stringify(derivationGraphs),
  73. __DERIVATION_VERSIONS__: JSON.stringify(Array.from(derivationVersions).sort())
  74. },
  75. build: {
  76. target: 'esnext',
  77. outDir: 'dist',
  78. assetsInlineLimit: 100000000,
  79. chunkSizeWarningLimit: 100000000,
  80. cssCodeSplit: false,
  81. rollupOptions: {
  82. output: {
  83. inlineDynamicImports: true
  84. }
  85. }
  86. }
  87. })