import path from 'path'; import fs from 'fs'; import { checkForCssFiles } from './utils'; /** Функция проверки vite.config.ts. Зависимости проставляются в checkDependencies.ts */ export async function checkViteConfig(project: string) { const projectDir = path.join(__dirname, '../../packages/react', project); const filePath = path.join(projectDir, 'vite.config.ts'); try { const content = fs.readFileSync(filePath, 'utf8'); let updatedContent = content; const hasCss = checkForCssFiles(projectDir); if (hasCss) { if (!content.includes('vite-plugin-lib-inject-css')) { updatedContent = updatedContent.replace( /(import \* as path from 'path';)/, `$1\nimport { libInjectCss } from 'vite-plugin-lib-inject-css';` ); } if (!content.includes('libInjectCss()')) { updatedContent = updatedContent.replace( /(plugins: \[\s*react\(\),)/, `$1\n libInjectCss(),` ); } } const shouldAddAliasesExclude = !content.includes('aliasesExclude'); const shouldAddStaticImport = !content.includes('staticImport: true'); const needsUpdate = shouldAddAliasesExclude || shouldAddStaticImport; if (needsUpdate) { const dtsConfigPattern = /(dts\(\s*{\s*)([\s\S]*?)(\s*)(}\);?)/gs; updatedContent = updatedContent.replace( dtsConfigPattern, (match, prefix, innerContent, whitespaceBeforeClosing, suffix) => { const newProperties: string[] = []; if (shouldAddAliasesExclude) { newProperties.push('aliasesExclude: [/^@dpa\\//],'); } if (shouldAddStaticImport) { newProperties.push('staticImport: true,'); } if (newProperties.length === 0) return match; const formattedProperties = newProperties.join('\n '); const indent = innerContent.match(/\n(\s*)\S/)?.pop() || ' '; return `${prefix}${innerContent} ${indent}${formattedProperties}${whitespaceBeforeClosing}${suffix}`; } ); } if (updatedContent !== content) { fs.writeFileSync(filePath, updatedContent + '\n'); } } catch (error) { console.error('Ошибка при обновлении vite.config.ts:', error); throw error; } }