From eadc058ca00ba1593df48ed2fa4e69f60707c9aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 23 Jul 2023 00:50:51 +0200 Subject: [PATCH] build: improve number of workers computation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- rollup.config.mjs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 6ee51a63..63f8acc7 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,5 +1,5 @@ /* eslint-disable n/no-unpublished-import */ -import { cpus } from 'node:os'; +import * as os from 'node:os'; import json from '@rollup/plugin-json'; import terser from '@rollup/plugin-terser'; @@ -8,6 +8,20 @@ import { copy } from '@web/rollup-plugin-copy'; import analyze from 'rollup-plugin-analyzer'; import del from 'rollup-plugin-delete'; +const availableParallelism = () => { + // eslint-disable-next-line no-shadow + let availableParallelism = 1; + try { + availableParallelism = os.availableParallelism(); + } catch { + const numberOfCpus = os.cpus(); + if (Array.isArray(numberOfCpus) && numberOfCpus.length > 0) { + availableParallelism = numberOfCpus.length; + } + } + return availableParallelism; +}; + const isDevelopmentBuild = process.env.BUILD === 'development'; const isAnalyzeBuild = process.env.ANALYZE; @@ -19,7 +33,7 @@ export default { dir: 'dist', format: 'esm', sourcemap: !!isDevelopmentBuild, - plugins: [terser({ maxWorkers: Math.floor(cpus().length / 2) })], + plugins: [terser({ maxWorkers: Math.floor(availableParallelism() / 2) })], }, ], external: [ -- 2.34.1