vite.config.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if (postGraphDir && fs.existsSync(postGraphDir)) {
  20. console.log('帖子图谱目录:', postGraphDir)
  21. const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json'))
  22. console.log('帖子图谱文件数:', files.length)
  23. // 读取所有帖子图谱
  24. for (const file of files) {
  25. const filePath = path.join(postGraphDir, file)
  26. const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
  27. postGraphList.push(postData)
  28. }
  29. // 按创建时间降序排序
  30. postGraphList.sort((a, b) => {
  31. const dateA = a.meta?.postDetail?.create_time || 0
  32. const dateB = b.meta?.postDetail?.create_time || 0
  33. return dateB - dateA
  34. })
  35. } else {
  36. console.log('未设置帖子图谱目录或目录不存在')
  37. }
  38. export default defineConfig({
  39. plugins: [vue(), viteSingleFile()],
  40. define: {
  41. // 将数据注入为全局常量
  42. __GRAPH_DATA__: JSON.stringify(personaGraphData),
  43. __POST_GRAPH_LIST__: JSON.stringify(postGraphList)
  44. },
  45. build: {
  46. target: 'esnext',
  47. outDir: 'dist',
  48. assetsInlineLimit: 100000000,
  49. chunkSizeWarningLimit: 100000000,
  50. cssCodeSplit: false,
  51. rollupOptions: {
  52. output: {
  53. inlineDynamicImports: true
  54. }
  55. }
  56. }
  57. })