Skip to content

Commit 97a8568

Browse files
update rsbuild envs
1 parent 4e878e6 commit 97a8568

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"start": "rsbuild dev",
6868
"dev": "DISABLE_ESLINT_PLUGIN=true TSC_COMPILE_ON_ERROR=true REACT_APP_BACKEND=http://localhost:8765 REACT_APP_META_BACKEND=undefined rsbuild dev",
6969
"build": "rm -rf build && DISABLE_ESLINT_PLUGIN=true CI=true rsbuild build",
70-
"//build:embedded": "echo 'PUBLIC_URL is a setting for create-react-app. Embedded version is built and hosted as is on ydb servers, with no way of knowing the final URL pattern. PUBLIC_URL=. keeps paths to all static relative, allowing servers to handle them as needed'",
7170
"build:embedded": "GENERATE_SOURCEMAP=false REACT_APP_BACKEND=http://localhost:8765 REACT_APP_META_BACKEND=undefined npm run build",
7271
"build:embedded-mc": "GENERATE_SOURCEMAP=false PUBLIC_URL=. REACT_APP_BACKEND= REACT_APP_META_BACKEND= npm run build",
7372
"build:embedded:archive": "npm run build:embedded && mv build embedded-ui && cp CHANGELOG.md embedded-ui/CHANGELOG.md && zip -r embedded-ui.zip embedded-ui && rm -rf embedded-ui",

rsbuild.config.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,52 @@ import {pluginSass} from '@rsbuild/plugin-sass';
44
import {pluginSvgr} from '@rsbuild/plugin-svgr';
55
import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin';
66

7+
/**
8+
* List of environment variables that should be accessible in HTML and JavaScript files.
9+
* These variables are injected into the client-side code via process.env definitions.
10+
*/
11+
const APP_VARS = [
12+
'DISABLE_ESLINT_PLUGIN',
13+
'TSC_COMPILE_ON_ERROR',
14+
'REACT_APP_DISABLE_CHECKS',
15+
16+
// Backend configuration
17+
'REACT_APP_BACKEND',
18+
'REACT_APP_META_BACKEND',
19+
'META_YDB_BACKEND',
20+
];
21+
22+
/**
23+
* Prepares environment variables to be exposed in client-side code.
24+
*
25+
* This function:
26+
* 1. Loads environment variables from .env files using rsbuild's loadEnv()
27+
* 2. Explicitly checks process.env for each variable (prioritizes process.env over .env file values)
28+
* 3. Stringifies values for safe injection into JavaScript code via source.define
29+
*
30+
* CRITICAL FOR CI: We must explicitly pass process.env variables to source.define,
31+
* otherwise they are not accessible in client code in CI (GitHub Actions).
32+
* rsbuild's loadEnv() only reads from .env files, not from process.env by default.
33+
*/
734
const prepareEnvVars = () => {
8-
// Load values from .env
35+
// Load values from .env files (if they exist)
936
const {parsed} = loadEnv();
1037

1138
const result: Record<string, string> = {};
1239

13-
// Additionall stringify vars to prevent errors with invalid symbols
14-
// rsbuild parses values additionally
15-
Object.entries(parsed).forEach(([key, value]) => {
16-
result[key] = JSON.stringify(value);
40+
APP_VARS.forEach((name) => {
41+
// Priority: process.env > .env file
42+
// This ensures CI-provided env vars (from GitHub Actions, Playwright webServer, etc.)
43+
// take precedence over .env file values
44+
const value = process.env[name] ?? parsed[name];
45+
// Stringify for safe injection into JavaScript code
46+
result[name] = JSON.stringify(value);
1747
});
1848

19-
return result;
49+
return {
50+
// Without this, environment variables would not be available in index.html or JavaScript
51+
'process.env': result,
52+
};
2053
};
2154

2255
export default defineConfig({
@@ -38,6 +71,7 @@ export default defineConfig({
3871
'node_modules/@gravity-ui/websql-autocomplete',
3972
'node_modules/antlr4ng',
4073
],
74+
define: prepareEnvVars(),
4175
},
4276
output: {
4377
distPath: {
@@ -49,12 +83,8 @@ export default defineConfig({
4983
template: './public/index.html',
5084
},
5185
tools: {
52-
rspack(config, {appendPlugins, rspack}) {
86+
rspack(config, {appendPlugins}) {
5387
appendPlugins([
54-
new rspack.DefinePlugin({
55-
// Without it vars from .env are not available in index.html
56-
'process.env': prepareEnvVars(),
57-
}),
5888
new MonacoWebpackPlugin({
5989
languages: ['yaml'],
6090
customLanguages: [

0 commit comments

Comments
 (0)