|
1 | 1 | import path from 'node:path' |
2 | 2 | import { cwd } from 'node:process' |
| 3 | +import { type Plugin } from 'vite' |
3 | 4 | import { createConfigLoader as createLoader } from 'unconfig' |
4 | | -import { type ConfigEnv, type Plugin, type UserConfig } from 'vite' |
5 | 5 |
|
6 | 6 | import { initUi, type UI } from './ui.js' |
7 | 7 | import { builtinValidation } from './validators/builtin/index.js' |
8 | 8 | import { standardValidation } from './validators/standard/index.js' |
9 | | -import type { FullPluginOptions, PluginOptions, Schema } from './types.js' |
| 9 | +import type { ConfigOptions, FullPluginOptions, PluginOptions, Schema } from './types.js' |
10 | 10 |
|
11 | 11 | /** |
12 | 12 | * Load schema defined in `env.ts` file using unconfig |
@@ -71,42 +71,27 @@ function shouldLogVariables(options: PluginOptions) { |
71 | 71 | /** |
72 | 72 | * Main function. Will call each validator defined in the schema and throw an error if any of them fails. |
73 | 73 | */ |
74 | | -async function validateEnv( |
75 | | - ui: UI, |
76 | | - userConfig: UserConfig, |
77 | | - envConfig: ConfigEnv, |
78 | | - inlineOptions?: PluginOptions, |
79 | | -) { |
| 74 | +async function validateEnv(ui: UI, config: ConfigOptions, inlineOptions?: PluginOptions) { |
80 | 75 | /** |
81 | 76 | * Dynamic import of Vite helpers to using the ESM build of Vite and |
82 | 77 | * avoiding CJS since it will be deprecated |
83 | 78 | * See : https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated |
84 | 79 | */ |
85 | 80 | const { normalizePath, loadEnv } = await import('vite') |
86 | | - const rootDir = userConfig.root || cwd() |
| 81 | + const rootDir = config.root || cwd() |
87 | 82 |
|
88 | | - const resolvedRoot = normalizePath( |
89 | | - userConfig.root ? path.resolve(userConfig.root) : process.cwd(), |
90 | | - ) |
| 83 | + const resolvedRoot = normalizePath(config.root ? path.resolve(config.root) : process.cwd()) |
91 | 84 |
|
92 | | - const envDir = userConfig.envDir |
93 | | - ? normalizePath(path.resolve(resolvedRoot, userConfig.envDir)) |
| 85 | + const envDir = config.envDir |
| 86 | + ? normalizePath(path.resolve(resolvedRoot, config.envDir)) |
94 | 87 | : resolvedRoot |
95 | 88 |
|
96 | | - const env = loadEnv(envConfig.mode, envDir, userConfig.envPrefix) |
| 89 | + const env = loadEnv(config.mode, envDir, config.envPrefix) |
97 | 90 |
|
98 | 91 | const options = await loadOptions(rootDir, inlineOptions) |
99 | 92 | const variables = await validateAndLog(ui, env, options) |
100 | 93 |
|
101 | | - return { |
102 | | - define: variables.reduce( |
103 | | - (acc, { key, value }) => { |
104 | | - acc[`import.meta.env.${key}`] = JSON.stringify(value) |
105 | | - return acc |
106 | | - }, |
107 | | - {} as Record<string, unknown>, |
108 | | - ), |
109 | | - } |
| 94 | + return variables |
110 | 95 | } |
111 | 96 |
|
112 | 97 | async function validateAndLog(ui: UI, env: Record<string, string>, options: PluginOptions) { |
@@ -139,10 +124,32 @@ export const ValidateEnv = (options?: PluginOptions): Plugin => { |
139 | 124 | // @ts-expect-error - only used for testing as we need to keep each instance of the plugin unique to a test |
140 | 125 | ui: process.env.NODE_ENV === 'testing' ? ui : undefined, |
141 | 126 | name: 'vite-plugin-validate-env', |
142 | | - config: (config, env) => validateEnv(ui, config, env, options), |
| 127 | + config: async ({ envDir, envPrefix, root }, { mode }) => { |
| 128 | + const env = await validateEnv(ui, { envDir, envPrefix, root, mode }, options) |
| 129 | + const define = Object.fromEntries( |
| 130 | + env.map(({ key, value }) => [`import.meta.env.${key}`, JSON.stringify(value)]), |
| 131 | + ) |
| 132 | + |
| 133 | + return { define } |
| 134 | + }, |
143 | 135 | } |
144 | 136 | } |
145 | 137 |
|
| 138 | +/** |
| 139 | + * Validate environment variables and load them inside `process.env` |
| 140 | + * Can be useful when you want to validate outside of Vite's build process. |
| 141 | + */ |
| 142 | +export const loadAndValidateEnv = async (config: ConfigOptions, options?: PluginOptions) => { |
| 143 | + const ui = initUi() |
| 144 | + const variables = await validateEnv(ui, config, options) |
| 145 | + |
| 146 | + for (const { key, value } of variables) { |
| 147 | + process.env[key] = value |
| 148 | + } |
| 149 | + |
| 150 | + return Object.fromEntries(variables.map(({ key, value }) => [key, value])) |
| 151 | +} |
| 152 | + |
146 | 153 | export const defineConfig = <T extends PluginOptions>(config: T): T => config |
147 | 154 |
|
148 | 155 | export { schema as Schema } from '@poppinss/validator-lite' |
|
0 commit comments