/**
 * @file Configuration for vite
 */
const path = require('path');
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';

export default defineConfig({
    /***
     * Project root directory (where index.html is located). Can be an absolute path, or a path
     * relative to the location of the config file itself.
     *
     * @type {String}
     * @defualt [process.cwd()]
     *
     * @see {@link https://vitejs.dev/config/#root}
     */
    root: process.cwd(),

    /**
     * Base public path when served in development or production. Valid values include:
     *
     * @type {String}
     * @default ['/']
     *
     * @see {@link https://vitejs.dev/config/#base}
     */
    base: '/',

    /**
     * Specifying this in config will override the default mode for both serve and build. This
     * value can also be overridden via the command line --mode option.
     *
     * @type {String}
     * @default ['development']
     *
     * @see {@link https://vitejs.dev/config/#mode}
     */
    mode: 'development',

    /**
     * Array of plugins to use. Falsy plugins are ignored and arrays of plugins are flattened.
     * See Plugin API for more details on Vite plugins.
     *
     * @type {Array}
     *
     * @see {@link https://vitejs.dev/config/#plugins}
     */
    plugins: [solidPlugin()],

    /**
     * Directory to serve as plain static assets. Files in this directory are served at / during
     * dev and copied to the root of outDir during build, and are always served or copied as-is
     * without transform. The value can be either an absolute file system path or a path relative
     * to project root.
     *
     * @type {String | false}
     * @default ['public']
     *
     * @see {@link https://vitejs.dev/config/#publicdir}
     */
    publicDir: 'public',

    /**
     * Directory to save cache files. Files in this directory are pre-bundled deps or some other
     * cache files generated by vite, which can improve the performance. You can use --force flag
     * or manually delete the directory to regenerate the cache files. The value can be either an
     * absolute file system path or a path relative to project root.
     *
     * @type {String}
     * @default ['node_modules/.vite']
     *
     * @see {@link https://vitejs.dev/config/#cachedir}
     */
    cacheDir: 'node_modules/.vite',

    resolve: {
        /**
         * When aliasing to file system paths, always use absolute paths. Relative alias values
         * will be used as-is and will not be resolved into file system paths.
         *
         * @type {Object | Array}
         *
         * @see {@link https://vitejs.dev/config/#resolve-alias}
         */
        alias: {
            '@': path.join(__dirname, './src')
        }
    },

    css: {
        /**
         * Inline PostCSS config (expects the same format as postcss.config.js), or a custom path
         * to search PostCSS config from (default is project root). The search is done using
         * postcss-load-config.
         *
         * Note if an inline config is provided, Vite will not search for other PostCSS config
         * sources.
         *
         * @type {String | (postcss.ProcessOptions & { plugins?: postcss.Plugin[] })}
         *
         * @see {@link https://vitejs.dev/config/#css-postcss}
         */
        postcss: '',

        /**
         * Specify options to pass to CSS pre-processors.
         *
         * @type {Object}
         *
         * @see {@link https://vitejs.dev/config/#css-preprocessoroptions}
         */
        preprocessorOptions: {}
    },

    /**
     * Specify additional picomatch patterns to be treated as static assets so that:
     *
     * - They will be excluded from the plugin transform pipeline when referenced from HTML or
     * directly requested over fetch or XHR.
     * - Importing them from JS will return their resolved URL string (this can be overwritten if
     * you have a enforce: 'pre' plugin to handle the asset type differently).
     *
     * @type {String | RegExp | (String | RegExp)[]}
     *
     * @see {@link https://vitejs.dev/config/#assetsinclude}
     */
    assetsInclude: ['**/.svg'],

    /**
     * Adjust console output verbosity. Default is 'info'.
     *
     * @type {String}
     * @default ['info']
     *
     * @see {@link https://vitejs.dev/config/#loglevel}
     */
    logLevel: 'info',

    /**
     * Set to false to prevent Vite from clearing the terminal screen when logging certain messages.
     * Via command line, use --clearScreen false.
     *
     * @type {Boolean}
     * @default [true]
     *
     * @see {@link https://vitejs.dev/config/#clearscreen}
     */
    clearScreen: true,

    /**
     * The directory from which .env files are loaded. Can be an absolute path, or a path relative
     * to the project root.
     *
     * @type {String}
     * @default ['root']
     *
     * @see {@link https://vitejs.dev/config/#envdir}
     */
    envDir: 'root',

    /**
     * Env variables starts with envPrefix will be exposed to your client source code via
     * import.meta.env.
     *
     * @type {String}
     * @default 'VITE_'
     *
     * @see {@link https://vitejs.dev/config/#envprefix}
     */
    envPrefix: 'VITE_',

    server: {
        /**
         * Specify which IP addresses the server should listen on. Set this to 0.0.0.0 or true to
         * listen on all addresses, including LAN and public addresses.
         *
         * @type {String | Boolean}
         * @default ['127.0.0.1']
         *
         * @see {@link https://vitejs.dev/config/#server-host}
         */
        host: 'localhost',

        /**
         * Specify server port. Note if the port is already being used, Vite will automatically
         * try the next available port so this may not be the actual port the server ends up
         * listening on.
         *
         * @type {Number}
         * @default [3000]
         *
         * @see {@link https://vitejs.dev/config/#server-port}
         */
        port: 3000,

        /**
         * Set to true to exit if port is already in use, instead of automatically try the next
         * available port.
         *
         * @type {Boolean}
         *
         * @see {@link https://vitejs.dev/config/#server-strictport}
         */
        strictPort: false,

        /**
         * Enable TLS + HTTP/2. Note this downgrades to TLS only when the server.proxy option is
         * also used.
         *
         * @type {Boolean}
         *
         * @see {@link https://vitejs.dev/config/#server-https}
         */
        https: false,

        /**
         * Automatically open the app in the browser on server start. When the value is a string,
         * it will be used as the URL's pathname. If you want to open the server in a specific
         * browser you like, you can set the env process.env.BROWSER (e.g. firefox).
         *
         * @type {Boolean}
         *
         * @see {@link https://vitejs.dev/config/#server-open}
         */
        open: true
    },

    build: {
        /**
         * Browser compatibility target for the final bundle
         *
         * @type {String | String[]}
         * @default ['modules']
         *
         * @see {@link https://vitejs.dev/config/#build-target}
         */
        target: 'esnext',

        /**
         * Specify the output directory (relative to project root).
         *
         * @type {String}
         * @default ['dist']
         *
         * @see {@link https://vitejs.dev/config/#build-outdir}
         */
        outDir: 'dist',

        /**
         * Specify the directory to nest generated assets under (relative to build.outDir).
         *
         * @type {String}
         * @default ['assets']
         *
         * @see {@link https://vitejs.dev/config/#build-assetsdir}
         */
        assetsDir: 'assets',

        /**
         * Imported or referenced assets that are smaller than this threshold will be inlined as
         * base64 URLs to avoid extra http requests. Set to 0 to disable inlining altogether.
         *
         * @type {Number}
         * @default ['4096']
         *
         * @see {@link https://vitejs.dev/config/#build-assetsinlinelimit}
         */
        assetsInlineLimit: 4096,

        /**
         * Enable/disable CSS code splitting. When enabled, CSS imported in async chunks will be
         * inlined into the async chunk itself and inserted when the chunk is loaded.
         *
         * @type {Boolean}
         * @default [true]
         *
         * @see {@link https://vitejs.dev/config/#build-csscodesplit}
         */
        cssCodeSplit: true,

        /**
         * Generate production source maps. If true, a separate sourcemap file will be created. If
         * 'inline', the sourcemap will be appended to the resulting output file as a data URI.
         * 'hidden' works like true except that the corresponding sourcemap comments in the bundled
         * files are suppressed.
         *
         * @type {Boolean | 'inline' | 'hidden'}
         * @default ['hidden']
         *
         * @see {@link https://vitejs.dev/config/#build-sourcemap}
         */
        sourcemap: 'hidden',

        /**
         * Set to false to disable minification, or specify the minifier to use. The default is
         * Esbuild which is 20 ~ 40x faster than terser and only 1 ~ 2% worse compression.
         *
         * @type {Boolean | 'terser' | 'esbuild'}
         * @default ['esbuild']
         *
         * @see {@link https://vitejs.dev/config/#build-minify}
         */
        minify: 'esbuild'
    }
});
