Доработки для интеграции

This commit is contained in:
NikitaKalitov
2026-08-25 16:08:35 +03:00
parent 1a9f24e53f
commit 0d714ab38e
7 changed files with 239 additions and 19 deletions
+32 -13
View File
@@ -1,7 +1,7 @@
import { createHash } from 'node:crypto'
import { existsSync, readFileSync, renameSync } from 'node:fs'
import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { defineConfig, loadEnv, type Plugin } from 'vite'
import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
// Vite always writes the HTML entry under its source name (index.html stays
@@ -14,6 +14,9 @@ import react from '@vitejs/plugin-react'
// (confirmed empirically — it never showed up in generateBundle's `bundle`),
// so this hashes and renames the file directly on disk after the whole build
// has finished writing it, rather than fighting the bundler's own emit step.
// Stable entry point for hosts: never hashed, always present next to the build.
const MANIFEST_FILE_NAME = 'app.json'
function hashIndexHtml(): Plugin {
let outDir = 'dist'
return {
@@ -29,24 +32,40 @@ function hashIndexHtml(): Plugin {
// Hashing the final content (already contains the hashed JS/CSS refs)
// means this name only changes when something meaningful actually did.
const hash = createHash('md5').update(readFileSync(htmlPath)).digest('hex').slice(0, 8)
renameSync(htmlPath, path.resolve(outDir, `index-${hash}.html`))
const fileName = `index-${hash}.html`
renameSync(htmlPath, path.resolve(outDir, fileName))
// Manifest with a STABLE name, so an embedding host can look the hashed
// entry up at runtime instead of hardcoding it (and re-deploying itself
// on every rebuild of this app). See docs/bitrix-integration.md §7.
writeFileSync(
path.resolve(outDir, MANIFEST_FILE_NAME),
`${JSON.stringify({ index: fileName }, null, 2)}
`,
)
},
}
}
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
// VITE_BASE_PATH is read here (config/Node context), so it needs an explicit
// loadEnv call — unlike VITE_BITRIX_ORIGIN, which Vite injects into
// import.meta.env automatically because it's only read from app code.
const env = loadEnv(mode, process.cwd(), '')
export default defineConfig(() => {
return {
// Needed when the build is served from a subpath (e.g. local/diagram-app/
// inside the Bitrix docroot) instead of domain root — see
// docs/bitrix-integration.md §7.
base: env.VITE_BASE_PATH || '/',
// Relative, so the build works from any directory it gets deployed to
// without being told where that is. The host always opens a concrete file
// (index-<hash>.html, see hashIndexHtml below), so asset URLs resolve
// against that file's own location — there is no client-side routing that
// would need an absolute base. This replaces the earlier VITE_BASE_PATH:
// a build-time path that had to match the deploy directory exactly, and
// silently produced a blank iframe when it didn't (html loads, its JS 404s).
base: './',
plugins: [react(), hashIndexHtml()],
// Порт фиксирован: portal-front проксирует сюда сборку в dev-режиме
// (см. docs/portal-front-integration.md, раздел 2). strictPort, чтобы
// при занятом порте preview падал, а не уезжал на соседний молча.
preview: {
port: 5800,
strictPort: true,
},
build: {
// Same content hash Vite computes by default — just without its default
// "assets/" subfolder prefix. The deploy target's own folder is itself