@veloss/vite-t3-env
v0.0.5
Published
vite-t3-env plugin for vite
Downloads
9
Maintainers
Readme
@veloss/vite-t3-env
vite 환경에서 환경변수를 t3-env와 쉽게 연동하고 타입을 자동으로 만들어주기 위한 vite plugins 입니다.
Concept
t3-env-pluginThis project was inspired by svelte-kit's approach to handling environment variables. It is a Vite plugin that seamlessly integrates environment variables with t3-env and automatically generates types.Define a t3 env function to validate client env and server env environment variables, and when this function is passed to t3EnvPlugin, environment variable validation is performed. After passing validation, environment variables are set in process.env and "$env/static/private"(server), import.meta.env and "$env/static/public"(client) modules. Type files are also automatically generated.
Usage
Install:
# npm
npm install @veloss/vite-t3-env
# yarn
yarn add @veloss/vite-t3-env
# pnpm
pnpm add @veloss/vite-t3-envImport:
// ESM / Typescript
import { t3EnvPlugin } from "@veloss/vite-t3-env";Examples
Simple example
// client.ts
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import type { RuntimeEnv } from "@veloss/vite-t3-env";
export const clientEnv = (
runtimeEnv: RuntimeEnv,
clientPrefix: string | undefined
) => {
return createEnv({
clientPrefix,
client: {
PUBLIC_SERVER_URL: z.string().url(),
},
runtimeEnv,
});
};
export type ClientEnv = ReturnType<typeof clientEnv>;// server.ts
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import type { RuntimeEnv } from "@veloss/vite-t3-env";
export const serverEnv = (runtimeEnv: RuntimeEnv) => {
return createEnv({
server: {
DATABASE_URL: z.string().url(),
NODE_ENV: z
.enum(["development", "production", "test"])
.default("development"),
SESSION_SECRET: z.string().min(32),
},
runtimeEnv,
});
};
export type ServerEnv = ReturnType<typeof serverEnv>;// vite.config.ts
import { defineConfig } from "vite";
import { t3EnvPlugin } from "@veloss/vite-t3-env";
import { type ServerEnv, serverEnv } from "./env/server";
import { type ClientEnv, clientEnv } from "./env/client";
export default defineConfig({
plugins: [
t3EnvPlugin<ClientEnv, ServerEnv>({
serverEnv,
clientEnv,
prefix: "PUBLIC_",
}),
// ...
],
});// global.d.ts
// @veloss/vite-t3-env will generate this file
// this file is generated — do not edit it
declare namespace NodeJS {
interface ProcessEnv {
[key: string]: string;
readonly DATABASE_URL: string;
readonly NODE_ENV: string;
readonly SESSION_SECRET: string;
}
}// vite-env.d.ts
// @veloss/vite-t3-env will generate this file
// this file is generated — do not edit it
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly PUBLIC_SERVER_URL: string;
}
declare module "$env/static/private" {
export const DATABASE_URL: string;
export const NODE_ENV: string;
export const SESSION_SECRET: string;
}
declare module "$env/static/public" {
export const PUBLIC_SERVER_URL: string;
}API
clientEnv
Type: (runtimeEnv: RuntimeEnv, clientPrefix?: string | undefined) => Readonly<ClientEnv>
Client environment function. This is used to create the client environment.
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import type { RuntimeEnv } from "@veloss/vite-t3-env";
const clientEnv = (
runtimeEnv: RuntimeEnv,
clientPrefix: string | undefined
) => {
return createEnv({
clientPrefix,
client: {
PUBLIC_SERVER_URL: z.string().url(),
},
runtimeEnv,
});
};
type ClientEnv = ReturnType<typeof clientEnv>;serverEnv
Type: (runtimeEnv: RuntimeEnv) => Readonly<ServerEnv> (optional)
Server environment function. This is used to create the server environment.
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import type { RuntimeEnv } from "@veloss/vite-t3-env";
const serverEnv = (runtimeEnv: RuntimeEnv) => {
return createEnv({
server: {
DATABASE_URL: z.string().url(),
NODE_ENV: z
.enum(["development", "production", "test"])
.default("development"),
SESSION_SECRET: z.string().min(32),
},
runtimeEnv,
});
};
type ServerEnv = ReturnType<typeof serverEnv>;envOptions
Type: EnvOptions (optional)
Environment options. This is used to configure the environment file.
export default defineConfig({
plugins: [
t3EnvPlugin<ClientEnv, ServerEnv>({
serverEnv,
clientEnv,
envOptions: {
envFile: ".env",
},
}),
// ...
],
});prefix
Type: Partial<Prefix> | string (optional)
Prefix for the environment variables. This is used to add a prefix to the environment
export default defineConfig({
plugins: [
t3EnvPlugin<ClientEnv, ServerEnv>({
serverEnv,
clientEnv,
prefix: "PUBLIC_",
}),
// ...
],
});
// or
export default defineConfig({
plugins: [
t3EnvPlugin<ClientEnv, ServerEnv>({
serverEnv,
clientEnv,
prefix: {
server: "PRIVATE_",
client: "PUBLIC_",
},
}),
// ...
],
});