Skip to content

Commit

Permalink
Load vite app configurations from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pavinduLakshan committed Jan 16, 2025
1 parent 19a7c92 commit 648cb0a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
3 changes: 3 additions & 0 deletions samples/asgardeo-react-app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=3000
HOST="localhost"
HTTPS=true
66 changes: 39 additions & 27 deletions samples/asgardeo-react-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { defineConfig, loadEnv, UserConfig } from 'vite';
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';

export default defineConfig({
plugins: [
react()
],
optimizeDeps: {
include: [
'@asgardeo/auth-react',
'react/jsx-runtime',
'react-router',
'react-router-dom',
'react',
],
},
server: {
https: {
key: fs.readFileSync(path.resolve(__dirname, 'public', 'cert', 'localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'public', 'cert', 'localhost-cert.pem')),
export default defineConfig(({ mode }) => {

const env = loadEnv(mode, process.cwd(), '')

// Explicitly define the configuration type
const config: UserConfig = {
plugins: [react()],
optimizeDeps: {
include: [
'@asgardeo/auth-react',
'react/jsx-runtime',
'react-router',
'react-router-dom',
'react',
],
},
server: {
host: env.HOST,
port: parseInt(env.PORT || '3000', 10),
},
host: 'localhost',
port: 3000
},
build: {
commonjsOptions: {
include: [ /react/ ],
},
build: {
commonjsOptions: {
include: [/react/],
},
},
};

if (env.HTTPS === 'true') {
config.server = {
...config.server,
https: {
key: fs.readFileSync(path.resolve(__dirname, 'public', 'cert', 'localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'public', 'cert', 'localhost-cert.pem')),
},
};
}
})

return config;
});

0 comments on commit 648cb0a

Please sign in to comment.