Remove legacy landing and documentation files, transitioning to a new structure with Pug templates for index and terms pages. Update Vite configuration for improved build process and streamline project organization. Clean up package dependencies and enhance project clarity.
This commit is contained in:
+68
-72
@@ -1,85 +1,81 @@
|
||||
import { defineConfig } from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
import { defineConfig, Plugin } from 'vite';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import pug from 'pug';
|
||||
|
||||
// Express middleware для stubs
|
||||
const stubsMiddleware = () => ({
|
||||
name: 'stubs-middleware',
|
||||
configureServer(server: any) {
|
||||
const stubsPath = path.resolve(__dirname, 'stubs/api');
|
||||
// Плагин для обработки Pug файлов
|
||||
function pugPlugin(): Plugin {
|
||||
return {
|
||||
name: 'vite-plugin-pug',
|
||||
|
||||
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' }));
|
||||
// Трансформация HTML из Pug во время сборки и dev
|
||||
transformIndexHtml: {
|
||||
order: 'pre',
|
||||
handler(html, ctx) {
|
||||
// Определяем путь к соответствующему .pug файлу
|
||||
const filename = ctx.filename;
|
||||
let pugPath: string | null = null;
|
||||
|
||||
if (filename.endsWith('index.html')) {
|
||||
pugPath = path.resolve(__dirname, 'src/html/index.pug');
|
||||
} else if (filename.endsWith('terms.html')) {
|
||||
pugPath = path.resolve(__dirname, 'src/html/terms.pug');
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
|
||||
if (pugPath && fs.existsSync(pugPath)) {
|
||||
console.log(`[pug-plugin] Compiling ${pugPath}`);
|
||||
const pugContent = fs.readFileSync(pugPath, 'utf-8');
|
||||
const compiled = pug.render(pugContent, {
|
||||
filename: pugPath,
|
||||
basedir: path.dirname(pugPath),
|
||||
pretty: process.env.NODE_ENV !== 'production'
|
||||
});
|
||||
console.log(`[pug-plugin] Compiled HTML length: ${compiled.length}`);
|
||||
return compiled;
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Hot reload для .pug файлов
|
||||
handleHotUpdate({ file, server }) {
|
||||
if (file.endsWith('.pug')) {
|
||||
console.log(`[pug-plugin] Hot reload triggered for ${file}`);
|
||||
server.ws.send({
|
||||
type: 'full-reload',
|
||||
path: '*'
|
||||
});
|
||||
return [];
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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'),
|
||||
plugins: [pugPlugin()],
|
||||
base: isProd ? 'https://static.brojs.ru/landing/main/' : '/',
|
||||
server: {
|
||||
port: 8099,
|
||||
open: '/',
|
||||
},
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
assetsDir: '.',
|
||||
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]',
|
||||
}
|
||||
},
|
||||
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
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user