Skip to content

Commit ceea9d5

Browse files
Consolidate boolean env parsing and cache Sass implementation
- Replace parseBooleanEnv/isDisabledEnv with getBooleanEnv to unify boolean env handling while preserving per-flag defaults - Resolve and cache Sass implementation once per process to avoid repeated dynamic imports across build variants These changes address review suggestions r2331670931 and r2331670979 and keep behavior stable with minimal diff
1 parent ac0ecee commit ceea9d5

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

build.mjs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ const __dirname = path.resolve()
1515
const isProduction = process.argv[2] !== '--development' // --production and --analyze are both production
1616
const isAnalyzing = process.argv[2] === '--analyze'
1717
// Env helpers
18-
function parseBooleanEnv(val) {
19-
if (val == null) return false
18+
function getBooleanEnv(val, defaultValue) {
19+
if (val == null) return defaultValue
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-
}
2823
// Default: parallel build ON unless explicitly disabled
29-
const parallelBuild = !isDisabledEnv(process.env.BUILD_PARALLEL)
30-
const isWatchOnce = parseBooleanEnv(process.env.BUILD_WATCH_ONCE)
24+
const parallelBuild = getBooleanEnv(process.env.BUILD_PARALLEL, true)
25+
const isWatchOnce = getBooleanEnv(process.env.BUILD_WATCH_ONCE, false)
3126
// Cache compression control: default none; allow override via env
3227
function parseCacheCompressionOption(envVal) {
3328
if (envVal == null) return undefined
@@ -77,7 +72,30 @@ if (process.env.BUILD_POOL_TIMEOUT) {
7772
}
7873
}
7974
// Enable threads by default; allow disabling via BUILD_THREAD=0/false/no/off
80-
const enableThread = !isDisabledEnv(process.env.BUILD_THREAD)
75+
const enableThread = getBooleanEnv(process.env.BUILD_THREAD, true)
76+
77+
// Cache and resolve Sass implementation once per process
78+
let sassImplPromise
79+
async function getSassImplementation() {
80+
if (!sassImplPromise) {
81+
sassImplPromise = (async () => {
82+
try {
83+
const mod = await import('sass-embedded')
84+
return mod.default || mod
85+
} catch (e1) {
86+
try {
87+
const mod = await import('sass')
88+
return mod.default || mod
89+
} catch (e2) {
90+
console.error('[build] Failed to load sass-embedded:', e1 && e1.message ? e1.message : e1)
91+
console.error('[build] Failed to load sass:', e2 && e2.message ? e2.message : e2)
92+
throw new Error("No Sass implementation available. Install 'sass-embedded' or 'sass'.")
93+
}
94+
}
95+
})()
96+
}
97+
return sassImplPromise
98+
}
8199

82100
async function deleteOldDir() {
83101
await fs.rm(outdir, { recursive: true, force: true })
@@ -98,18 +116,7 @@ async function runWebpack(isWithoutKatex, isWithoutTiktoken, minimal, sourceBuil
98116
]
99117
if (isWithoutKatex) shared.push('./src/components')
100118

101-
let sassImpl
102-
try {
103-
const mod = await import('sass-embedded')
104-
sassImpl = mod.default || mod
105-
} catch (e1) {
106-
try {
107-
const mod = await import('sass')
108-
sassImpl = mod.default || mod
109-
} catch (e2) {
110-
throw new Error("No Sass implementation available. Install 'sass-embedded' or 'sass'.")
111-
}
112-
}
119+
const sassImpl = await getSassImplementation()
113120

114121
const dirKey = path.basename(sourceBuildDir || outdir)
115122
const variantParts = [

0 commit comments

Comments
 (0)