vite.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // 创作模式数据:{ version: { postId: data } }
  26. const creationPatterns = {}
  27. // 创作模式版本列表
  28. const creationPatternVersions = []
  29. if (postGraphDir && fs.existsSync(postGraphDir)) {
  30. console.log('帖子图谱目录:', postGraphDir)
  31. const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json'))
  32. console.log('帖子图谱文件数:', files.length)
  33. // 读取所有推导图谱(所有版本)
  34. if (derivationGraphDir && fs.existsSync(derivationGraphDir)) {
  35. console.log('推导图谱目录:', derivationGraphDir)
  36. // 匹配所有推导图谱文件:*_推导图谱.json 或 *_推导图谱_v*.json
  37. const derivationFiles = fs.readdirSync(derivationGraphDir).filter(f => f.includes('_推导图谱') && f.endsWith('.json'))
  38. console.log('推导图谱文件数:', derivationFiles.length)
  39. for (const file of derivationFiles) {
  40. const filePath = path.join(derivationGraphDir, file)
  41. const derivationData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  42. const postId = derivationData.meta?.postId
  43. const version = derivationData.meta?.version || 'default'
  44. if (postId) {
  45. if (!derivationGraphs[postId]) {
  46. derivationGraphs[postId] = {}
  47. }
  48. derivationGraphs[postId][version] = derivationData
  49. derivationVersions.add(version)
  50. console.log(`加载推导图谱: ${postId} [${version}]`, derivationData.meta?.stats)
  51. }
  52. }
  53. }
  54. // 读取所有帖子图谱(不合并推导数据,由前端动态合并)
  55. for (const file of files) {
  56. const filePath = path.join(postGraphDir, file)
  57. const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  58. postGraphList.push(postData)
  59. }
  60. // 按创建时间降序排序
  61. postGraphList.sort((a, b) => {
  62. const dateA = a.meta?.postDetail?.create_time || 0
  63. const dateB = b.meta?.postDetail?.create_time || 0
  64. return dateB - dateA
  65. })
  66. console.log('推导图谱版本:', Array.from(derivationVersions).sort())
  67. // 读取创作模式数据(多版本)
  68. const parentDir = path.dirname(postGraphDir)
  69. const allDirs = fs.readdirSync(parentDir)
  70. // 只匹配 creation_pattern_v 加数字的目录(如 v2, v3, v4)
  71. const patternDirs = allDirs.filter(d => /^creation_pattern_v\d+$/.test(d))
  72. .sort((a, b) => {
  73. // 按版本号排序 v2, v3, v4...
  74. const va = parseInt(a.replace('creation_pattern_v', '')) || 0
  75. const vb = parseInt(b.replace('creation_pattern_v', '')) || 0
  76. return va - vb
  77. })
  78. console.log('创作模式版本目录:', patternDirs)
  79. for (const dirName of patternDirs) {
  80. const version = dirName.replace('creation_pattern_', '') // 'v2', 'v3', 'v4'
  81. const dirPath = path.join(parentDir, dirName)
  82. if (!fs.statSync(dirPath).isDirectory()) continue
  83. creationPatternVersions.push(version)
  84. creationPatterns[version] = {}
  85. const patternFiles = fs.readdirSync(dirPath).filter(f => f.endsWith('_创作模式.json'))
  86. console.log(`创作模式 [${version}] 文件数:`, patternFiles.length)
  87. for (const file of patternFiles) {
  88. const filePath = path.join(dirPath, file)
  89. const patternData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  90. const postId = patternData.帖子详情?.postId
  91. if (postId) {
  92. creationPatterns[version][postId] = patternData
  93. console.log(` 加载: ${postId}`)
  94. }
  95. }
  96. }
  97. } else {
  98. console.log('未设置帖子图谱目录或目录不存在')
  99. }
  100. export default defineConfig({
  101. plugins: [vue(), viteSingleFile()],
  102. define: {
  103. // 将数据注入为全局常量
  104. __GRAPH_DATA__: JSON.stringify(personaGraphData),
  105. __POST_GRAPH_LIST__: JSON.stringify(postGraphList),
  106. __DERIVATION_GRAPHS__: JSON.stringify(derivationGraphs),
  107. __DERIVATION_VERSIONS__: JSON.stringify(Array.from(derivationVersions).sort()),
  108. __CREATION_PATTERNS__: JSON.stringify(creationPatterns),
  109. __CREATION_PATTERN_VERSIONS__: JSON.stringify(creationPatternVersions)
  110. },
  111. build: {
  112. target: 'esnext',
  113. outDir: 'dist',
  114. assetsInlineLimit: 100000000,
  115. chunkSizeWarningLimit: 100000000,
  116. cssCodeSplit: false,
  117. rollupOptions: {
  118. output: {
  119. inlineDynamicImports: true
  120. }
  121. }
  122. }
  123. })