Skip to content

Commit 3a17836

Browse files
committed
perf(rstack): load config once during rs check
1 parent 636c98a commit 3a17836

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

‎packages/rstack/src/cli/commands.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join, resolve } from 'node:path';
2-
import { getConfigState } from '../config.ts';
2+
import { pathToFileURL } from 'node:url';
3+
import { getConfigState, type LoadedRstackConfig } from '../config.ts';
34
import { insertConfigArg, parseArgs, parseCliArgs } from './args.ts';
45
import { hasHelpFlag, printCommandHelp } from './help.ts';
56

@@ -138,6 +139,8 @@ async function runRspressCLI(args: string[]): Promise<void> {
138139
}
139140
}
140141

142+
const RSLINT_CONFIG_PATH = join(import.meta.dirname, 'rslintConfig.js');
143+
141144
async function runRslintCLI(args: string[]): Promise<void> {
142145
if (hasHelpFlag(args)) {
143146
return printCommandHelp('lint');
@@ -146,17 +149,23 @@ async function runRslintCLI(args: string[]): Promise<void> {
146149
const argv = [
147150
process.execPath,
148151
'rslint',
149-
...insertConfigArg(
150-
args,
151-
'--config',
152-
join(import.meta.dirname, 'rslintConfig.js'),
153-
),
152+
...insertConfigArg(args, '--config', RSLINT_CONFIG_PATH),
154153
];
155154

156155
const { runCLI } = await import('@rslint/core');
157156
await runCLI({ argv });
158157
}
159158

159+
const getLoadedRslintRstackConfig = async (): Promise<LoadedRstackConfig> => {
160+
// Rslint loads its one-shot config through Node's module cache. Import the
161+
// same URL to read the Rstack config exported for the following fmt phase.
162+
const configModule = (await import(
163+
pathToFileURL(RSLINT_CONFIG_PATH).href
164+
)) as typeof import('../rslintConfig.ts');
165+
166+
return configModule.loadedConfig;
167+
};
168+
160169
async function runCheckCLI(args: string[]): Promise<void> {
161170
const { values } = parseArgs({
162171
args,
@@ -177,11 +186,12 @@ async function runCheckCLI(args: string[]): Promise<void> {
177186
return;
178187
}
179188

189+
const loadedConfig = await getLoadedRslintRstackConfig();
180190
const { runFmtCLI } = await import(
181191
/* rspackChunkName: 'fmt' */
182192
'../fmt/cli.ts'
183193
);
184-
await runFmtCLI(['--check']);
194+
await runFmtCLI(['--check'], { loadedConfig });
185195
}
186196

187197
export async function setupCommands(): Promise<void> {

‎packages/rstack/src/fmt/cli.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { performance } from 'node:perf_hooks';
33
import { color, logger } from 'rslog';
44
import { parseArgs } from '../cli/args.ts';
55
import { printCommandHelp } from '../cli/help.ts';
6-
import { loadRstackConfig } from '../config.ts';
6+
import { loadRstackConfig, type LoadedRstackConfig } from '../config.ts';
77
import { ensureProjectCacheDir } from '../projectCache.ts';
88
import { fmtCacheFileName } from './cacheStore.ts';
99
import { resolveFmtConfig } from './config.ts';
@@ -29,6 +29,11 @@ interface ParsedFmtCLIArgs {
2929
lsp: boolean;
3030
}
3131

32+
type RunFmtCLIOptions = {
33+
/** Rstack config already loaded by the lint phase of `rs check`. */
34+
loadedConfig?: LoadedRstackConfig;
35+
};
36+
3237
const parseMaxWorkers = (value: string | undefined): number | undefined => {
3338
if (value === undefined) {
3439
return undefined;
@@ -255,8 +260,12 @@ const logFmtResult = (
255260
}
256261
};
257262

258-
const loadFmtConfig = async (cwd: string): Promise<ResolvedFmtConfig> => {
259-
const { configs, filePath } = await loadRstackConfig({ cwd });
263+
const loadFmtConfig = async (
264+
cwd: string,
265+
loadedConfig?: LoadedRstackConfig,
266+
): Promise<ResolvedFmtConfig> => {
267+
const { configs, filePath } =
268+
loadedConfig ?? (await loadRstackConfig({ cwd }));
260269

261270
return resolveFmtConfig({
262271
definition: configs.fmt,
@@ -265,7 +274,10 @@ const loadFmtConfig = async (cwd: string): Promise<ResolvedFmtConfig> => {
265274
});
266275
};
267276

268-
const runFmtCLI = async (args: string[]): Promise<void> => {
277+
const runFmtCLI = async (
278+
args: string[],
279+
{ loadedConfig }: RunFmtCLIOptions = {},
280+
): Promise<void> => {
269281
const cwd = process.cwd();
270282
const startTime = performance.now();
271283

@@ -337,7 +349,7 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
337349
}
338350
}
339351

340-
const config = await loadFmtConfig(cwd);
352+
const config = await loadFmtConfig(cwd, loadedConfig);
341353
const files = await discoverFmtFiles({
342354
cwd,
343355
patterns,

‎packages/rstack/src/rslintConfig.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { loadRstackConfig } from './config.ts';
1+
import { loadRstackConfig, type LoadedRstackConfig } from './config.ts';
22
import type { RslintConfig } from '@rslint/core';
33

4-
const { configs } = await loadRstackConfig();
4+
// Expose the loaded config so `rs check` can pass it to fmt instead of loading
5+
// and executing the Rstack config a second time.
6+
export const loadedConfig: LoadedRstackConfig = await loadRstackConfig();
7+
const { configs } = loadedConfig;
58
const lintDefinition = configs.lint ?? [];
69

710
let lintConfig: RslintConfig;

0 commit comments

Comments
 (0)