Skip to content

Commit 3dfe5da

Browse files
committed
add global pinning of ENV values...
fixing an issue where multiple bundles of `common` were executing with different injected env context from the parent `platform-ui` app
1 parent 67adaf2 commit 3dfe5da

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed

packages/common/src/ENVIRONMENT_VARS.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ interface Environment {
2929

3030
type ProtocolString = 'http' | 'https';
3131

32-
const ENV: Environment = {
32+
// Create a global reference to ensure singleton behavior
33+
declare global {
34+
interface Window {
35+
__SKUILDER_ENV__: Environment | undefined;
36+
}
37+
}
38+
let ENV: Environment = {
3339
COUCHDB_SERVER_URL: '',
3440
COUCHDB_SERVER_PROTOCOL: 'http',
3541
EXPRESS_SERVER_URL: '',
@@ -38,49 +44,62 @@ const ENV: Environment = {
3844
MOCK: false,
3945
};
4046

41-
// Try to load from Vite environment if available
42-
try {
43-
// Use typeof check to avoid issues in non-Vite environments
44-
if (typeof import.meta !== 'undefined' && 'env' in import.meta) {
45-
const env = (import.meta as any).env;
47+
if (typeof window !== 'undefined' && window.__SKUILDER_ENV__) {
48+
console.log('Using existing ENV from global scope');
49+
ENV = window.__SKUILDER_ENV__;
50+
} else {
51+
// Try to load from Vite environment if available
52+
try {
53+
// Use typeof check to avoid issues in non-Vite environments
54+
if (typeof import.meta !== 'undefined' && 'env' in import.meta) {
55+
const env = (import.meta as any).env;
4656

47-
if (env.VITE_COUCHDB_SERVER) {
48-
ENV.COUCHDB_SERVER_URL = env.VITE_COUCHDB_SERVER;
49-
}
57+
if (env.VITE_COUCHDB_SERVER) {
58+
ENV.COUCHDB_SERVER_URL = env.VITE_COUCHDB_SERVER;
59+
}
5060

51-
if (env.VITE_COUCHDB_PROTOCOL) {
52-
ENV.COUCHDB_SERVER_PROTOCOL = env.VITE_COUCHDB_PROTOCOL as ProtocolString;
53-
}
61+
if (env.VITE_COUCHDB_PROTOCOL) {
62+
ENV.COUCHDB_SERVER_PROTOCOL = env.VITE_COUCHDB_PROTOCOL as ProtocolString;
63+
}
5464

55-
if (env.VITE_EXPRESS_SERVER) {
56-
ENV.EXPRESS_SERVER_URL = env.VITE_EXPRESS_SERVER;
57-
}
65+
if (env.VITE_EXPRESS_SERVER) {
66+
ENV.EXPRESS_SERVER_URL = env.VITE_EXPRESS_SERVER;
67+
}
5868

59-
if (env.VITE_EXPRESS_PROTOCOL) {
60-
ENV.EXPRESS_SERVER_PROTOCOL = env.VITE_EXPRESS_PROTOCOL as ProtocolString;
61-
}
69+
if (env.VITE_EXPRESS_PROTOCOL) {
70+
ENV.EXPRESS_SERVER_PROTOCOL = env.VITE_EXPRESS_PROTOCOL as ProtocolString;
71+
}
6272

63-
if (env.VITE_DEBUG !== undefined) {
64-
ENV.DEBUG = env.VITE_DEBUG === 'true';
65-
}
73+
if (env.VITE_DEBUG !== undefined) {
74+
ENV.DEBUG = env.VITE_DEBUG === 'true';
75+
}
6676

67-
if (env.VITE_MOCK !== undefined) {
68-
ENV.MOCK = env.VITE_MOCK === 'true';
77+
if (env.VITE_MOCK !== undefined) {
78+
ENV.MOCK = env.VITE_MOCK === 'true';
79+
}
6980
}
81+
} catch (e) {
82+
console.warn('Unable to load environment variables from Vite:', e);
7083
}
71-
} catch (e) {
72-
console.warn('Unable to load environment variables from Vite:', e);
73-
}
84+
// Store in global scope if in browser environment
85+
if (typeof window !== 'undefined' && ENV.COUCHDB_SERVER_URL && ENV.EXPRESS_SERVER_URL) {
86+
window.__SKUILDER_ENV__ = ENV;
87+
console.log('ENV initialized and stored in global scope');
7488

75-
if (ENV.DEBUG) {
76-
console.log(`ENV init:`);
89+
console.log(`ENV init:`);
7790

78-
console.log(` COUCHDB_SERVER_URL: ${ENV.COUCHDB_SERVER_URL}`);
79-
console.log(` COUCHDB_SERVER_PROTOCOL: ${ENV.COUCHDB_SERVER_PROTOCOL}`);
80-
console.log(` EXPRESS_SERVER_URL: ${ENV.EXPRESS_SERVER_URL}`);
81-
console.log(` EXPRESS_SERVER_PROTOCOL: ${ENV.EXPRESS_SERVER_PROTOCOL}`);
82-
console.log(` DEBUG: ${ENV.DEBUG}`);
83-
console.log(` MOCK: ${ENV.MOCK}`);
91+
console.log(` COUCHDB_SERVER_URL: ${ENV.COUCHDB_SERVER_URL}`);
92+
console.log(` COUCHDB_SERVER_PROTOCOL: ${ENV.COUCHDB_SERVER_PROTOCOL}`);
93+
console.log(` EXPRESS_SERVER_URL: ${ENV.EXPRESS_SERVER_URL}`);
94+
console.log(` EXPRESS_SERVER_PROTOCOL: ${ENV.EXPRESS_SERVER_PROTOCOL}`);
95+
console.log(` DEBUG: ${ENV.DEBUG}`);
96+
console.log(` MOCK: ${ENV.MOCK}`);
97+
} else {
98+
console.warn('ENV initialization failed');
99+
}
84100
}
85101

102+
// if (ENV.DEBUG) {
103+
// }
104+
86105
export default ENV;

0 commit comments

Comments
 (0)