@@ -24,19 +24,24 @@ import {
2424 cssExternalHandler ,
2525 isCssGlobalFile ,
2626} from './css/cssConfig' ;
27- import { pluginCjsShim } from './plugins/cjsShim' ;
27+ import {
28+ pluginCjsImportMetaUrlShim ,
29+ pluginEsmRequireShim ,
30+ } from './plugins/shims' ;
2831import type {
2932 AutoExternal ,
3033 BannerAndFooter ,
3134 Format ,
3235 LibConfig ,
3336 PkgJson ,
3437 Redirect ,
38+ ResolvedShims ,
3539 RsbuildConfigOutputTarget ,
3640 RslibConfig ,
3741 RslibConfigAsyncFn ,
3842 RslibConfigExport ,
3943 RslibConfigSyncFn ,
44+ Shims ,
4045 Syntax ,
4146} from './types' ;
4247import { getDefaultExtension } from './utils/extension' ;
@@ -447,12 +452,16 @@ export async function createConstantRsbuildConfig(): Promise<RsbuildConfig> {
447452
448453const composeFormatConfig = ( format : Format ) : RsbuildConfig => {
449454 const jsParserOptions = {
450- importMeta : false ,
451- requireResolve : false ,
452- requireDynamic : false ,
453- requireAsExpression : false ,
454- importDynamic : false ,
455- } ;
455+ cjs : {
456+ requireResolve : false ,
457+ requireDynamic : false ,
458+ requireAsExpression : false ,
459+ } ,
460+ esm : {
461+ importMeta : false ,
462+ importDynamic : false ,
463+ } ,
464+ } as const ;
456465
457466 switch ( format ) {
458467 case 'esm' :
@@ -461,7 +470,10 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
461470 rspack : {
462471 module : {
463472 parser : {
464- javascript : jsParserOptions ,
473+ javascript : {
474+ ...jsParserOptions . esm ,
475+ ...jsParserOptions . cjs ,
476+ } ,
465477 } ,
466478 } ,
467479 optimization : {
@@ -486,12 +498,11 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
486498 } ;
487499 case 'cjs' :
488500 return {
489- plugins : [ pluginCjsShim ( ) ] ,
490501 tools : {
491502 rspack : {
492503 module : {
493504 parser : {
494- javascript : jsParserOptions ,
505+ javascript : { ... jsParserOptions . esm , ... jsParserOptions . cjs } ,
495506 } ,
496507 } ,
497508 output : {
@@ -531,6 +542,85 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
531542 }
532543} ;
533544
545+ const resolveShims = ( shims ?: Shims ) => {
546+ const resolvedShims = {
547+ cjs : {
548+ 'import.meta.url' : true ,
549+ } ,
550+ esm : {
551+ __filename : true ,
552+ __dirname : true ,
553+ require : false ,
554+ } ,
555+ } ;
556+
557+ if ( ! shims ) {
558+ return resolvedShims ;
559+ }
560+
561+ if ( shims . cjs ) {
562+ if ( typeof shims . cjs === 'boolean' ) {
563+ if ( shims . cjs === true ) {
564+ resolvedShims . cjs [ 'import.meta.url' ] = true ;
565+ } else {
566+ resolvedShims . cjs [ 'import.meta.url' ] = false ;
567+ }
568+ } else {
569+ resolvedShims . cjs [ 'import.meta.url' ] =
570+ shims . cjs [ 'import.meta.url' ] ?? false ;
571+ }
572+ }
573+
574+ if ( shims . esm ) {
575+ if ( typeof shims . esm === 'boolean' ) {
576+ if ( shims . esm === true ) {
577+ resolvedShims . esm . __filename = true ;
578+ resolvedShims . esm . __dirname = true ;
579+ resolvedShims . esm . require = true ;
580+ }
581+ } else {
582+ resolvedShims . esm . __filename = shims . esm . __filename ?? false ;
583+ resolvedShims . esm . __dirname = shims . esm . __dirname ?? false ;
584+ resolvedShims . esm . require = shims . esm . require ?? false ;
585+ }
586+ }
587+
588+ return resolvedShims ;
589+ } ;
590+
591+ const composeShimsConfig = (
592+ format : Format ,
593+ resolvedShims : ResolvedShims ,
594+ ) : RsbuildConfig => {
595+ switch ( format ) {
596+ case 'esm' :
597+ return {
598+ tools : {
599+ rspack : {
600+ node : {
601+ // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`
602+ __dirname : resolvedShims . esm . __dirname ? 'node-module' : false ,
603+ __filename : resolvedShims . esm . __filename ? 'node-module' : false ,
604+ } ,
605+ } ,
606+ } ,
607+ plugins : [ resolvedShims . esm . require && pluginEsmRequireShim ( ) ] . filter (
608+ Boolean ,
609+ ) ,
610+ } ;
611+ case 'cjs' :
612+ return {
613+ plugins : [
614+ resolvedShims . cjs [ 'import.meta.url' ] && pluginCjsImportMetaUrlShim ( ) ,
615+ ] . filter ( Boolean ) ,
616+ } ;
617+ case 'umd' :
618+ return { } ;
619+ default :
620+ throw new Error ( `Unsupported format: ${ format } ` ) ;
621+ }
622+ } ;
623+
534624export const composeModuleImportWarn = ( request : string ) : string => {
535625 return `The externalized commonjs request ${ color . green ( `"${ request } "` ) } will use ${ color . blue ( '"module"' ) } external type in ESM format. If you want to specify other external type, considering set the request and type with ${ color . blue ( '"output.externals"' ) } .` ;
536626} ;
@@ -832,9 +922,6 @@ const composeTargetConfig = (
832922 tools : {
833923 rspack : {
834924 target : [ 'node' ] ,
835- // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
836- // and leave them as-is in the rest of the cases. Leave the comments here to explain the behavior.
837- // { node: { __dirname: ..., __filename: ... } }
838925 } ,
839926 } ,
840927 output : {
@@ -915,6 +1002,8 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
9151002 externalHelpers = false ,
9161003 redirect = { } ,
9171004 } = config ;
1005+ const resolvedShims = resolveShims ( config . shims ) ;
1006+ const shimsConfig = composeShimsConfig ( format ! , resolvedShims ) ;
9181007 const formatConfig = composeFormatConfig ( format ! ) ;
9191008 const externalHelpersConfig = composeExternalHelpersConfig (
9201009 externalHelpers ,
@@ -967,6 +1056,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
9671056
9681057 return mergeRsbuildConfig (
9691058 formatConfig ,
1059+ shimsConfig ,
9701060 externalHelpersConfig ,
9711061 // externalsWarnConfig should before other externals config
9721062 externalsWarnConfig ,
@@ -1046,6 +1136,7 @@ export async function composeCreateRsbuildConfig(
10461136 'banner' ,
10471137 'footer' ,
10481138 'dts' ,
1139+ 'shims' ,
10491140 ] ) ,
10501141 ) ,
10511142 } ;
0 commit comments