bro.landing/vite.config.ts

86 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import fs from 'fs';
// Express middleware для stubs
const stubsMiddleware = () => ({
name: 'stubs-middleware',
configureServer(server: any) {
const stubsPath = path.resolve(__dirname, 'stubs/api');
server.middlewares.use('/api', (req: any, res: any, next: any) => {
// Получаем путь запроса без /api
const apiPath = req.url.replace(/\?.*$/, ''); // убираем query params
const stubFile = path.join(stubsPath, `${apiPath}.js`);
// Проверяем существует ли stub файл
if (fs.existsSync(stubFile)) {
try {
// Очищаем кеш модуля для hot reload
delete require.cache[require.resolve(stubFile)];
const stub = require(stubFile);
// Если это функция, вызываем её
if (typeof stub === 'function') {
stub(req, res, next);
} else if (stub.default && typeof stub.default === 'function') {
stub.default(req, res, next);
} else {
// Если это просто объект, отдаём как JSON
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(stub.default || stub));
}
} catch (error) {
console.error(`Error loading stub ${stubFile}:`, error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Internal Server Error' }));
}
} else {
next();
}
});
}
});
export default defineConfig(({ mode }) => {
const isProd = mode === 'production';
return {
plugins: [
react(),
stubsMiddleware()
],
base: isProd ? 'https://static.brojs.ru/landing/main/' : '/',
server: {
port: 8099,
open: '/',
},
build: {
outDir: 'dist',
assetsDir: '.', // Все ассеты в корень dist
rollupOptions: {
input: {
main: path.resolve(__dirname, 'index.html'),
terms: path.resolve(__dirname, 'terms.html'),
},
output: {
entryFileNames: '[name].[hash].js',
chunkFileNames: '[name].[hash].js',
assetFileNames: '[name].[hash].[ext]'
}
},
},
css: {
modules: {
localsConvention: 'camelCase',
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
}}); // Двойная скобка для закрытия return и defineConfig