From ce105693314cf94b86236dbfc10b9b6ee35b1724 Mon Sep 17 00:00:00 2001
From: Spencer <email@spalger.com>
Date: Mon, 24 Feb 2020 18:45:15 -0700
Subject: [PATCH] [kbn/optimizer] disable parallelization in terser plugin
 (#58396) (#58421)

* [kbn/optimizer] disable parallelization in terer plugin

* use more workers when building the dist

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
---
 .../kbn-optimizer/src/optimizer/optimizer_config.ts   | 11 ++++++++++-
 packages/kbn-optimizer/src/worker/webpack.config.ts   |  1 +
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/packages/kbn-optimizer/src/optimizer/optimizer_config.ts b/packages/kbn-optimizer/src/optimizer/optimizer_config.ts
index a258e1010fce3..1c8ae265bf6bb 100644
--- a/packages/kbn-optimizer/src/optimizer/optimizer_config.ts
+++ b/packages/kbn-optimizer/src/optimizer/optimizer_config.ts
@@ -25,6 +25,15 @@ import { Bundle, WorkerConfig } from '../common';
 import { findKibanaPlatformPlugins, KibanaPlatformPlugin } from './kibana_platform_plugins';
 import { getBundles } from './get_bundles';
 
+function pickMaxWorkerCount(dist: boolean) {
+  // don't break if cpus() returns nothing, or an empty array
+  const cpuCount = Math.max(Os.cpus()?.length, 1);
+  // if we're buiding the dist then we can use more of the system's resources to get things done a little quicker
+  const maxWorkers = dist ? cpuCount - 1 : Math.ceil(cpuCount / 3);
+  // ensure we always have at least two workers
+  return Math.max(maxWorkers, 2);
+}
+
 interface Options {
   /** absolute path to root of the repo/build */
   repoRoot: string;
@@ -110,7 +119,7 @@ export class OptimizerConfig {
 
     const maxWorkerCount = process.env.KBN_OPTIMIZER_MAX_WORKERS
       ? parseInt(process.env.KBN_OPTIMIZER_MAX_WORKERS, 10)
-      : options.maxWorkerCount ?? Math.max(Math.ceil(Math.max(Os.cpus()?.length, 1) / 3), 2);
+      : options.maxWorkerCount ?? pickMaxWorkerCount(dist);
     if (typeof maxWorkerCount !== 'number' || !Number.isFinite(maxWorkerCount)) {
       throw new TypeError('worker count must be a number');
     }
diff --git a/packages/kbn-optimizer/src/worker/webpack.config.ts b/packages/kbn-optimizer/src/worker/webpack.config.ts
index 74e0eca356f48..24f6c22519cce 100644
--- a/packages/kbn-optimizer/src/worker/webpack.config.ts
+++ b/packages/kbn-optimizer/src/worker/webpack.config.ts
@@ -248,6 +248,7 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
           cache: false,
           sourceMap: false,
           extractComments: false,
+          parallel: false,
           terserOptions: {
             compress: false,
             mangle: false,