-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Doc on custom config file #23
Comments
Hey! I rushed this feature a bit. Happy to accept a PR |
EDIT: It seems I misconfigured the plugin — you need to specify In my case, I configured Maybe it's worth considering removing the non- Regarding invalid diff --git a/src/index.ts b/src/index.ts
index 4cbabdb..effe68d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -8,49 +8,72 @@ import { zodValidation } from './validators/zod/index.js'
import { builtinValidation } from './validators/builtin/index.js'
import type { FullPluginOptions, PluginOptions, Schema } from './types.js'
+function getConfigFile(options: PluginOptions | undefined) {
+ if (options && 'configFile' in options && options.configFile) {
+ return {
+ name: options.configFile,
+ isDefault: false,
+ }
+ }
+
+ return {
+ name: 'env',
+ isDefault: true,
+ }
+}
+
/**
* Load schema defined in `env.ts` file using unconfig
*/
async function loadOptions(rootDir: string, inlineConfig?: PluginOptions) {
- let source = 'env'
-
/**
* If configFile is defined in the inlineConfig, use it as the source
*/
- if (inlineConfig && 'configFile' in inlineConfig && inlineConfig.configFile) {
- source = inlineConfig.configFile
- }
+ const configFile = getConfigFile(inlineConfig)
const loader = createLoader<PluginOptions>({
- sources: [{ files: source, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
+ sources: [{ files: configFile.name, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
cwd: rootDir,
defaults: inlineConfig,
})
const result = await loader.load()
+
+ if (!configFile.isDefault && !result.sources.length) {
-
/**
* If configFile is defined in the inlineConfig, use it as the source
*/
- if (inlineConfig && 'configFile' in inlineConfig && inlineConfig.configFile) {
- source = inlineConfig.configFile
- }
+ const configFile = getConfigFile(inlineConfig)
const loader = createLoader<PluginOptions>({
- sources: [{ files: source, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
+ sources: [{ files: configFile.name, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
cwd: rootDir,
defaults: inlineConfig,
})
const result = await loader.load()
+
+ if (!configFile.isDefault && !result.sources.length) {
+ throw new Error(`Missing configuration for vite-plugin-validate-env: ${configFile.name}`)
+ }
+
const config = result.config
- if (!config) throw new Error('Missing configuration for vite-plugin-validate-env')
+ if (!config) {
+ throw new Error('Missing configuration for vite-plugin-validate-env')
+ } |
I have the same issue. How did you workaround it? |
Hi,
I have try the new feature to add a custom file.
The doc indicate
ValidateEnv({ envFile: 'config/env' }
when it is actually
ValidateEnv({ configFile: 'config/env' })
.Plus when the config file is not found, we get this error :
Which state that
configFIle
is an expected env variable, not an options for the env path.I would expect something like this instead :
The text was updated successfully, but these errors were encountered: