UNPKG

@neondatabase/vite-plugin-postgres

Version:

This Vite plugin instantly provisions a Postgres instance (via Neon) and injects the connection string into your `.env` file, so you can start developing immediately.

62 lines (59 loc) 1.8 kB
import { note, intro, outro } from '@clack/prompts'; import { instantNeon } from 'neondb/launchpad'; import { resolve } from 'path'; import { loadEnv } from 'vite'; const DEFAULTS = { dotEnvFile: ".env", dotEnvKey: "DATABASE_URL", referrer: "unknown", seed: void 0 }; let claimProcessStarted = false; function postgresPlugin(options) { const { dotEnvFile: envPath, dotEnvKey: envKey, referrer, seed } = { ...DEFAULTS, ...options }; return { name: "@neondatabase/vite-plugin-postgres", enforce: "pre", async config({ root, envDir }, { mode }) { if (mode === "production" || claimProcessStarted) return; const resolvedRoot = resolve(root ?? process.cwd()); envDir = envDir ? resolve(resolvedRoot, envDir) : resolvedRoot; const resolvedEnvPath = resolve( envDir, envPath || DEFAULTS.dotEnvFile ); const envVars = loadEnv(mode, envDir, envKey); if (Object.keys(envVars).length > 0) { const envVar = envVars[envKey]; if (!envVar) { note( `Environment variable ${envKey} not found in ${resolvedEnvPath}. We will create one for you.` ); } else { note( `Environment variable ${envKey} found in ${resolvedEnvPath}. If you wish to create a new Neon database, please remove the existing variable.` ); return; } } claimProcessStarted = true; intro("Setting up your project with a Neon database."); await instantNeon({ dotEnvFile: envPath, dotEnvKey: envKey, referrer: `npm:@neondatabase/vite-plugin-postgres|${referrer}`, seed }); outro("Neon database created successfully."); } }; } export { postgresPlugin as default };