build: improve maxWorkers computation
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 22 Jul 2023 22:44:09 +0000 (00:44 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 22 Jul 2023 22:44:09 +0000 (00:44 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
benchmarks/benchmarks-utils.mjs
rollup.config.mjs

index c88ada12fa61eda4c474b4142bbd81dbb59ae06d..8819c59255eb4cb6925e7463760695c2872924e8 100644 (file)
@@ -1,5 +1,5 @@
-import crypto from 'crypto'
-import fs from 'fs'
+import crypto from 'node:crypto'
+import fs from 'node:fs'
 import {
   DynamicClusterPool,
   DynamicThreadPool,
index 28b89d86ba9a550a80637b5d18217f569f42ec0d..e40f170d422e4b09a18f5e64f975a24d2b15bfb2 100644 (file)
@@ -1,15 +1,28 @@
-import { cpus } from 'node:os'
+import * as os from 'node:os'
 import terser from '@rollup/plugin-terser'
 import typescript from '@rollup/plugin-typescript'
 import analyze from 'rollup-plugin-analyzer'
 import command from 'rollup-plugin-command'
 import del from 'rollup-plugin-delete'
 
+const availableParallelism = () => {
+  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
 const isDocumentationBuild = process.env.DOCUMENTATION
 
-const maxWorkers = Math.floor(cpus().length / 2)
+const maxWorkers = Math.floor(availableParallelism() / 2)
 
 export default {
   input: 'src/index.ts',