Skip to content

Commit 45a6aa9

Browse files
authored
perf: pre-filter test projects to avoid unnecessary builds (#1092)
1 parent e7ec36c commit 45a6aa9

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

packages/core/src/core/listTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const collectNodeTests = async ({
7373
globTestSourceEntries,
7474
setupFiles,
7575
globalSetupFiles,
76+
nodeProjects,
7677
);
7778

7879
const { getRsbuildStats, closeServer } = await createRsbuildServer({

packages/core/src/core/rsbuild.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import path from 'pathe';
1111
import type {
1212
EntryInfo,
1313
NormalizedProjectConfig,
14+
ProjectContext,
1415
RstestContext,
1516
} from '../types';
1617
import { isDebug } from '../utils';
@@ -65,16 +66,23 @@ export const prepareRsbuild = async (
6566
globTestSourceEntries: (name: string) => Promise<Record<string, string>>,
6667
setupFiles: Record<string, Record<string, string>>,
6768
globalSetupFiles: Record<string, Record<string, string>>,
69+
/**
70+
* Explicit list of node-mode projects to include in the Rsbuild instance.
71+
* When provided, only these projects will be compiled.
72+
*/
73+
targetNodeProjects?: ProjectContext[],
6874
): Promise<RsbuildInstance> => {
6975
const {
7076
command,
7177
normalizedConfig: { isolate, dev = {}, coverage, pool },
7278
} = context;
7379

7480
// Filter out browser mode projects - this rsbuild is for node mode only
75-
const projects = context.projects.filter(
76-
(project) => !project.normalizedConfig.browser.enabled,
77-
);
81+
const projects = targetNodeProjects?.length
82+
? targetNodeProjects
83+
: context.projects.filter(
84+
(project) => !project.normalizedConfig.browser.enabled,
85+
);
7886
const debugMode = isDebug();
7987

8088
RsbuildLogger.level = debugMode ? 'verbose' : 'error';

packages/core/src/core/runTests.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,24 @@ export async function runTests(context: Rstest): Promise<void> {
142142
let browserProjectsToRun = browserProjects;
143143
let nodeProjectsToRun = nodeProjects;
144144

145-
if (shard) {
145+
// In non-watch mode, proactively skip projects with no test files to avoid unnecessary builds
146+
if (!isWatchMode) {
147+
// Populate entries cache for all projects
148+
await Promise.all(
149+
allProjects.map((p) => globTestSourceEntries(p.environmentName)),
150+
);
151+
152+
const hasEntries = (env: string) =>
153+
Object.keys(entriesCache.get(env)?.entries || {}).length > 0;
154+
155+
browserProjectsToRun = browserProjects.filter((p) =>
156+
hasEntries(p.environmentName),
157+
);
158+
nodeProjectsToRun = nodeProjects.filter((p) =>
159+
hasEntries(p.environmentName),
160+
);
161+
} else if (shard) {
162+
// In watch mode with sharding, only run projects that have sharded entries
146163
browserProjectsToRun = browserProjects.filter((p) => {
147164
return (
148165
Object.keys(entriesCache.get(p.environmentName)?.entries || {}).length >
@@ -232,6 +249,7 @@ export async function runTests(context: Rstest): Promise<void> {
232249
globTestSourceEntries,
233250
setupFiles,
234251
globalSetupFiles,
252+
projects,
235253
);
236254

237255
const { getRsbuildStats, closeServer } = await createRsbuildServer({

0 commit comments

Comments
 (0)