build: improve number of workers computation
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 22 Jul 2023 22:50:51 +0000 (00:50 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 22 Jul 2023 22:50:51 +0000 (00:50 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
rollup.config.mjs

index 6ee51a63bdfeb0a07f13f52361bc378785c11a36..63f8acc7bbc8285c72b23f55115e481e297ebd4f 100644 (file)
@@ -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: [