Skip to content

Commit

Permalink
feat: simplify environment variable injection
Browse files Browse the repository at this point in the history
  • Loading branch information
TinsFox committed Sep 11, 2024
1 parent 4aa380c commit 8b9ffa0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
20 changes: 4 additions & 16 deletions env.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import { createEnv } from "@t3-oss/env-core"
import { z } from "zod"

const isDev =
"process" in globalThis ?
process.env.NODE_ENV === "development" :
import.meta.env.DEV
const isDev = import.meta.env.DEV

export const env = createEnv({
clientPrefix: "VITE_",
client: {
VITE_APP_NAME: z.string(),
VITE_API_URL: z.string().url(),
VITE_ENABLE_DEVTOOLS: z.boolean().default(false),
VITE_ENABLE_DEVTOOLS: z.string().default("false").transform((s) => s !== "false" && s !== "0"),
VITE_EDITOR: z.string().default("vscode"),
VITE_ENABLE_MOCK: z.boolean().default(isDev),
VITE_ENABLE_MOCK: z.string().default(isDev.toString()).transform((s) => s !== "false" && s !== "0"),
},
emptyStringAsUndefined: true,
runtimeEnv:
"process" in globalThis ?
process.env :
{
...import.meta.env,
VITE_ENABLE_DEVTOOLS:
import.meta.env.VITE_ENABLE_DEVTOOLS === "true" ? true : false,
VITE_ENABLE_MOCK:
import.meta.env.VITE_ENABLE_MOCK === "true" ? true : false,
},
runtimeEnv: import.meta.env,
skipValidation: !isDev,
})
28 changes: 11 additions & 17 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ const readme = readFileSync("README.md", "utf8")
export default defineConfig(({ mode }) => {
const viteEnv = loadEnv(mode, process.cwd(), "")

const define = {
APP_VERSION: JSON.stringify(pkg.version),
APP_NAME: JSON.stringify(pkg.name),
APP_DEV_CWD: JSON.stringify(process.cwd()),
GIT_COMMIT_SHA: JSON.stringify(
process.env.VERCEL_GIT_COMMIT_SHA || getGitHash(),
),
DEBUG: process.env.DEBUG === "true",
dependencies: JSON.stringify(pkg.dependencies),
devDependencies: JSON.stringify(pkg.devDependencies),
README: JSON.stringify(readme),
pkg: JSON.stringify(pkg),
}

return {
plugins: [
tsconfigPaths(),
Expand All @@ -40,11 +26,19 @@ export default defineConfig(({ mode }) => {
},
}),
],
define,
define: {
APP_VERSION: JSON.stringify(pkg.version),
APP_NAME: JSON.stringify(pkg.name),
APP_DEV_CWD: JSON.stringify(process.cwd()),
GIT_COMMIT_SHA: JSON.stringify(getGitHash()),
dependencies: JSON.stringify(pkg.dependencies),
devDependencies: JSON.stringify(pkg.devDependencies),
README: JSON.stringify(readme),
pkg: JSON.stringify(pkg),
},
server: {
port: 3000,
proxy: {
"/^api/": {
"/api/": {
target: viteEnv.VITE_API_URL,
changeOrigin: true,
},
Expand Down

0 comments on commit 8b9ffa0

Please sign in to comment.