Skip to content

Commit 9568d81

Browse files
Refactor: add parseCacheCompressionOption helper
Extract a small parser for BUILD_CACHE_COMPRESSION to centralize the mapping of env values to webpack filesystem cache 'compression' option. Improves readability and keeps the previous behavior (undefined=webpack default, '',0,false,none=false, else 'gzip'|'brotli' or warn->false). Verified with prettier, ESLint and build.
1 parent b5f27c3 commit 9568d81

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

build.mjs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ const isAnalyzing = process.argv[2] === '--analyze'
1717
const parallelBuild = process.env.BUILD_PARALLEL !== '0'
1818
const isWatchOnce = !!process.env.BUILD_WATCH_ONCE
1919
// Cache compression control: default none; allow override via env
20-
const cacheCompressionEnv = process.env.BUILD_CACHE_COMPRESSION
21-
let cacheCompressionOption
22-
if (cacheCompressionEnv != null) {
23-
const v = String(cacheCompressionEnv).trim().toLowerCase()
24-
if (v === '' || v === '0' || v === 'false' || v === 'none') {
25-
cacheCompressionOption = false
26-
} else if (v === 'gzip' || v === 'brotli') {
27-
cacheCompressionOption = v
28-
} else {
29-
console.warn(
30-
`[build] Unknown BUILD_CACHE_COMPRESSION="${cacheCompressionEnv}", defaulting to no compression`,
31-
)
32-
cacheCompressionOption = false
33-
}
20+
function parseCacheCompressionOption(envVal) {
21+
if (envVal == null) return undefined
22+
const v = String(envVal).trim().toLowerCase()
23+
if (v === '' || v === '0' || v === 'false' || v === 'none') return false
24+
if (v === 'gzip' || v === 'brotli') return v
25+
console.warn(`[build] Unknown BUILD_CACHE_COMPRESSION="${envVal}", defaulting to no compression`)
26+
return false
3427
}
28+
const cacheCompressionOption = parseCacheCompressionOption(process.env.BUILD_CACHE_COMPRESSION)
3529
let cpuCount = 1
3630
try {
3731
const cpuInfo = os.cpus && os.cpus()
@@ -40,26 +34,25 @@ try {
4034
} catch {
4135
cpuCount = 1
4236
}
43-
const rawWorkers = process.env.BUILD_THREAD_WORKERS
44-
? parseInt(process.env.BUILD_THREAD_WORKERS, 10)
45-
: undefined
46-
let threadWorkers
47-
if (Number.isInteger(rawWorkers) && rawWorkers > 0) {
37+
function parseThreadWorkerCount(envValue, cpuCount) {
4838
const maxWorkers = Math.max(1, cpuCount)
49-
if (rawWorkers > maxWorkers) {
50-
console.warn(
51-
`[build] BUILD_THREAD_WORKERS=${rawWorkers} exceeds CPU count (${cpuCount}); capping to ${maxWorkers}`,
52-
)
53-
}
54-
threadWorkers = Math.min(rawWorkers, maxWorkers)
55-
} else {
56-
if (process.env.BUILD_THREAD_WORKERS) {
39+
if (envValue !== undefined && envValue !== null) {
40+
const raw = parseInt(envValue, 10)
41+
if (Number.isInteger(raw) && raw > 0) {
42+
if (raw > maxWorkers) {
43+
console.warn(
44+
`[build] BUILD_THREAD_WORKERS=${raw} exceeds CPU count (${cpuCount}); capping to ${maxWorkers}`,
45+
)
46+
}
47+
return Math.min(raw, maxWorkers)
48+
}
5749
console.warn(
58-
`[build] Invalid BUILD_THREAD_WORKERS="${process.env.BUILD_THREAD_WORKERS}", defaulting to CPU count (${cpuCount})`,
50+
`[build] Invalid BUILD_THREAD_WORKERS="${envValue}", defaulting to CPU count (${cpuCount})`,
5951
)
6052
}
61-
threadWorkers = cpuCount
53+
return maxWorkers
6254
}
55+
const threadWorkers = parseThreadWorkerCount(process.env.BUILD_THREAD_WORKERS, cpuCount)
6356
// Thread-loader pool timeout constants (allow override via env)
6457
let PRODUCTION_POOL_TIMEOUT_MS = 2000
6558
if (process.env.BUILD_POOL_TIMEOUT) {

0 commit comments

Comments
 (0)