import * as threadLoader from 'thread-loader' ; import { getPool } from 'thread-loader/dist/workerPools' ; import os from 'os' ; import type { ConfigManager } from '../../../config/configManager' ; function getCpuNumber ( ) { return ( os . cpus ( ) || { length : 1 } ) . length ; } function calculateNumberOfWorkers ( ) { const cpus = getCpuNumber ( ) ; return Math . max ( 1 , cpus / 2 - 1 ) ; } const getCustomConfig = ( configManager ) => { const { build : { configurations : { threadLoader : additionalConfig } , } , } = configManager ; return additionalConfig || { } ; } ; const createBabelConfig = ( configManager ) => { return { name : 'babel' , poolTimeout : configManager . env === 'development' ? Infinity : undefined , workers : calculateNumberOfWorkers ( ) , ... getCustomConfig ( configManager ) , } ; } ; export const createWorkerPoolBabel = ( configManager ) => { const config = createBabelConfig ( configManager ) ; if ( ! ( createWorkerPoolBabel as any ) . warmup ) { threadLoader . warmup ( config , [ 'babel-loader' , '@babel/preset-env' , '@babel/preset-typescript' , '@babel/preset-react' , '@babel/plugin-transform-runtime' , 'babel-plugin-lodash' , ] ) ; ( createWorkerPoolBabel as any ) . warmup = true ; } return config ; } ; export const closeWorkerPoolBabel = ( configManager ) => { getPool ( createBabelConfig ( configManager ) ) ?. disposeWorkers ( ) ; } ; const createStylesConfig = ( configManager ) => { return { name : 'styles' , workerParallelJobs : 2 , workers : Math . max ( getCpuNumber ( ) , 2 ) , poolTimeout : configManager . env === 'development' ? Infinity : undefined , ... getCustomConfig ( configManager ) , } ; } ; export const createWorkerPoolStyles = ( configManager ) => { const config = createStylesConfig ; if ( ! ( createWorkerPoolStyles as any ) . warmup ) { threadLoader . warmup ( config , [ 'postcss-loader' ] ) ; ( createWorkerPoolStyles as any ) . warmup = true ; } return config ; } ; export const closeWorkerPoolStyles = ( configManager ) => { getPool ( createStylesConfig ( configManager ) ) ?. disposeWorkers ( ) ; } ;