Skip to content

Commit f9ac82e

Browse files
committed
refactor(@angular/build): align environment option parsing with CLI package
The environment options in the build package are updated to align with the tristate parsing logic and environment variable checks found in the CLI package. This change introduces a `parseTristate` function to handle 'true', 'false', and undefined values from environment variables, providing more consistent behavior across the two packages.
1 parent c3789e4 commit f9ac82e

File tree

1 file changed

+89
-36
lines changed

1 file changed

+89
-36
lines changed

packages/angular/build/src/utils/environment-options.ts

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,48 @@
88

99
import { availableParallelism } from 'node:os';
1010

11-
function isDisabled(variable: string): boolean {
12-
return variable === '0' || variable.toLowerCase() === 'false';
13-
}
11+
/** A set of strings that are considered "truthy" when parsing environment variables. */
12+
const TRUTHY_VALUES = new Set(['1', 'true']);
1413

15-
function isEnabled(variable: string): boolean {
16-
return variable === '1' || variable.toLowerCase() === 'true';
17-
}
14+
/** A set of strings that are considered "falsy" when parsing environment variables. */
15+
const FALSY_VALUES = new Set(['0', 'false']);
1816

17+
/**
18+
* Checks if an environment variable is present and has a non-empty value.
19+
* @param variable The environment variable to check.
20+
* @returns `true` if the variable is a non-empty string.
21+
*/
1922
function isPresent(variable: string | undefined): variable is string {
2023
return typeof variable === 'string' && variable !== '';
2124
}
2225

