forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvironment-options.ts
More file actions
203 lines (173 loc) · 6.57 KB
/
environment-options.ts
File metadata and controls
203 lines (173 loc) · 6.57 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { availableParallelism } from 'node:os';
/** A set of strings that are considered "truthy" when parsing environment variables. */
const TRUTHY_VALUES = new Set(['1', 'true']);
/** A set of strings that are considered "falsy" when parsing environment variables. */
const FALSY_VALUES = new Set(['0', 'false']);
/**
* Checks if an environment variable is present and has a non-empty value.
* @param variable The environment variable to check.
* @returns `true` if the variable is a non-empty string.
*/
function isPresent(variable: string | undefined): variable is string {
return typeof variable === 'string' && variable !== '';
}
/**
* Parses an environment variable into a boolean or undefined.
* @returns `true` if the variable is truthy ('1', 'true').
* @returns `false` if the variable is falsy ('0', 'false').
* @returns `undefined` if the variable is not present or has an unknown value.
*/
function parseTristate(variable: string | undefined): boolean | undefined {
if (!isPresent(variable)) {
return undefined;
}
const value = variable.toLowerCase();
if (TRUTHY_VALUES.has(value)) {
return true;
}
if (FALSY_VALUES.has(value)) {
return false;
}
// TODO: Consider whether a warning is useful in this case of a malformed value
return undefined;
}
// Optimization and mangling
const debugOptimizeVariable = process.env['NG_BUILD_DEBUG_OPTIMIZE'];
const debugOptimize = (() => {
if (!isPresent(debugOptimizeVariable) || parseTristate(debugOptimizeVariable) === false) {
return {
mangle: true,
minify: true,
beautify: false,
};
}
const debugValue = {
mangle: false,
minify: false,
beautify: true,
};
if (parseTristate(debugOptimizeVariable) === true) {
return debugValue;
}
for (const part of debugOptimizeVariable.split(',')) {
switch (part.trim().toLowerCase()) {
case 'mangle':
debugValue.mangle = true;
break;
case 'minify':
debugValue.minify = true;
break;
case 'beautify':
debugValue.beautify = true;
break;
}
}
return debugValue;
})();
/**
* Allows disabling of code mangling when the `NG_BUILD_MANGLE` environment variable is set to `0` or `false`.
* This is useful for debugging build output.
*/
export const allowMangle = parseTristate(process.env['NG_BUILD_MANGLE']) ?? debugOptimize.mangle;
/**
* Allows beautification of build output when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
* This is useful for debugging build output.
*/
export const shouldBeautify = debugOptimize.beautify;
/**
* Allows disabling of code minification when the `NG_BUILD_DEBUG_OPTIMIZE` environment variable is enabled.
* This is useful for debugging build output.
*/
export const allowMinify = debugOptimize.minify;
/**
* Allows using Rolldown for chunk optimization instead of Rollup.
* This is useful for debugging and testing scenarios.
*/
export const useRolldownChunks = parseTristate(process.env['NG_BUILD_CHUNKS_ROLLDOWN']) ?? false;
/**
* Some environments, like CircleCI which use Docker report a number of CPUs by the host and not the count of available.
* This cause `Error: Call retries were exceeded` errors when trying to use them.
*
* @see https://github.com/nodejs/node/issues/28762
* @see https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
* @see https://ithub.com/angular/angular-cli/issues/16860#issuecomment-588828079
*
*/
const maxWorkersVariable = process.env['NG_BUILD_MAX_WORKERS'];
/**
* The maximum number of workers to use for parallel processing.
* This can be controlled by the `NG_BUILD_MAX_WORKERS` environment variable.
*/
export const maxWorkers = isPresent(maxWorkersVariable)
? +maxWorkersVariable
: Math.min(4, Math.max(availableParallelism() - 1, 1));
/**
* When `NG_BUILD_PARALLEL_TS` is set to `0` or `false`, parallel TypeScript compilation is disabled.
*/
export const useParallelTs = parseTristate(process.env['NG_BUILD_PARALLEL_TS']) !== false;
/**
* When `NG_BUILD_DEBUG_PERF` is enabled, performance debugging information is printed.
*/
export const debugPerformance = parseTristate(process.env['NG_BUILD_DEBUG_PERF']) === true;
/**
* When `NG_BUILD_WATCH_ROOT` is enabled, the build will watch the root directory for changes.
*/
export const shouldWatchRoot = parseTristate(process.env['NG_BUILD_WATCH_ROOT']) === true;
/**
* When `NG_BUILD_TYPE_CHECK` is set to `0` or `false`, type checking is disabled.
*/
export const useTypeChecking = parseTristate(process.env['NG_BUILD_TYPE_CHECK']) !== false;
/**
* When `NG_BUILD_LOGS_JSON` is enabled, build logs will be output in JSON format.
*/
export const useJSONBuildLogs = parseTristate(process.env['NG_BUILD_LOGS_JSON']) === true;
/**
* When `NG_BUILD_OPTIMIZE_CHUNKS` is enabled, the build will optimize chunks.
*/
/**
* The threshold of lazy chunks required to enable the chunk optimization pass.
* Can be configured via the `NG_BUILD_OPTIMIZE_CHUNKS` environment variable.
* - `false` or `0` disables the feature.
* - `true` or `1` forces the feature on (threshold 0).
* - A number sets the specific threshold.
* - Default is 3.
*/
const optimizeChunksEnv = process.env['NG_BUILD_OPTIMIZE_CHUNKS'];
export const optimizeChunksThreshold = (() => {
if (optimizeChunksEnv === undefined) {
return 3;
}
if (optimizeChunksEnv === 'false' || optimizeChunksEnv === '0') {
return Infinity;
}
if (optimizeChunksEnv === 'true' || optimizeChunksEnv === '1') {
return 0;
}
const num = Number.parseInt(optimizeChunksEnv, 10);
return Number.isNaN(num) || num < 0 ? 3 : num;
})();
/**
* When `NG_HMR_CSTYLES` is enabled, component styles will be hot-reloaded.
*/
export const useComponentStyleHmr = parseTristate(process.env['NG_HMR_CSTYLES']) === true;
/**
* When `NG_HMR_TEMPLATES` is set to `0` or `false`, component templates will not be hot-reloaded.
*/
export const useComponentTemplateHmr = parseTristate(process.env['NG_HMR_TEMPLATES']) !== false;
/**
* When `NG_BUILD_PARTIAL_SSR` is enabled, a partial server-side rendering build will be performed.
*/
export const usePartialSsrBuild = parseTristate(process.env['NG_BUILD_PARTIAL_SSR']) === true;
const bazelBinDirectory = process.env['BAZEL_BINDIR'];
const bazelExecRoot = process.env['JS_BINARY__EXECROOT'];
export const bazelEsbuildPluginPath =
bazelBinDirectory && bazelExecRoot
? process.env['NG_INTERNAL_ESBUILD_PLUGINS_DO_NOT_USE']
: undefined;