88
99import { 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+ */
1922function 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+ return undefined ;
46+ }
47+
2348// Optimization and mangling
2449const debugOptimizeVariable = process . env [ 'NG_BUILD_DEBUG_OPTIMIZE' ] ;
2550const debugOptimize = ( ( ) => {
26- if ( ! isPresent ( debugOptimizeVariable ) || isDisabled ( debugOptimizeVariable ) ) {
51+ if ( ! isPresent ( debugOptimizeVariable ) || parseTristate ( debugOptimizeVariable ) === false ) {
2752 return {
2853 mangle : true ,
2954 minify : true ,
@@ -37,7 +62,7 @@ const debugOptimize = (() => {
3762 beautify : true ,
3863 } ;
3964
40- if ( isEnabled ( debugOptimizeVariable ) ) {
65+ if ( parseTristate ( debugOptimizeVariable ) === true ) {
4166 return debugValue ;
4267 }
4368
@@ -58,12 +83,22 @@ const debugOptimize = (() => {
5883 return debugValue ;
5984} ) ( ) ;
6085
61- const mangleVariable = process . env [ 'NG_BUILD_MANGLE' ] ;
62- export const allowMangle = isPresent ( mangleVariable )
63- ? ! isDisabled ( mangleVariable )
64- : debugOptimize . mangle ;
86+ /**
87+ * Allows disabling of code mangling when the `NG_BUILD_MANGLE` environment variable is set to `0` or `false`.
88+ * This is useful for debugging build output.
89+ */
90+ export const allowMangle = parseTristate ( process . env [ 'NG_BUILD_MANGLE' ] ) ?? debugOptimize . mangle ;
6591
92+ /**
93+ * Allows beautification of build output when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
94+ * This is useful for debugging build output.
95+ */
6696export const shouldBeautify = debugOptimize . beautify ;
97+
98+ /**
99+ * Allows disabling of code minification when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
100+ * This is useful for debugging build output.
101+ */
67102export const allowMinify = debugOptimize . minify ;
68103
69104/**
@@ -76,39 +111,56 @@ export const allowMinify = debugOptimize.minify;
76111 *
77112 */
78113const maxWorkersVariable = process . env [ 'NG_BUILD_MAX_WORKERS' ] ;
114+
115+ /**
116+ * The maximum number of workers to use for parallel processing.
117+ * This can be controlled by the `NG_BUILD_MAX_WORKERS` environment variable.
118+ */
79119export const maxWorkers = isPresent ( maxWorkersVariable )
80120 ? + maxWorkersVariable
81121 : Math . min ( 4 , Math . max ( availableParallelism ( ) - 1 , 1 ) ) ;
82122
83- const parallelTsVariable = process . env [ 'NG_BUILD_PARALLEL_TS' ] ;
84- export const useParallelTs = ! isPresent ( parallelTsVariable ) || ! isDisabled ( parallelTsVariable ) ;
123+ /**
124+ * When `NG_BUILD_PARALLEL_TS` is set to `0` or `false`, parallel TypeScript compilation is disabled.
125+ */
126+ export const useParallelTs = parseTristate ( process . env [ 'NG_BUILD_PARALLEL_TS' ] ) !== false ;
85127
86- const debugPerfVariable = process . env [ 'NG_BUILD_DEBUG_PERF' ] ;
87- export const debugPerformance = isPresent ( debugPerfVariable ) && isEnabled ( debugPerfVariable ) ;
128+ /**
129+ * When `NG_BUILD_DEBUG_PERF` is enabled, performance debugging information is printed.
130+ */
131+ export const debugPerformance = parseTristate ( process . env [ 'NG_BUILD_DEBUG_PERF' ] ) === true ;
88132
89- const watchRootVariable = process . env [ 'NG_BUILD_WATCH_ROOT' ] ;
90- export const shouldWatchRoot = isPresent ( watchRootVariable ) && isEnabled ( watchRootVariable ) ;
133+ /**
134+ * When `NG_BUILD_WATCH_ROOT` is enabled, the build will watch the root directory for changes.
135+ */
136+ export const shouldWatchRoot = parseTristate ( process . env [ 'NG_BUILD_WATCH_ROOT' ] ) === true ;
91137
92- const typeCheckingVariable = process . env [ 'NG_BUILD_TYPE_CHECK' ] ;
93- export const useTypeChecking =
94- ! isPresent ( typeCheckingVariable ) || ! isDisabled ( typeCheckingVariable ) ;
138+ /**
139+ * When `NG_BUILD_TYPE_CHECK` is set to `0` or `false`, type checking is disabled.
140+ */
141+ export const useTypeChecking = parseTristate ( process . env [ 'NG_BUILD_TYPE_CHECK' ] ) !== false ;
95142
96- const buildLogsJsonVariable = process . env [ 'NG_BUILD_LOGS_JSON' ] ;
97- export const useJSONBuildLogs =
98- isPresent ( buildLogsJsonVariable ) && isEnabled ( buildLogsJsonVariable ) ;
143+ /**
144+ * When `NG_BUILD_LOGS_JSON` is enabled, build logs will be output in JSON format.
145+ */
146+ export const useJSONBuildLogs = parseTristate ( process . env [ 'NG_BUILD_LOGS_JSON' ] ) === true ;
99147
100- const optimizeChunksVariable = process . env [ 'NG_BUILD_OPTIMIZE_CHUNKS' ] ;
101- export const shouldOptimizeChunks =
102- isPresent ( optimizeChunksVariable ) && isEnabled ( optimizeChunksVariable ) ;
148+ /**
149+ * When `NG_BUILD_OPTIMIZE_CHUNKS` is enabled, the build will optimize chunks.
150+ */
151+ export const shouldOptimizeChunks = parseTristate ( process . env [ 'NG_BUILD_OPTIMIZE_CHUNKS' ] ) === true ;
103152
104- const hmrComponentStylesVariable = process . env [ 'NG_HMR_CSTYLES' ] ;
105- export const useComponentStyleHmr =
106- isPresent ( hmrComponentStylesVariable ) && isEnabled ( hmrComponentStylesVariable ) ;
153+ /**
154+ * When `NG_HMR_CSTYLES` is enabled, component styles will be hot-reloaded.
155+ */
156+ export const useComponentStyleHmr = parseTristate ( process . env [ 'NG_HMR_CSTYLES' ] ) === true ;
107157
108- const hmrComponentTemplateVariable = process . env [ 'NG_HMR_TEMPLATES' ] ;
109- export const useComponentTemplateHmr =
110- ! isPresent ( hmrComponentTemplateVariable ) || ! isDisabled ( hmrComponentTemplateVariable ) ;
158+ /**
159+ * When `NG_HMR_TEMPLATES` is set to `0` or `false`, component templates will not be hot-reloaded.
160+ */
161+ export const useComponentTemplateHmr = parseTristate ( process . env [ 'NG_HMR_TEMPLATES' ] ) !== false ;
111162
112- const partialSsrBuildVariable = process . env [ 'NG_BUILD_PARTIAL_SSR' ] ;
113- export const usePartialSsrBuild =
114- isPresent ( partialSsrBuildVariable ) && isEnabled ( partialSsrBuildVariable ) ;
163+ /**
164+ * When `NG_BUILD_PARTIAL_SSR` is enabled, a partial server-side rendering build will be performed.
165+ */
166+ export const usePartialSsrBuild = parseTristate ( process . env [ 'NG_BUILD_PARTIAL_SSR' ] ) === true ;
0 commit comments