Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/rstack/src/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { getConfigState } from '../config.ts';
import { insertConfigArg, parseArgs, parseCliArgs } from './args.ts';
import { hasHelpFlag, printCommandHelp } from './help.ts';
Expand Down Expand Up @@ -138,6 +139,8 @@ async function runRspressCLI(args: string[]): Promise<void> {
}
}

const RSLINT_CONFIG_PATH = join(import.meta.dirname, 'rslintConfig.js');

async function runRslintCLI(args: string[]): Promise<void> {
if (hasHelpFlag(args)) {
return printCommandHelp('lint');
Expand All @@ -146,11 +149,7 @@ async function runRslintCLI(args: string[]): Promise<void> {
const argv = [
process.execPath,
'rslint',
...insertConfigArg(
args,
'--config',
join(import.meta.dirname, 'rslintConfig.js'),
),
...insertConfigArg(args, '--config', RSLINT_CONFIG_PATH),
];

const { runCLI } = await import('@rslint/core');
Expand All @@ -177,11 +176,16 @@ async function runCheckCLI(args: string[]): Promise<void> {
return;
}

// Rslint loads its one-shot config through Node's module cache. Import the
// same URL to read the Rstack config exported for the following fmt phase.
const { loadedConfig } = (await import(
pathToFileURL(RSLINT_CONFIG_PATH).href
)) as typeof import('../rslintConfig.ts');
const { runFmtCLI } = await import(
/* rspackChunkName: 'fmt' */
'../fmt/cli.ts'
);
await runFmtCLI(['--check']);
await runFmtCLI(['--check'], { loadedConfig });
}

export async function setupCommands(): Promise<void> {
Expand Down
22 changes: 17 additions & 5 deletions packages/rstack/src/fmt/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { performance } from 'node:perf_hooks';
import { color, logger } from 'rslog';
import { parseArgs } from '../cli/args.ts';
import { printCommandHelp } from '../cli/help.ts';
import { loadRstackConfig } from '../config.ts';
import { loadRstackConfig, type LoadedRstackConfig } from '../config.ts';
import { ensureProjectCacheDir } from '../projectCache.ts';
import { fmtCacheFileName } from './cacheStore.ts';
import { resolveFmtConfig } from './config.ts';
Expand All @@ -29,6 +29,11 @@ interface ParsedFmtCLIArgs {
lsp: boolean;
}

type RunFmtCLIOptions = {
/** Rstack config already loaded by the lint phase of `rs check`. */
loadedConfig?: LoadedRstackConfig;
};

const parseMaxWorkers = (value: string | undefined): number | undefined => {
if (value === undefined) {
return undefined;
Expand Down Expand Up @@ -255,8 +260,12 @@ const logFmtResult = (
}
};

const loadFmtConfig = async (cwd: string): Promise<ResolvedFmtConfig> => {
const { configs, filePath } = await loadRstackConfig({ cwd });
const loadFmtConfig = async (
cwd: string,
loadedConfig?: LoadedRstackConfig,
): Promise<ResolvedFmtConfig> => {
const { configs, filePath } =
loadedConfig ?? (await loadRstackConfig({ cwd }));

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

const runFmtCLI = async (args: string[]): Promise<void> => {
const runFmtCLI = async (
args: string[],
{ loadedConfig }: RunFmtCLIOptions = {},
): Promise<void> => {
const cwd = process.cwd();
const startTime = performance.now();

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

const config = await loadFmtConfig(cwd);
const config = await loadFmtConfig(cwd, loadedConfig);
const files = await discoverFmtFiles({
cwd,
patterns,
Expand Down
7 changes: 5 additions & 2 deletions packages/rstack/src/rslintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { loadRstackConfig } from './config.ts';
import { loadRstackConfig, type LoadedRstackConfig } from './config.ts';
import type { RslintConfig } from '@rslint/core';

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

let lintConfig: RslintConfig;
Expand Down