Skip to content

Commit 5663e3c

Browse files
Harden env flags and simplify CPU detection
Treat 'false/no/off/0' as disabled for BUILD_PARALLEL and BUILD_THREAD while keeping default enabled when unset. Replace defensive cpu count logic with os.cpus().length guarded by try/catch. This aligns behavior with documented defaults and improves robustness without changing outputs.
1 parent 3a3ff77 commit 5663e3c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

build.mjs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ const outdir = 'build'
1414
const __dirname = path.resolve()
1515
const isProduction = process.argv[2] !== '--development' // --production and --analyze are both production
1616
const isAnalyzing = process.argv[2] === '--analyze'
17-
const parallelBuild = process.env.BUILD_PARALLEL !== '0'
17+
// Env helpers
1818
function parseBooleanEnv(val) {
1919
if (val == null) return false
2020
const s = String(val).trim().toLowerCase()
2121
return !(s === '' || s === '0' || s === 'false' || s === 'no' || s === 'off')
2222
}
23+
function isDisabledEnv(val) {
24+
if (val == null) return false
25+
const s = String(val).trim().toLowerCase()
26+
return s === '' || s === '0' || s === 'false' || s === 'no' || s === 'off'
27+
}
28+
// Default: parallel build ON unless explicitly disabled
29+
const parallelBuild = !isDisabledEnv(process.env.BUILD_PARALLEL)
2330
const isWatchOnce = parseBooleanEnv(process.env.BUILD_WATCH_ONCE)
2431
// Cache compression control: default none; allow override via env
2532
function parseCacheCompressionOption(envVal) {
@@ -33,9 +40,8 @@ function parseCacheCompressionOption(envVal) {
3340
const cacheCompressionOption = parseCacheCompressionOption(process.env.BUILD_CACHE_COMPRESSION)
3441
let cpuCount = 1
3542
try {
36-
const cpuInfo = os.cpus && os.cpus()
37-
const len = Array.isArray(cpuInfo) ? cpuInfo.length : 0
38-
cpuCount = Number.isInteger(len) && len > 0 ? len : 1
43+
// os.cpus() returns an array in Node.js; guard with try/catch for portability
44+
cpuCount = Math.max(1, os.cpus().length || 1)
3945
} catch {
4046
cpuCount = 1
4147
}
@@ -70,8 +76,8 @@ if (process.env.BUILD_POOL_TIMEOUT) {
7076
)
7177
}
7278
}
73-
// Enable threads by default; allow disabling via BUILD_THREAD=0
74-
const enableThread = process.env.BUILD_THREAD !== '0'
79+
// Enable threads by default; allow disabling via BUILD_THREAD=0/false/no/off
80+
const enableThread = !isDisabledEnv(process.env.BUILD_THREAD)
7581

7682
async function deleteOldDir() {
7783
await fs.rm(outdir, { recursive: true, force: true })

0 commit comments

Comments
 (0)