UNPKG

@teambit/bundler

Version:
77 lines (74 loc) 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dedupEnvs = dedupEnvs; /** * de-duping dev servers by the amount of type the dev server configuration was overridden by envs. * This will split the dev server to groups of dev server that share the same webpack config, and same peer dependencies * @param contexts * @param dependencyResolver * @param dedicatedEnvDevServers */ async function dedupEnvs(contexts, dependencyResolver, dedicatedEnvDevServers) { const idsGroups = groupByEnvId(contexts, dedicatedEnvDevServers); const hasRootComponents = dependencyResolver.hasRootComponents(); // Do not split envs by peers if root components is enabled as it should be already handled by the package manager // this will improve the performance of the dev server when root components is enabled const finalGroups = hasRootComponents ? idsGroups : await splitByPeers(idsGroups, dependencyResolver); return finalGroups; } function groupByEnvId(contexts, dedicatedEnvDevServers) { const groupedEnvs = {}; contexts.forEach(context => { const envId = getEnvId(context, dedicatedEnvDevServers); if (!envId) return; if (!(envId in groupedEnvs)) groupedEnvs[envId] = []; groupedEnvs[envId].push(context); }); return groupedEnvs; } async function splitByPeers(idsGroups, dependencyResolver) { const newGroupedEnvs = {}; const promises = Object.values(idsGroups).map(async contexts => { const peersGroups = await groupByPeersHash(contexts, dependencyResolver); Object.assign(newGroupedEnvs, peersGroups); }); await Promise.all(promises); return newGroupedEnvs; } function getEnvId(context, dedicatedServers) { const id = context.id.split('@')[0]; if (dedicatedServers?.includes(id)) { return context.id; } // Happen for envs that are not implementing preview if (!context.env?.getDevEnvId) { return undefined; } return context.env?.getDevEnvId(context); } async function groupByPeersHash(contexts, dependencyResolver) { const peerGroups = {}; await Promise.all(contexts.map(async context => { const env = context.env; const policy = await dependencyResolver.getComponentEnvPolicyFromEnv(env, { envId: context.id }); const peersHash = policy.byLifecycleType('peer').hashNameVersion(); if (!peerGroups[peersHash]) { peerGroups[peersHash] = []; } peerGroups[peersHash].push(context); })); return indexPeerGroupsById(peerGroups); } function indexPeerGroupsById(peerGroups) { const result = Object.values(peerGroups).reduce((acc, contexts) => { const firstId = contexts[0].id; acc[firstId] = contexts; return acc; }, {}); return result; } //# sourceMappingURL=dedup-envs.js.map