Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure vite.config for production when using the 'server' field with HTTPS for development?

Tags:

vite

sveltekit

I enabled https in vite.config because I need it for development. But I don't know how to use this field in production since I'm going to use Nginx and let's encrypt. Do I need to check if I'm in development mode with an environment variable?

I'm using vite with SvelteKit.

Here's my vite.config

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, loadEnv } from 'vite';
import fs from 'fs';

export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), '');
    return {
        plugins: [sveltekit()],
        server: {
            https: {
                key: fs.readFileSync(env.HTTPS_KEY),
                cert: fs.readFileSync(env.HTTPS_CERTS)
            },
            proxy: {}
        }
    };
});
like image 548
mvannsec Avatar asked Jan 26 '26 09:01

mvannsec


2 Answers

If you are looking for production solution I'd suggest using a process manager like pm2

You can build a server using standard config and running vite build and configure pm2 with ecosystem file to use the compiled project

like image 198
abilaro Avatar answered Jan 29 '26 11:01

abilaro


Example on how to use env vars for future reference:

import { defineConfig, loadEnv } from "vite";      // <-- loadEnv!
import { sveltekit } from '@sveltejs/kit/vite';
import fs from 'fs';

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd(), "");

    const serverSettings = env.APP_ENV === "local" // <-- Condition here
        ? { server: { host: "localhost" } } 
        : {};

    return {
        plugins: [sveltekit()],
        ...serverSettings,                         // <-- Add here!
    };
});

Hope that helps!

like image 29
d0utone Avatar answered Jan 29 '26 13:01

d0utone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!