From 2371c1c092b993004d12f7642ad5500fc234eee0 Mon Sep 17 00:00:00 2001 From: Nicolas Martinez <17040442+nicomt@users.noreply.github.com> Date: Thu, 6 Jun 2024 16:02:16 -0400 Subject: [PATCH 1/3] Filter environmet variables to prevent unintended leakage of environment variables. --- vite.config.mjs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/vite.config.mjs b/vite.config.mjs index f5cdf664..573cc8fa 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -5,8 +5,15 @@ import autoprefixer from 'autoprefixer' export default defineConfig(({ mode }) => { // Load .env - const env = loadEnv(mode, process.cwd(), '') - process.env = { ...process.env, ...env } + const viteEnv = loadEnv(mode, process.cwd(), '') + const env = {} + + // Filter env to variables starting with VITE_APP or VUE_APP + Object.keys({...process.env, ...viteEnv}).forEach(key => { + if (key.startsWith('VITE_APP') || key.startsWith('VUE_APP')) { + env[key] = viteEnv[key] + } + }) return { plugins: [vue()], @@ -53,7 +60,7 @@ export default defineConfig(({ mode }) => { }, define: { // vitejs does not support process.env so we have to redefine it - 'process.env': process.env, + 'process.env': env, }, } }) From 6b9c7c6ca8a88dda26469338988b217575c4fdcb Mon Sep 17 00:00:00 2001 From: Nicolas Martinez <17040442+nicomt@users.noreply.github.com> Date: Thu, 6 Jun 2024 16:12:25 -0400 Subject: [PATCH 2/3] Fix for loading data from both viteEnv and process.env --- vite.config.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vite.config.mjs b/vite.config.mjs index 573cc8fa..81ec0d8e 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -9,9 +9,9 @@ export default defineConfig(({ mode }) => { const env = {} // Filter env to variables starting with VITE_APP or VUE_APP - Object.keys({...process.env, ...viteEnv}).forEach(key => { + Object.entries({...process.env, ...viteEnv}).forEach(([key, value]) => { if (key.startsWith('VITE_APP') || key.startsWith('VUE_APP')) { - env[key] = viteEnv[key] + env[key] = value } }) From dfa4381ce13433a7b48056ecfe7b204755665581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Holeczek?= Date: Fri, 14 Jun 2024 13:10:02 +0200 Subject: [PATCH 3/3] Update vite.config.mjs --- vite.config.mjs | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/vite.config.mjs b/vite.config.mjs index 81ec0d8e..7195c8b7 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -1,29 +1,18 @@ -import { defineConfig, loadEnv } from 'vite' +import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'node:path' import autoprefixer from 'autoprefixer' -export default defineConfig(({ mode }) => { - // Load .env - const viteEnv = loadEnv(mode, process.cwd(), '') - const env = {} - - // Filter env to variables starting with VITE_APP or VUE_APP - Object.entries({...process.env, ...viteEnv}).forEach(([key, value]) => { - if (key.startsWith('VITE_APP') || key.startsWith('VUE_APP')) { - env[key] = value - } - }) - +export default defineConfig(() => { return { plugins: [vue()], base: './', css: { postcss: { plugins: [ - autoprefixer({}) // add options if needed + autoprefixer({}), // add options if needed ], - } + }, }, resolve: { alias: [ @@ -41,16 +30,7 @@ export default defineConfig(({ mode }) => { replacement: path.resolve(__dirname, '/src'), }, ], - extensions: [ - '.mjs', - '.js', - '.ts', - '.jsx', - '.tsx', - '.json', - '.vue', - '.scss', - ], + extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.scss'], }, server: { port: 3000, @@ -58,9 +38,5 @@ export default defineConfig(({ mode }) => { // https://vitejs.dev/config/server-options.html }, }, - define: { - // vitejs does not support process.env so we have to redefine it - 'process.env': env, - }, } })