Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"cors": "^2.8.5",
"css-loader": "7.1.2",
"deepmerge": "^4.3.1",
"dotenv": "16.5.0",
"dotenv-expand": "12.0.2",
"html-rspack-plugin": "6.1.2",
"http-proxy-middleware": "^2.0.9",
Expand Down
52 changes: 51 additions & 1 deletion packages/core/src/loadEnv.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
import fs from 'node:fs';
import { join } from 'node:path';
import { parse } from 'dotenv';
import { expand } from 'dotenv-expand';
import { color, getNodeEnv, isFileSync } from './helpers';
import { logger } from './logger';

const DOTENV_LINE =
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;

/**
* This method is modified based on source found in
* https://github.com/motdotla/dotenv/blob/v16.5.0/lib/main.js#L9-L48
*
* Copyright (c) 2015, Scott Motte
*
* We copied this method because Rsbuild only uses the `parse` method
* in dotenv and most of the other code is redundant.
*/
function parse(src: Buffer) {
const obj: Record<string, string> = {};

// Convert buffer to string
let lines = src.toString();

// Convert line breaks to same format
lines = lines.replace(/\r\n?/gm, '\n');

let match: RegExpExecArray | null;
// biome-ignore lint/suspicious/noAssignInExpressions: allowed
while ((match = DOTENV_LINE.exec(lines)) != null) {
const key = match[1];

// Default undefined or null to empty string
let value = match[2] || '';

// Remove whitespace
value = value.trim();

// Check if double quoted
const maybeQuote = value[0];

// Remove surrounding quotes
value = value.replace(/^(['"`])([\s\S]*)\1$/gm, '$2');

// Expand newlines if double quoted
if (maybeQuote === '"') {
value = value.replace(/\\n/g, '\n');
value = value.replace(/\\r/g, '\r');
}

// Add to object
obj[key] = value;
}

return obj;
}

export type LoadEnvOptions = {
/**
* The root path to load the env file
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading