Skip to content

Commit fb01c85

Browse files
authored
Fix.env file (#649)
2 parents 8279f66 + d08d182 commit fb01c85

File tree

3 files changed

+55
-118
lines changed

3 files changed

+55
-118
lines changed
Lines changed: 9 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
interface Environment {
1+
// packages/common/src/ENVIRONMENT_VARS.ts
2+
export interface Environment {
23
/**
34
* URL to the remote couchDB instance that the app connects to.
45
*
@@ -29,7 +30,6 @@ interface Environment {
2930

3031
type ProtocolString = 'http' | 'https';
3132

32-
// Create a global reference to ensure singleton behavior
3333
declare global {
3434
interface Window {
3535
__SKUILDER_ENV__: Environment | undefined;
@@ -38,129 +38,21 @@ declare global {
3838

3939
// Default fallback values if all else fails
4040
const defaultEnv: Environment = {
41-
COUCHDB_SERVER_URL: 'localhost:5984/',
41+
COUCHDB_SERVER_URL: 'default:5984/',
4242
COUCHDB_SERVER_PROTOCOL: 'http',
4343
EXPRESS_SERVER_URL: 'localhost:3000/',
4444
EXPRESS_SERVER_PROTOCOL: 'http',
4545
DEBUG: false,
4646
MOCK: false,
4747
};
4848

49-
// Function to load environment variables from Vite
50-
function loadFromVite(): Partial<Environment> {
51-
const result: Partial<Environment> = {};
52-
53-
try {
54-
// Use typeof check to avoid issues in non-Vite environments
55-
if (typeof import.meta !== 'undefined' && 'env' in import.meta) {
56-
const env = (import.meta as any).env;
57-
58-
if (env.VITE_COUCHDB_SERVER) {
59-
result.COUCHDB_SERVER_URL = env.VITE_COUCHDB_SERVER;
60-
}
61-
62-
if (env.VITE_COUCHDB_PROTOCOL) {
63-
result.COUCHDB_SERVER_PROTOCOL = env.VITE_COUCHDB_PROTOCOL as ProtocolString;
64-
}
65-
66-
if (env.VITE_EXPRESS_SERVER) {
67-
result.EXPRESS_SERVER_URL = env.VITE_EXPRESS_SERVER;
68-
}
69-
70-
if (env.VITE_EXPRESS_PROTOCOL) {
71-
result.EXPRESS_SERVER_PROTOCOL = env.VITE_EXPRESS_PROTOCOL as ProtocolString;
72-
}
73-
74-
if (env.VITE_DEBUG !== undefined) {
75-
result.DEBUG = env.VITE_DEBUG === 'true';
76-
}
77-
78-
if (env.VITE_MOCK !== undefined) {
79-
result.MOCK = env.VITE_MOCK === 'true';
80-
}
81-
}
82-
} catch (e) {
83-
console.warn('Unable to load environment variables from Vite:', e);
84-
}
85-
86-
return result;
87-
}
88-
89-
// Function to validate if environment is properly loaded
90-
function isValidEnv(env: Environment): boolean {
91-
return Boolean(env.COUCHDB_SERVER_URL && env.EXPRESS_SERVER_URL);
92-
}
93-
94-
// Initialize ENV with a blocking retry mechanism if needed
95-
let ENV: Environment;
96-
97-
// First check if we have a global instance already
98-
if (
99-
typeof window !== 'undefined' &&
100-
window.__SKUILDER_ENV__ &&
101-
isValidEnv(window.__SKUILDER_ENV__)
102-
) {
103-
console.log('Using existing ENV from global scope');
104-
ENV = window.__SKUILDER_ENV__;
105-
} else {
106-
// Try loading with a blocking retry if in browser environment
107-
if (typeof window !== 'undefined') {
108-
let viteEnv = loadFromVite();
109-
110-
// If initial load didn't succeed, do a blocking retry
111-
if (!viteEnv.COUCHDB_SERVER_URL || !viteEnv.EXPRESS_SERVER_URL) {
112-
console.warn('Initial ENV load incomplete, performing blocking retries...');
113-
114-
// Synchronous retries with exponential backoff
115-
const maxRetries = 5;
116-
let retryCount = 0;
117-
let baseDelay = 20; // ms
118-
119-
while (
120-
retryCount < maxRetries &&
121-
(!viteEnv.COUCHDB_SERVER_URL || !viteEnv.EXPRESS_SERVER_URL)
122-
) {
123-
// Create a delay using a synchronous approach
124-
const delay = baseDelay * Math.pow(2, retryCount);
125-
const startTime = Date.now();
126-
while (Date.now() - startTime < delay) {
127-
// Empty blocking loop
128-
}
129-
130-
retryCount++;
131-
console.log(`Retry ${retryCount}/${maxRetries} for ENV initialization...`);
132-
viteEnv = loadFromVite();
133-
}
134-
135-
if (!viteEnv.COUCHDB_SERVER_URL || !viteEnv.EXPRESS_SERVER_URL) {
136-
console.error('ENV initialization failed after retries, using defaults');
137-
// Use default values if all retries fail
138-
ENV = { ...defaultEnv };
139-
} else {
140-
console.log('ENV successfully initialized after retries');
141-
ENV = { ...defaultEnv, ...viteEnv };
142-
}
143-
} else {
144-
ENV = { ...defaultEnv, ...viteEnv };
145-
}
146-
147-
// Store in global scope
148-
window.__SKUILDER_ENV__ = ENV;
149-
console.log('ENV initialized and stored in global scope');
150-
} else {
151-
// Node.js environment (SSR or build)
152-
const viteEnv = loadFromVite();
153-
ENV = { ...defaultEnv, ...viteEnv };
154-
}
155-
}
49+
// Get environment from global or default
50+
const ENV: Environment =
51+
typeof window !== 'undefined' && window.__SKUILDER_ENV__
52+
? { ...defaultEnv, ...window.__SKUILDER_ENV__ }
53+
: defaultEnv;
15654

15755
// Log the initialized environment
158-
console.log(`ENV init:`);
159-
console.log(` COUCHDB_SERVER_URL: ${ENV.COUCHDB_SERVER_URL}`);
160-
console.log(` COUCHDB_SERVER_PROTOCOL: ${ENV.COUCHDB_SERVER_PROTOCOL}`);
161-
console.log(` EXPRESS_SERVER_URL: ${ENV.EXPRESS_SERVER_URL}`);
162-
console.log(` EXPRESS_SERVER_PROTOCOL: ${ENV.EXPRESS_SERVER_PROTOCOL}`);
163-
console.log(` DEBUG: ${ENV.DEBUG}`);
164-
console.log(` MOCK: ${ENV.MOCK}`);
56+
console.log('ENV initialized:', ENV);
16557

16658
export default ENV;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// packages/platform-ui/vite-env-plugin.js
2+
export default function injectEnvPlugin() {
3+
// Store the config for use in transformIndexHtml
4+
let configEnv = {};
5+
6+
return {
7+
name: 'inject-env',
8+
configResolved(config) {
9+
// Store the resolved config for use in transformIndexHtml
10+
configEnv = config.env;
11+
console.log('Vite config ENV stored:', configEnv);
12+
},
13+
transformIndexHtml(html) {
14+
console.log('Transform HTML running with configEnv:', configEnv);
15+
16+
// Extract relevant environment variables
17+
const envVars = {
18+
COUCHDB_SERVER_URL: configEnv.VITE_COUCHDB_SERVER || 'injected-server:5984/',
19+
COUCHDB_SERVER_PROTOCOL: configEnv.VITE_COUCHDB_PROTOCOL || 'http',
20+
EXPRESS_SERVER_URL: configEnv.VITE_EXPRESS_SERVER || 'injected-express:3000/',
21+
EXPRESS_SERVER_PROTOCOL: configEnv.VITE_EXPRESS_PROTOCOL || 'http',
22+
DEBUG: configEnv.VITE_DEBUG === 'true',
23+
MOCK: configEnv.VITE_MOCK === 'true',
24+
};
25+
26+
console.log('Injecting ENV vars:', envVars);
27+
28+
// Create the script tag to inject at build time
29+
return {
30+
html,
31+
tags: [
32+
{
33+
tag: 'script',
34+
attrs: { type: 'text/javascript' },
35+
children: `console.log("ENV injection running"); window.__SKUILDER_ENV__ = ${JSON.stringify(
36+
envVars
37+
)};`,
38+
injectTo: 'head-prepend',
39+
},
40+
],
41+
};
42+
},
43+
};
44+
}

packages/platform-ui/vite.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import vue from '@vitejs/plugin-vue';
33
import { VitePWA } from 'vite-plugin-pwa';
44
import eslint from 'vite-plugin-eslint';
55
import { fileURLToPath, URL } from 'node:url';
6-
import { resolve } from 'path';
6+
import injectEnvPlugin from './vite-env-plugin';
77

88
// https://vitejs.dev/config/
99
export default defineConfig({
@@ -21,6 +21,7 @@ export default defineConfig({
2121
'process.version': JSON.stringify(process.version),
2222
},
2323
plugins: [
24+
injectEnvPlugin(),
2425
vue(),
2526
VitePWA({
2627
registerType: 'autoUpdate',

0 commit comments

Comments
 (0)