@@ -3,8 +3,7 @@ import { cwd } from 'node:process'
33import { createConfigLoader as createLoader } from 'unconfig'
44import { type ConfigEnv , type Plugin , type UserConfig , loadEnv , normalizePath } from 'vite'
55
6- import { initUi } from './ui.js'
7- import type { UI } from './ui.js'
6+ import { initUi , type UI } from './ui.js'
87import { zodValidation } from './validators/zod/index.js'
98import { builtinValidation } from './validators/builtin/index.js'
109import type { FullPluginOptions , PluginOptions , Schema } from './types.js'
@@ -14,12 +13,7 @@ import type { FullPluginOptions, PluginOptions, Schema } from './types.js'
1413 */
1514async function loadConfig ( rootDir : string ) {
1615 const loader = createLoader < PluginOptions > ( {
17- sources : [
18- {
19- files : 'env' ,
20- extensions : [ 'ts' , 'cts' , 'mts' , 'js' , 'cjs' , 'mjs' ] ,
21- } ,
22- ] ,
16+ sources : [ { files : 'env' , extensions : [ 'ts' , 'cts' , 'mts' , 'js' , 'cjs' , 'mjs' ] } ] ,
2317 cwd : rootDir ,
2418 } )
2519
@@ -107,16 +101,19 @@ async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options:
107101 const { schema, validator } = getNormalizedOptions ( options )
108102 const showDebug = shouldLogVariables ( options )
109103 const validate = { zod : zodValidation , builtin : builtinValidation } [ validator ]
104+
110105 try {
111106 const variables = await validate ( ui , env , schema as any )
107+
112108 if ( showDebug ) logVariables ( ui , variables )
109+
113110 return variables
114111 } catch ( error ) {
115- if ( showDebug )
116- logVariables (
117- ui ,
118- Object . entries ( schema ) . map ( ( [ key ] ) => ( { key , value : env [ key ] } ) ) ,
119- )
112+ if ( showDebug ) {
113+ const variables = Object . entries ( schema ) . map ( ( [ key ] ) => ( { key , value : env [ key ] } ) )
114+ logVariables ( ui , variables )
115+ }
116+
120117 throw error
121118 }
122119}
@@ -127,7 +124,7 @@ async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options:
127124export const ValidateEnv = ( options ?: PluginOptions ) : Plugin => {
128125 const ui = initUi ( )
129126 return {
130- // @ts -expect-error -- only used for testing as we need to keep each instance of the plugin unique to a test
127+ // @ts -expect-error - only used for testing as we need to keep each instance of the plugin unique to a test
131128 ui : process . env . NODE_ENV === 'testing' ? ui : undefined ,
132129 name : 'vite-plugin-validate-env' ,
133130 config : ( config , env ) => validateEnv ( ui , config , env , options ) ,
0 commit comments