fix: production crash due to undefined import.meta.env by adding env utility and updating build script

This commit is contained in:
bipproduction
2026-02-07 17:03:42 +08:00
parent 0dd2aa17d6
commit 60457b1f2f
6 changed files with 39 additions and 17 deletions

View File

@@ -11,7 +11,7 @@
"lint": "biome check .",
"check": "biome check --write .",
"format": "biome format --write .",
"build": "bun build ./src/index.html --outdir=dist --sourcemap --target=browser --minify --define:process.env.NODE_ENV='\"production\"' --env='BUN_PUBLIC_*'",
"build": "bun build ./src/index.html --outdir=dist --sourcemap --target=browser --minify --define:process.env.NODE_ENV='\"production\"' --env='VITE_*'",
"start": "NODE_ENV=production bun src/index.ts"
},
"dependencies": {

View File

@@ -13,6 +13,7 @@ import { Inspector } from "react-dev-inspector";
import { createRoot } from "react-dom/client";
import { routeTree } from "./routeTree.gen";
import { ModalsProvider } from "@mantine/modals";
import { VITE_PUBLIC_URL } from "./utils/env";
// Create a new router instance
export const router = createRouter({
@@ -39,11 +40,12 @@ const elem = document.getElementById("root")!;
const app = (
<InspectorWrapper
keys={["shift", "a"]}
onClickElement={(e) => {
if (!e.codeInfo) return;
const url = import.meta.env.VITE_PUBLIC_URL;
fetch(`${url}/__open-in-editor`, {
onClickElement={(e) => {
if (!e.codeInfo) return;
const url = VITE_PUBLIC_URL;
fetch(`${url}/__open-in-editor`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({

View File

@@ -15,10 +15,8 @@ const api = new Elysia({
prefix: "/api",
})
.all("/auth/*", ({ request }) => auth.handler(request))
.use(cors());
if (!isProduction) {
api.use(
.use(cors())
.use(
swagger({
path: "/docs",
documentation: {
@@ -28,10 +26,7 @@ if (!isProduction) {
},
},
}),
);
}
api
)
.get("/session", async ({ request }) => {
const data = await auth.api.getSession({ headers: request.headers });
return { data };
@@ -191,4 +186,4 @@ console.log(
`🚀 Server running at http://localhost:3000 in ${isProduction ? "production" : "development"} mode`,
);
export type ApiApp = typeof app;
export type ApiApp = typeof app;

View File

@@ -1,4 +1,5 @@
import { redirect } from "@tanstack/react-router";
import { VITE_PUBLIC_URL } from "../utils/env";
/* ================================
* Types
@@ -21,7 +22,7 @@ type SessionResponse = {
async function fetchSession(): Promise<SessionResponse | null> {
try {
const baseURL = import.meta.env.VITE_PUBLIC_URL || window.location.origin;
const baseURL = VITE_PUBLIC_URL || window.location.origin;
const res = await fetch(`${baseURL}/api/session`, {
method: "GET",
credentials: "include",

View File

@@ -1,7 +1,8 @@
import { createAuthClient } from "better-auth/react";
import { VITE_PUBLIC_URL } from "./env";
export const authClient = createAuthClient({
baseURL: import.meta.env.VITE_PUBLIC_URL || "http://localhost:3000",
baseURL: VITE_PUBLIC_URL,
});
export const { useSession, signIn, signOut, signUp, getSession } = authClient;

23
src/utils/env.ts Normal file
View File

@@ -0,0 +1,23 @@
/**
* Safely access environment variables across different runtimes and builders.
* Supports Vite's import.meta.env and Bun's process.env (used in bun build).
*/
export const getEnv = (key: string, defaultValue = ""): string => {
// 1. Try Vite's import.meta.env
try {
if (typeof import.meta.env !== "undefined" && import.meta.env[key]) {
return import.meta.env[key];
}
} catch {}
// 2. Try process.env (injected by bun build --env)
try {
if (typeof process !== "undefined" && process.env[key]) {
return process.env[key];
}
} catch {}
return defaultValue;
};
export const VITE_PUBLIC_URL = getEnv("VITE_PUBLIC_URL", "http://localhost:3000");