bertui 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client/compiler.js +38 -5
- package/src/utils/env.js +57 -0
package/package.json
CHANGED
package/src/client/compiler.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readdirSync, statSync } from 'fs';
|
|
2
2
|
import { join, extname, relative, dirname } from 'path';
|
|
3
3
|
import logger from '../logger/logger.js';
|
|
4
|
+
import { loadEnvVariables, generateEnvCode, replaceEnvInCode } from '../utils/env.js';
|
|
4
5
|
|
|
5
6
|
export async function compileProject(root) {
|
|
6
7
|
logger.bigLog('COMPILING PROJECT', { color: 'blue' });
|
|
@@ -19,6 +20,16 @@ export async function compileProject(root) {
|
|
|
19
20
|
logger.info('Created .bertui/compiled/');
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
// Load environment variables
|
|
24
|
+
const envVars = loadEnvVariables(root);
|
|
25
|
+
if (Object.keys(envVars).length > 0) {
|
|
26
|
+
logger.info(`Loaded ${Object.keys(envVars).length} environment variables`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Generate env.js file
|
|
30
|
+
const envCode = generateEnvCode(envVars);
|
|
31
|
+
await Bun.write(join(outDir, 'env.js'), envCode);
|
|
32
|
+
|
|
22
33
|
let routes = [];
|
|
23
34
|
if (existsSync(pagesDir)) {
|
|
24
35
|
routes = await discoverRoutes(pagesDir);
|
|
@@ -36,7 +47,7 @@ export async function compileProject(root) {
|
|
|
36
47
|
}
|
|
37
48
|
|
|
38
49
|
const startTime = Date.now();
|
|
39
|
-
const stats = await compileDirectory(srcDir, outDir, root);
|
|
50
|
+
const stats = await compileDirectory(srcDir, outDir, root, envVars);
|
|
40
51
|
const duration = Date.now() - startTime;
|
|
41
52
|
|
|
42
53
|
if (routes.length > 0) {
|
|
@@ -239,7 +250,7 @@ ${routeConfigs}
|
|
|
239
250
|
await Bun.write(routerPath, routerComponentCode);
|
|
240
251
|
}
|
|
241
252
|
|
|
242
|
-
async function compileDirectory(srcDir, outDir, root) {
|
|
253
|
+
async function compileDirectory(srcDir, outDir, root, envVars) {
|
|
243
254
|
const stats = { files: 0, skipped: 0 };
|
|
244
255
|
|
|
245
256
|
const files = readdirSync(srcDir);
|
|
@@ -251,7 +262,7 @@ async function compileDirectory(srcDir, outDir, root) {
|
|
|
251
262
|
if (stat.isDirectory()) {
|
|
252
263
|
const subOutDir = join(outDir, file);
|
|
253
264
|
mkdirSync(subOutDir, { recursive: true });
|
|
254
|
-
const subStats = await compileDirectory(srcPath, subOutDir, root);
|
|
265
|
+
const subStats = await compileDirectory(srcPath, subOutDir, root, envVars);
|
|
255
266
|
stats.files += subStats.files;
|
|
256
267
|
stats.skipped += subStats.skipped;
|
|
257
268
|
} else {
|
|
@@ -268,7 +279,7 @@ async function compileDirectory(srcDir, outDir, root) {
|
|
|
268
279
|
logger.debug(`Copied CSS: ${relativePath}`);
|
|
269
280
|
stats.files++;
|
|
270
281
|
} else if (['.jsx', '.tsx', '.ts'].includes(ext)) {
|
|
271
|
-
await compileFile(srcPath, outDir, file, relativePath, root);
|
|
282
|
+
await compileFile(srcPath, outDir, file, relativePath, root, envVars);
|
|
272
283
|
stats.files++;
|
|
273
284
|
} else if (ext === '.js') {
|
|
274
285
|
const outPath = join(outDir, file);
|
|
@@ -276,6 +287,8 @@ async function compileDirectory(srcDir, outDir, root) {
|
|
|
276
287
|
|
|
277
288
|
// Remove ALL CSS imports
|
|
278
289
|
code = removeCSSImports(code);
|
|
290
|
+
// Inject environment variables
|
|
291
|
+
code = replaceEnvInCode(code, envVars);
|
|
279
292
|
// Fix router imports
|
|
280
293
|
code = fixRouterImports(code, outPath, root);
|
|
281
294
|
|
|
@@ -292,7 +305,7 @@ async function compileDirectory(srcDir, outDir, root) {
|
|
|
292
305
|
return stats;
|
|
293
306
|
}
|
|
294
307
|
|
|
295
|
-
async function compileFile(srcPath, outDir, filename, relativePath, root) {
|
|
308
|
+
async function compileFile(srcPath, outDir, filename, relativePath, root, envVars) {
|
|
296
309
|
const ext = extname(filename);
|
|
297
310
|
const loader = ext === '.tsx' ? 'tsx' : ext === '.ts' ? 'ts' : 'jsx';
|
|
298
311
|
|
|
@@ -302,6 +315,12 @@ async function compileFile(srcPath, outDir, filename, relativePath, root) {
|
|
|
302
315
|
// CRITICAL FIX: Remove ALL CSS imports before transpilation
|
|
303
316
|
code = removeCSSImports(code);
|
|
304
317
|
|
|
318
|
+
// Remove dotenv imports (not needed in browser)
|
|
319
|
+
code = removeDotenvImports(code);
|
|
320
|
+
|
|
321
|
+
// Inject environment variables
|
|
322
|
+
code = replaceEnvInCode(code, envVars);
|
|
323
|
+
|
|
305
324
|
const outPath = join(outDir, filename.replace(/\.(jsx|tsx|ts)$/, '.js'));
|
|
306
325
|
code = fixRouterImports(code, outPath, root);
|
|
307
326
|
|
|
@@ -343,6 +362,20 @@ function removeCSSImports(code) {
|
|
|
343
362
|
return code;
|
|
344
363
|
}
|
|
345
364
|
|
|
365
|
+
// NEW FUNCTION: Remove dotenv imports and dotenv.config() calls
|
|
366
|
+
function removeDotenvImports(code) {
|
|
367
|
+
// Remove: import dotenv from 'dotenv'
|
|
368
|
+
code = code.replace(/import\s+\w+\s+from\s+['"]dotenv['"]\s*;?\s*/g, '');
|
|
369
|
+
|
|
370
|
+
// Remove: import { config } from 'dotenv'
|
|
371
|
+
code = code.replace(/import\s+\{[^}]+\}\s+from\s+['"]dotenv['"]\s*;?\s*/g, '');
|
|
372
|
+
|
|
373
|
+
// Remove: dotenv.config()
|
|
374
|
+
code = code.replace(/\w+\.config\(\s*\)\s*;?\s*/g, '');
|
|
375
|
+
|
|
376
|
+
return code;
|
|
377
|
+
}
|
|
378
|
+
|
|
346
379
|
function fixRouterImports(code, outPath, root) {
|
|
347
380
|
const buildDir = join(root, '.bertui', 'compiled');
|
|
348
381
|
const routerPath = join(buildDir, 'router.js');
|
package/src/utils/env.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// bertui/src/utils/env.js
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Load environment variables for BertUI
|
|
7
|
+
* This runs at BUILD TIME (Node.js), not in the browser
|
|
8
|
+
*/
|
|
9
|
+
export function loadEnvVariables(root) {
|
|
10
|
+
const envVars = {};
|
|
11
|
+
|
|
12
|
+
// Load from process.env (already loaded by Bun/Node)
|
|
13
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
14
|
+
// Only expose variables that start with VITE_ or PUBLIC_
|
|
15
|
+
if (key.startsWith('BERTUI_') || key.startsWith('PUBLIC_')) {
|
|
16
|
+
envVars[key] = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return envVars;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generate code to inject env variables into the browser
|
|
25
|
+
*/
|
|
26
|
+
export function generateEnvCode(envVars) {
|
|
27
|
+
const envObject = Object.entries(envVars)
|
|
28
|
+
.map(([key, value]) => ` "${key}": ${JSON.stringify(value)}`)
|
|
29
|
+
.join(',\n');
|
|
30
|
+
|
|
31
|
+
return `
|
|
32
|
+
// Environment variables injected at build time
|
|
33
|
+
export const env = {
|
|
34
|
+
${envObject}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
Make it available globally (optional)
|
|
38
|
+
if (typeof window !== 'undefined') {
|
|
39
|
+
window.__BERTUI_ENV__ = env;
|
|
40
|
+
}
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Replace process.env references in code with actual values
|
|
46
|
+
*/
|
|
47
|
+
export function replaceEnvInCode(code, envVars) {
|
|
48
|
+
let result = code;
|
|
49
|
+
|
|
50
|
+
// Replace process.env.VARIABLE_NAME with actual values
|
|
51
|
+
for (const [key, value] of Object.entries(envVars)) {
|
|
52
|
+
const regex = new RegExp(`process\\.env\\.${key}`, 'g');
|
|
53
|
+
result = result.replace(regex, JSON.stringify(value));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return result;
|
|
57
|
+
}
|