import { defineConfig } from 'vite';
import fs from 'node:fs';
import path from 'node:path';
import { replaceData, isProd } from './vite/app.js';
import { base, build } from './vite/build.js';
import { absPath } from './vite/path.js';
import { server, preview } from './vite/server.js';
import css from './vite/css.js';
import htmlmin from './vite/htmlmin.js';
import htmlTransformBase from './vite/html-transform-base.js';
import imagemin from './vite/imagemin.js';
import twig from './vite/twig.js';
import vituum from './vite/vituum.js';

const exampleData = JSON.parse(fs.readFileSync(path.resolve(absPath.data, 'example.json'), 'utf8'));

const plugins = [
  vituum(),
  twig({
    globals: {
      example: exampleData,
    },
  }),
];

if (isProd) {
  plugins.push(imagemin());
  plugins.push(htmlmin());
  plugins.push(htmlTransformBase());
}

export default defineConfig({
  // APP CONFIG
  envPrefix: 'APP_',
  define: replaceData,
  css,
  plugins,
  resolve: {
    alias: {
      '@': absPath.src,
    },
  },

  // BUILD
  base,
  build,

  // SERVE
  server: {
    ...server,
    watch: {
      include: 'src/**/*.twig',
      exclude: 'node_modules/**',
    },
  },
  preview,
});
