82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { defineConfig, Plugin } from 'vite';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import pug from 'pug';
|
|
|
|
// Плагин для обработки Pug файлов
|
|
function pugPlugin(): Plugin {
|
|
return {
|
|
name: 'vite-plugin-pug',
|
|
|
|
// Трансформация 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');
|
|
}
|
|
|
|
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: [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]',
|
|
}
|
|
},
|
|
},
|
|
};
|
|
});
|