Skip to content

Commit 7afd8ea

Browse files
committed
add env injection plugin...
pre-empts loading of `common` module, so that its env lookups are consistent
1 parent 8279f66 commit 7afd8ea

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
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)