Skip to content

Commit 402a6b3

Browse files
Julien-R44reslear
andauthored
fix: custom env prefixes (#3)
Co-authored-by: reslear <[email protected]>
1 parent 809081b commit 402a6b3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function getNormalizedOptions(options: PluginOptions) {
7979
*/
8080
async function validateEnv(userConfig: UserConfig, envConfig: ConfigEnv, options?: PluginOptions) {
8181
const rootDir = userConfig.root || cwd()
82-
const env = loadEnv(envConfig.mode, rootDir)
82+
const env = loadEnv(envConfig.mode, rootDir, userConfig.envPrefix)
8383

8484
const isInlineConfig = options !== undefined
8585
if (!isInlineConfig) {

tests/common.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ test.group('vite-plugin-validate-env', (group) => {
104104
const fn = plugin.config!.bind(plugin, viteConfig, viteEnvConfig)
105105
await assert.rejects(fn, 'Missing configuration for vite-plugin-validate-env')
106106
})
107+
108+
test('Should pick up var with custom prefix', async ({ assert }) => {
109+
const plugin = ValidateEnv({
110+
CUSTOM_TEST: Schema.boolean(),
111+
})
112+
113+
await fs.add(`.env.development`, `CUSTOM_TEST=not boolean`)
114+
115+
// @ts-expect-error - `config` is the handler
116+
const fn = plugin.config!.bind(plugin, { ...viteConfig, envPrefix: 'CUSTOM_' }, viteEnvConfig)
117+
await assert.rejects(
118+
fn,
119+
'E_INVALID_ENV_VALUE: Value for environment variable "CUSTOM_TEST" must be a boolean, instead received "not boolean"'
120+
)
121+
})
107122
})

0 commit comments

Comments
 (0)