8
8
9
9
import { availableParallelism } from 'node:os' ;
10
10
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' ] ) ;
14
13
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' ] ) ;
18
16
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
+ */
19
22
function isPresent ( variable : string | undefined ) : variable is string {
20
23
return typeof variable === 'string' && variable !== '' ;
21
24
}
22
25
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
+
23
49
// Optimization and mangling
24
50
const debugOptimizeVariable = process . env [ 'NG_BUILD_DEBUG_OPTIMIZE' ] ;
25
51
const debugOptimize = ( ( ) => {
26
- if ( ! isPresent ( debugOptimizeVariable ) || isDisabled ( debugOptimizeVariable ) ) {
52
+ if ( ! isPresent ( debugOptimizeVariable ) || parseTristate ( debugOptimizeVariable ) === false ) {
27
53
return {
28
54
mangle : true ,
29
55
minify : true ,
@@ -37,7 +63,7 @@ const debugOptimize = (() => {
37
63
beautify : true ,
38
64
} ;
39
65
40
- if ( isEnabled ( debugOptimizeVariable ) ) {
66
+ if ( parseTristate ( debugOptimizeVariable ) === true ) {
41
67
return debugValue ;
42
68
}
43
69
@@ -58,12 +84,22 @@ const debugOptimize = (() => {
58
84
return debugValue ;
59
85
} ) ( ) ;
60
86
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 ;
65
92
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
+ */
66
97
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
+ */
67
103
export const allowMinify = debugOptimize . minify ;
68
104
69
105
/**
@@ -76,39 +112,56 @@ export const allowMinify = debugOptimize.minify;
76
112
*
77
113
*/
78
114
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
+ */
79
120
export const maxWorkers = isPresent ( maxWorkersVariable )
80
121
? + maxWorkersVariable
81
122
: Math . min ( 4 , Math . max ( availableParallelism ( ) - 1 , 1 ) ) ;
82
123
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 ;
85
128
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 ;
88
133
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 ;
91
138
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 ;
95
143
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 ;
99
148
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 ;
103
153
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 ;
107
158
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 ;
111
163
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