-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadEnv.js
More file actions
26 lines (21 loc) · 708 Bytes
/
Copy pathloadEnv.js
File metadata and controls
26 lines (21 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { loadEnv as rsbuildLoadEnv } from '@rsbuild/core';
import { type } from 'arktype';
const Env = type({
PUBLIC_PORT: type('string.integer.parse').to('number > 0'),
});
const { rawPublicVars } = await rsbuildLoadEnv({
cwd: import.meta.dirname,
mode: process.env.NODE_ENV ?? 'development',
});
const env = Env.assert(rawPublicVars);
const define = /** @type {(keyof typeof env)[]} */ (Object.keys(env)).reduce(
(define, key) => {
const stringified = JSON.stringify(env[key]);
return Object.assign(define, {
[`import.meta.env.${key}`]: stringified,
[`process.env.${key}`]: stringified,
});
},
/** @type {Record<string, string>} */ ({}),
);
export { env, define };