Skip to content
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

Open
pewulfman opened this issue Feb 11, 2024 · 2 comments
Open

Fix: Doc on custom config file #23

pewulfman opened this issue Feb 11, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@pewulfman
Copy link

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 :

$ vite dev
The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
error when starting dev server:
Error: Failed to validate environment variables : 

[configFile]: 
  validator is not a function 

Which state that configFIle is an expected env variable, not an options for the env path.

I would expect something like this instead :

Error : `config/env` file not found
@Julien-R44
Copy link
Owner

Hey! I rushed this feature a bit. Happy to accept a PR
I fixed the readme

@Julien-R44 Julien-R44 added the enhancement New feature or request label Feb 18, 2024
@biesbjerg
Copy link

biesbjerg commented Mar 6, 2024

I had issues with the plugin trying to validate debug and configFile as env vars when passed as options — this PR takes care to remove the keys when normalizing the options:
#24

EDIT: It seems I misconfigured the plugin — you need to specify schema key AND validator or everything will be threated as a schema.

In my case, I configured configFile in vite.config.ts and schema in defineConfig in my-env.ts.

Maybe it's worth considering removing the non-FullPluginOptions and require the schema-key to avoid any confusion?


Regarding invalid configFile paths, I'm not sure what the best way is to handle it, but maybe something along the lines of this?

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')
+  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants