Skip to content

Commit 6c05af1

Browse files
committed
chore: handle env vars for server side
1 parent fc66400 commit 6c05af1

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

package-lock.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@types/react": "^18.0.26",
2929
"@types/react-dom": "^18.0.9",
3030
"@vitejs/plugin-react": "^3.0.0",
31+
"dotenv": "^16.0.3",
3132
"express": "^4.18.2",
3233
"glob": "^8.0.3",
3334
"nodemon": "^2.0.20",

vite.config.api.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { fileURLToPath } from "node:url";
22
import path from "node:path";
33
import fs from "node:fs";
44
import glob from "glob";
5+
import dotenv from "dotenv";
56
import { build } from "vite";
67

8+
dotenv.config();
79
const __dirname = path.dirname(fileURLToPath(import.meta.url));
810

911
const files = glob
@@ -39,6 +41,15 @@ files.forEach(async (file: { entry: string; distFileName: string }) => {
3941
],
4042
extensions: [".ts", ".tsx"],
4143
},
44+
define: {
45+
...Object.keys(process.env).reduce(
46+
(obj: Record<string, string>, key: string) => {
47+
obj[`process.env.${key}`] = JSON.stringify(process.env[key]);
48+
return obj;
49+
},
50+
{}
51+
),
52+
},
4253
build: {
4354
ssr: true,
4455
copyPublicDir: false,

vite.config.ssr.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { defineConfig } from "vite";
2-
import { viteStaticCopy } from "vite-plugin-static-copy";
3-
import { fileURLToPath } from "node:url";
41
import path from "node:path";
52
import fs from "node:fs";
3+
import { fileURLToPath } from "node:url";
4+
import { defineConfig } from "vite";
5+
import { viteStaticCopy } from "vite-plugin-static-copy";
66
import react from "@vitejs/plugin-react";
7+
import dotenv from "dotenv";
78

9+
dotenv.config();
810
process.env.NODE_ENV = "production";
911
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1012

@@ -43,6 +45,15 @@ export default defineConfig({
4345
},
4446
},
4547
},
48+
define: {
49+
...Object.keys(process.env).reduce(
50+
(obj: Record<string, string>, key: string) => {
51+
obj[`process.env.${key}`] = JSON.stringify(process.env[key]);
52+
return obj;
53+
},
54+
{}
55+
),
56+
},
4657
plugins: [
4758
react(),
4859
viteStaticCopy({

0 commit comments

Comments
 (0)