26+
/**
27+
* Parses an environment variable into a boolean or undefined.
28+
* @returns `true` if the variable is truthy ('1', 'true').
29+
* @returns `false` if the variable is falsy ('0', 'false').
30+
* @returns `undefined` if the variable is not present or has an unknown value.
31+
*/
32+
function parseTristate(variable: string | undefined): boolean | undefined {
33+
if (!isPresent(variable)) {
34+
return undefined;
35+
}
36+
37+
const value = variable.toLowerCase();
38+
if (TRUTHY_VALUES.has(value)) {
39+
return true;
40+
}
41+
if (FALSY_VALUES.has(value)) {
42+
return false;
43+
}
44+
45+
// TODO: Consider whether a warning is useful in this case of a malformed value
46+
return undefined;
47+
}
48+
2349
// Optimization and mangling
2450
const debugOptimizeVariable = process.env['NG_BUILD_DEBUG_OPTIMIZE'];
2551
const debugOptimize = (() => {
26-
if (!isPresent(debugOptimizeVariable) || isDisabled(debugOptimizeVariable)) {
52+
if (!isPresent(debugOptimizeVariable) || parseTristate(debugOptimizeVariable) === false) {
2753
return {
2854
mangle: true,
2955
minify: true,
@@ -37,7 +63,7 @@ const debugOptimize = (() => {
3763
beautify: true,
3864
};
3965

40-
if (isEnabled(debugOptimizeVariable)) {
66+
if (parseTristate(debugOptimizeVariable) === true) {
4167
return debugValue;
4268
}
4369

@@ -58,12 +84,22 @@ const debugOptimize = (() => {
5884
return debugValue;
5985
})();
6086

61-
const mangleVariable = process.env['NG_BUILD_MANGLE'];
62-
export const allowMangle = isPresent(mangleVariable)
63-
? !isDisabled(mangleVariable)
64-
: debugOptimize.mangle;
87+
/**
88+
* Allows disabling of code mangling when the `NG_BUILD_MANGLE` environment variable is set to `0` or `false`.
89+
* This is useful for debugging build output.
90+
*/
91+
export const allowMangle = parseTristate(process.env['NG_BUILD_MANGLE']) ?? debugOptimize.mangle;
6592

93+
/**
94+
* Allows beautification of build output when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
95+
* This is useful for debugging build output.
96+
*/
6697
export const shouldBeautify = debugOptimize.beautify;
98+
99+
/**
100+
* Allows disabling of code minification when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
101+
* This is useful for debugging build output.
102+
*/
67103
export const allowMinify = debugOptimize.minify;
68104

69105
/**
@@ -76,39 +112,56 @@ export const allowMinify = debugOptimize.minify;
76112
*
77113
*/
78114
const maxWorkersVariable = process.env['NG_BUILD_MAX_WORKERS'];
115+
116+
/**
117+
* The maximum number of workers to use for parallel processing.
118+
* This can be controlled by the `NG_BUILD_MAX_WORKERS` environment variable.
119+
*/
79120
export const maxWorkers = isPresent(maxWorkersVariable)
80121
? +maxWorkersVariable
81122
: Math.min(4, Math.max(availableParallelism() - 1, 1));
82123

83-
const parallelTsVariable = process.env['NG_BUILD_PARALLEL_TS'];
84-
export const useParallelTs = !isPresent(parallelTsVariable) || !isDisabled(parallelTsVariable);
124+
/**
125+
* When `NG_BUILD_PARALLEL_TS` is set to `0` or `false`, parallel TypeScript compilation is disabled.
126+
*/
127+
export const useParallelTs = parseTristate(process.env['NG_BUILD_PARALLEL_TS']) !== false;
85128

86-
const debugPerfVariable = process.env['NG_BUILD_DEBUG_PERF'];
87-
export const debugPerformance = isPresent(debugPerfVariable) && isEnabled(debugPerfVariable);
129+
/**
130+
* When `NG_BUILD_DEBUG_PERF` is enabled, performance debugging information is printed.
131+
*/
132+
export const debugPerformance = parseTristate(process.env['NG_BUILD_DEBUG_PERF']) === true;
88133

89-
const watchRootVariable = process.env['NG_BUILD_WATCH_ROOT'];
90-
export const shouldWatchRoot = isPresent(watchRootVariable) && isEnabled(watchRootVariable);
134+
/**
135+
* When `NG_BUILD_WATCH_ROOT` is enabled, the build will watch the root directory for changes.
136+
*/
137+
export const shouldWatchRoot = parseTristate(process.env['NG_BUILD_WATCH_ROOT']) === true;
91138

92-
const typeCheckingVariable = process.env['NG_BUILD_TYPE_CHECK'];
93-
export const useTypeChecking =
94-
!isPresent(typeCheckingVariable) || !isDisabled(typeCheckingVariable);
139+
/**
140+
* When `NG_BUILD_TYPE_CHECK` is set to `0` or `false`, type checking is disabled.
141+
*/
142+
export const useTypeChecking = parseTristate(process.env['NG_BUILD_TYPE_CHECK']) !== false;
95143

96-
const buildLogsJsonVariable = process.env['NG_BUILD_LOGS_JSON'];
97-
export const useJSONBuildLogs =
98-
isPresent(buildLogsJsonVariable) && isEnabled(buildLogsJsonVariable);
144+
/**
145+
* When `NG_BUILD_LOGS_JSON` is enabled, build logs will be output in JSON format.
146+
*/
147+
export const useJSONBuildLogs = parseTristate(process.env['NG_BUILD_LOGS_JSON']) === true;
99148

100-
const optimizeChunksVariable = process.env['NG_BUILD_OPTIMIZE_CHUNKS'];
101-
export const shouldOptimizeChunks =
102-
isPresent(optimizeChunksVariable) && isEnabled(optimizeChunksVariable);
149+
/**
150+
* When `NG_BUILD_OPTIMIZE_CHUNKS` is enabled, the build will optimize chunks.
151+
*/
152+
export const shouldOptimizeChunks = parseTristate(process.env['NG_BUILD_OPTIMIZE_CHUNKS']) === true;
103153

104-
const hmrComponentStylesVariable = process.env['NG_HMR_CSTYLES'];
105-
export const useComponentStyleHmr =
106-
isPresent(hmrComponentStylesVariable) && isEnabled(hmrComponentStylesVariable);
154+
/**
155+
* When `NG_HMR_CSTYLES` is enabled, component styles will be hot-reloaded.
156+
*/
157+
export const useComponentStyleHmr = parseTristate(process.env['NG_HMR_CSTYLES']) === true;
107158

108-
const hmrComponentTemplateVariable = process.env['NG_HMR_TEMPLATES'];
109-
export const useComponentTemplateHmr =
110-
!isPresent(hmrComponentTemplateVariable) || !isDisabled(hmrComponentTemplateVariable);
159+
/**
160+
* When `NG_HMR_TEMPLATES` is set to `0` or `false`, component templates will not be hot-reloaded.
161+
*/
162+
export const useComponentTemplateHmr = parseTristate(process.env['NG_HMR_TEMPLATES']) !== false;
111163

112-
const partialSsrBuildVariable = process.env['NG_BUILD_PARTIAL_SSR'];
113-
export const usePartialSsrBuild =
114-
isPresent(partialSsrBuildVariable) && isEnabled(partialSsrBuildVariable);
164+
/**
165+
* When `NG_BUILD_PARTIAL_SSR` is enabled, a partial server-side rendering build will be performed.
166+
*/
167+
export const usePartialSsrBuild = parseTristate(process.env['NG_BUILD_PARTIAL_SSR']) === true;

0 commit comments

Comments
 (0)