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: {}
}
};
});
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With