Skip to content

Commit

Permalink
Refactor environment variable handling in vite config and main.jsx
Browse files Browse the repository at this point in the history
This commit updates the usage of environment variables in vite.config.js by switching to the 'loadEnv' method provided by 'vite'. In main.jsx, the log statement was updated for clarity. These changes improve code readability and maintainability.
  • Loading branch information
THORSTEN SCHMINKEL committed Feb 12, 2024
1 parent 871727e commit 32c69d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'

console.log("#### (main.jsx) all env: ", import.meta.env);
console.log("#### (main.jsx) import.meta.env: ", import.meta.env);

/**
* Renders the app
Expand Down
26 changes: 16 additions & 10 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {defineConfig, loadEnv} from 'vite'
import react from '@vitejs/plugin-react'

const env = loadEnv('', process.cwd(), '')
console.log("### (vite.config.js) all env: ", env);
console.log("### (vite.config.js) VITE_BASE_PATH: ", env.VITE_BASE_PATH);
export default ({ mode }) => {
// Load app-level env vars to node-level env vars.
process.env = {...process.env, ...loadEnv(mode, process.cwd())};
console.log("### (vite.config.js) process.env: ", process.env)

export default defineConfig({
base: env.VITE_BASE_PATH,
plugins: [react()],
define: {
__VITE_BASE_PATH__: JSON.stringify(env.VITE_BASE_PATH),
return defineConfig({
// To access env vars here use process.env.TEST_VAR
base: process.env.VITE_BASE_PATH,
plugins: [react()],
});
}

}
})
// export default defineConfig({
// base: env.VITE_BASE_PATH,
// plugins: [react()],
// process.env:
// __VITE_BASE_PATH__: JSON.stringify(env.VITE_BASE_PATH),
// })

// https://vitejs.dev/config/
// export default defineConfig(({ command, mode }) => {
Expand Down

0 comments on commit 32c69d6

Please sign in to comment.