forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
42 lines (37 loc) · 1.19 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable import/no-nodejs-modules */
const { cpus } = require( 'os' );
/**
* Get an env var that should be a positive integer greater than 0
*
* @param {string} envVarName Environment variable name
* @param {number} defaultValue Fallback in case env variable isn't present or invalid
* @returns {number} Value
*/
function getEnvVarAsNaturalNumber( envVarName, defaultValue ) {
if ( typeof envVarName !== 'string' ) {
throw new TypeError( 'Expected string environment variable name' );
}
if ( typeof defaultValue !== 'number' ) {
throw new TypeError( 'Expected number defaultValue' );
}
if ( process.env[ envVarName ] && ! Number.isNaN( parseInt( process.env[ envVarName ], 10 ) ) ) {
return Math.max( 1, parseInt( process.env[ envVarName ], 10 ) );
}
return defaultValue;
}
let workerCount;
if ( process.env.CIRCLECI ) {
workerCount = 2;
} else {
workerCount = getEnvVarAsNaturalNumber(
'WORKERS',
Math.min( Math.max( cpus().length - 1, 2 ), 32 )
);
}
const concurrentBuilds = getEnvVarAsNaturalNumber( 'CONCURRENT_BUILDS', 1 );
if ( concurrentBuilds > 1 ) {
workerCount = Math.max( 1, Math.floor( workerCount / concurrentBuilds ) );
}
module.exports = {
workerCount,
};