@@ -2,7 +2,6 @@ import fs from 'node:fs';
22import { createRequire } from 'node:module' ;
33import { dirname , isAbsolute , join } from 'node:path' ;
44import { pathToFileURL } from 'node:url' ;
5- import RspackChain from '../compiled/rspack-chain' ;
65import {
76 ASSETS_DIST_DIR ,
87 CSS_DIST_DIR ,
@@ -28,16 +27,12 @@ import {
2827 getNodeEnv ,
2928 isFileExists ,
3029 isObject ,
31- upperFirst ,
3230} from './helpers' ;
3331import { logger } from './logger' ;
3432import { mergeRsbuildConfig } from './mergeConfig' ;
3533import 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
5851const 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 ( / \. m j s $ / , `.${ 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-
689528export const normalizePublicDirs = (
690529 publicDir ?: PublicDir ,
691530) : Required < PublicDirOptions > [ ] => {
0 commit comments