Skip to content

Commit e2d40a3

Browse files
authored
refactor: migrate inspectConfig functionality to a dedicated file (#5265)
1 parent fd53ecb commit e2d40a3

File tree

11 files changed

+274
-368
lines changed

11 files changed

+274
-368
lines changed

packages/compat/webpack/src/initConfigs.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type RsbuildProviderHelpers,
77
logger,
88
} from '@rsbuild/core';
9-
import { inspectConfig } from './inspectConfig.js';
109
import type { WebpackConfig } from './types.js';
1110
import { generateWebpackConfig } from './webpackConfig.js';
1211

@@ -48,13 +47,13 @@ export async function initConfigs({
4847
verbose: true,
4948
writeToDisk: true,
5049
};
51-
await inspectConfig({
50+
await helpers.inspectConfig<'webpack'>({
5251
context,
52+
bundler: 'webpack',
5353
pluginManager,
5454
inspectOptions,
5555
rsbuildOptions,
5656
bundlerConfigs: webpackConfigs,
57-
helpers,
5857
});
5958
};
6059

packages/compat/webpack/src/inspectConfig.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

packages/compat/webpack/src/provider.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { CreateCompiler, RsbuildProvider } from '@rsbuild/core';
22
import { build } from './build.js';
33
import { createCompiler as baseCreateCompiler } from './createCompiler.js';
44
import { initConfigs } from './initConfigs.js';
5-
import { inspectConfig } from './inspectConfig.js';
65
import { pluginAdaptor } from './plugin.js';
76

87
export const webpackProvider: RsbuildProvider<'webpack'> = async ({
@@ -88,12 +87,22 @@ export const webpackProvider: RsbuildProvider<'webpack'> = async ({
8887
},
8988

9089
async inspectConfig(inspectOptions) {
91-
return await inspectConfig({
90+
const bundlerConfigs = (
91+
await initConfigs({
92+
context,
93+
pluginManager,
94+
rsbuildOptions,
95+
helpers,
96+
})
97+
).webpackConfigs;
98+
99+
return await helpers.inspectConfig<'webpack'>({
92100
context,
93101
pluginManager,
102+
bundler: 'webpack',
103+
bundlerConfigs,
94104
rsbuildOptions,
95105
inspectOptions,
96-
helpers,
97106
});
98107
},
99108
};

packages/core/src/config.ts

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'node:fs';
22
import { createRequire } from 'node:module';
33
import { dirname, isAbsolute, join } from 'node:path';
44
import { pathToFileURL } from 'node:url';
5-
import RspackChain from '../compiled/rspack-chain';
65
import {
76
ASSETS_DIST_DIR,
87
CSS_DIST_DIR,
@@ -28,16 +27,12 @@ import {
2827
getNodeEnv,
2928
isFileExists,
3029
isObject,
31-
upperFirst,
3230
} from './helpers';
3331
import { logger } from './logger';
3432
import { mergeRsbuildConfig } from './mergeConfig';
3533
import type {
36-
InspectConfigOptions,
37-
InspectConfigResult,
3834
NormalizedConfig,
3935
NormalizedDevConfig,
40-
NormalizedEnvironmentConfig,
4136
NormalizedHtmlConfig,
4237
NormalizedOutputConfig,
4338
NormalizedPerformanceConfig,
@@ -46,13 +41,11 @@ import type {
4641
NormalizedServerConfig,
4742
NormalizedSourceConfig,
4843
NormalizedToolsConfig,
49-
PluginManager,
5044
PublicDir,
5145
PublicDirOptions,
5246
RsbuildConfig,
5347
RsbuildEntry,
5448
RsbuildMode,
55-
RsbuildPlugin,
5649
} from './types';
5750

5851
const require = createRequire(import.meta.url);
@@ -532,160 +525,6 @@ export async function loadConfig({
532525
};
533526
}
534527

535-
const normalizePluginObject = (plugin: RsbuildPlugin): RsbuildPlugin => {
536-
const { setup: _, ...rest } = plugin;
537-
return {
538-
...rest,
539-
// use empty `setup` function as it's not meaningful in inspect config
540-
setup() {},
541-
};
542-
};
543-
544-
export const getRsbuildInspectConfig = ({
545-
normalizedConfig,
546-
inspectOptions,
547-
pluginManager,
548-
}: {
549-
normalizedConfig: NormalizedConfig;
550-
inspectOptions: InspectConfigOptions;
551-
pluginManager: PluginManager;
552-
}): {
553-
rawRsbuildConfig: string;
554-
rsbuildConfig: InspectConfigResult['origin']['rsbuildConfig'];
555-
rawEnvironmentConfigs: Array<{
556-
name: string;
557-
content: string;
558-
}>;
559-
environmentConfigs: InspectConfigResult['origin']['environmentConfigs'];
560-
} => {
561-
const { environments, ...rsbuildConfig } = normalizedConfig;
562-
563-
const debugConfig: Omit<NormalizedConfig, 'environments'> = {
564-
...rsbuildConfig,
565-
plugins: pluginManager.getPlugins().map(normalizePluginObject),
566-
};
567-
568-
const rawRsbuildConfig = stringifyConfig(debugConfig, inspectOptions.verbose);
569-
const environmentConfigs: Record<string, NormalizedEnvironmentConfig> = {};
570-
571-
const rawEnvironmentConfigs: Array<{
572-
name: string;
573-
content: string;
574-
}> = [];
575-
576-
for (const [name, config] of Object.entries(environments)) {
577-
const debugConfig = {
578-
...config,
579-
plugins: pluginManager
580-
.getPlugins({ environment: name })
581-
.map(normalizePluginObject),
582-
};
583-
rawEnvironmentConfigs.push({
584-
name,
585-
content: stringifyConfig(debugConfig, inspectOptions.verbose),
586-
});
587-
environmentConfigs[name] = debugConfig;
588-
}
589-
590-
return {
591-
rsbuildConfig,
592-
rawRsbuildConfig,
593-
environmentConfigs: environments,
594-
rawEnvironmentConfigs,
595-
};
596-
};
597-
598-
export async function outputInspectConfigFiles({
599-
rawBundlerConfigs,
600-
rawEnvironmentConfigs,
601-
inspectOptions,
602-
configType,
603-
}: {
604-
configType: string;
605-
rawEnvironmentConfigs: Array<{
606-
name: string;
607-
content: string;
608-
}>;
609-
rawBundlerConfigs: Array<{
610-
name: string;
611-
content: string;
612-
}>;
613-
inspectOptions: InspectConfigOptions & {
614-
outputPath: string;
615-
};
616-
}): Promise<void> {
617-
const { outputPath } = inspectOptions;
618-
619-
const files = [
620-
...rawEnvironmentConfigs.map(({ name, content }) => {
621-
if (rawEnvironmentConfigs.length === 1) {
622-
const outputFile = 'rsbuild.config.mjs';
623-
const outputFilePath = join(outputPath, outputFile);
624-
625-
return {
626-
path: outputFilePath,
627-
label: 'Rsbuild config',
628-
content,
629-
};
630-
}
631-
const outputFile = `rsbuild.config.${name}.mjs`;
632-
const outputFilePath = join(outputPath, outputFile);
633-
634-
return {
635-
path: outputFilePath,
636-
label: `Rsbuild config (${name})`,
637-
content,
638-
};
639-
}),
640-
...rawBundlerConfigs.map(({ name, content }) => {
641-
const outputFile = `${configType}.config.${name}.mjs`;
642-
let outputFilePath = join(outputPath, outputFile);
643-
644-
// if filename is conflict, add a random id to the filename.
645-
if (fs.existsSync(outputFilePath)) {
646-
outputFilePath = outputFilePath.replace(/\.mjs$/, `.${Date.now()}.mjs`);
647-
}
648-
649-
return {
650-
path: outputFilePath,
651-
label: `${upperFirst(configType)} Config (${name})`,
652-
content,
653-
};
654-
}),
655-
];
656-
657-
await fs.promises.mkdir(outputPath, { recursive: true });
658-
659-
await Promise.all(
660-
files.map(async (item) => {
661-
return fs.promises.writeFile(item.path, `export default ${item.content}`);
662-
}),
663-
);
664-
665-
const fileInfos = files
666-
.map(
667-
(item) =>
668-
` - ${color.bold(color.yellow(item.label))}: ${color.underline(
669-
item.path,
670-
)}`,
671-
)
672-
.join('\n');
673-
674-
logger.success(
675-
`config inspection completed, generated files: \n\n${fileInfos}\n`,
676-
);
677-
}
678-
679-
export function stringifyConfig(config: unknown, verbose?: boolean): string {
680-
// webpackChain.toString can be used as a common stringify method
681-
const stringify = RspackChain.toString as (
682-
config: unknown,
683-
options: { verbose?: boolean },
684-
) => string;
685-
686-
return stringify(config, { verbose });
687-
}
688-
689528
export const normalizePublicDirs = (
690529
publicDir?: PublicDir,
691530
): Required<PublicDirOptions>[] => {

packages/core/src/createContext.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ export async function updateEnvironmentContext(
9898
context.environments ||= {};
9999

100100
for (const [index, [name, config]] of Object.entries(configs).entries()) {
101-
const { tsconfigPath } = config.source;
102101
const browserslist = await getBrowserslistByEnvironment(
103102
context.rootPath,
104103
config,
105104
);
106105

107-
const entry = config.source.entry ?? {};
106+
const { entry = {}, tsconfigPath } = config.source;
108107
const htmlPaths = getEnvironmentHTMLPaths(entry, config);
109108

110109
const environmentContext: EnvironmentContext = {

0 commit comments

Comments
 (0)