@@ -24,7 +24,10 @@ 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 ,
@@ -37,6 +40,7 @@ import type {
3740 RslibConfigAsyncFn ,
3841 RslibConfigExport ,
3942 RslibConfigSyncFn ,
43+ Shims ,
4044 Syntax ,
4145} from './types' ;
4246import { getDefaultExtension } from './utils/extension' ;
@@ -447,12 +451,16 @@ export async function createConstantRsbuildConfig(): Promise<RsbuildConfig> {
447451
448452const composeFormatConfig = ( format : Format ) : RsbuildConfig => {
449453 const jsParserOptions = {
450- importMeta : false ,
451- requireResolve : false ,
452- requireDynamic : false ,
453- requireAsExpression : false ,
454- importDynamic : false ,
455- } ;
454+ cjs : {
455+ requireResolve : false ,
456+ requireDynamic : false ,
457+ requireAsExpression : false ,
458+ } ,
459+ esm : {
460+ importMeta : false ,
461+ importDynamic : false ,
462+ } ,
463+ } as const ;
456464
457465 switch ( format ) {
458466 case 'esm' :
@@ -461,7 +469,10 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
461469 rspack : {
462470 module : {
463471 parser : {
464- javascript : jsParserOptions ,
472+ javascript : {
473+ ...jsParserOptions . esm ,
474+ ...jsParserOptions . cjs ,
475+ } ,
465476 } ,
466477 } ,
467478 optimization : {
@@ -486,12 +497,11 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
486497 } ;
487498 case 'cjs' :
488499 return {
489- plugins : [ pluginCjsShim ( ) ] ,
490500 tools : {
491501 rspack : {
492502 module : {
493503 parser : {
494- javascript : jsParserOptions ,
504+ javascript : { ... jsParserOptions . esm , ... jsParserOptions . cjs } ,
495505 } ,
496506 } ,
497507 output : {
@@ -531,6 +541,47 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
531541 }
532542} ;
533543
544+ const composeShimsConfig = ( format : Format , shims ?: Shims ) : RsbuildConfig => {
545+ const resolvedShims = {
546+ cjs : {
547+ 'import.meta.url' : shims ?. cjs ?. [ 'import.meta.url' ] ?? true ,
548+ } ,
549+ esm : {
550+ __filename : shims ?. esm ?. __filename ?? false ,
551+ __dirname : shims ?. esm ?. __dirname ?? false ,
552+ require : shims ?. esm ?. require ?? false ,
553+ } ,
554+ } ;
555+
556+ switch ( format ) {
557+ case 'esm' :
558+ return {
559+ tools : {
560+ rspack : {
561+ node : {
562+ // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`
563+ __dirname : resolvedShims . esm . __dirname ? 'node-module' : false ,
564+ __filename : resolvedShims . esm . __filename ? 'node-module' : false ,
565+ } ,
566+ } ,
567+ } ,
568+ plugins : [ resolvedShims . esm . require && pluginEsmRequireShim ( ) ] . filter (
569+ Boolean ,
570+ ) ,
571+ } ;
572+ case 'cjs' :
573+ return {
574+ plugins : [
575+ resolvedShims . cjs [ 'import.meta.url' ] && pluginCjsImportMetaUrlShim ( ) ,
576+ ] . filter ( Boolean ) ,
577+ } ;
578+ case 'umd' :
579+ return { } ;
580+ default :
581+ throw new Error ( `Unsupported format: ${ format } ` ) ;
582+ }
583+ } ;
584+
534585export const composeModuleImportWarn = ( request : string ) : string => {
535586 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"' ) } .` ;
536587} ;
@@ -832,9 +883,6 @@ const composeTargetConfig = (
832883 tools : {
833884 rspack : {
834885 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: ... } }
838886 } ,
839887 } ,
840888 output : {
@@ -908,13 +956,15 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
908956
909957 const {
910958 format,
959+ shims,
911960 banner = { } ,
912961 footer = { } ,
913962 autoExtension = true ,
914963 autoExternal = true ,
915964 externalHelpers = false ,
916965 redirect = { } ,
917966 } = config ;
967+ const shimsConfig = composeShimsConfig ( format ! , shims ) ;
918968 const formatConfig = composeFormatConfig ( format ! ) ;
919969 const externalHelpersConfig = composeExternalHelpersConfig (
920970 externalHelpers ,
@@ -967,6 +1017,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
9671017
9681018 return mergeRsbuildConfig (
9691019 formatConfig ,
1020+ shimsConfig ,
9701021 externalHelpersConfig ,
9711022 // externalsWarnConfig should before other externals config
9721023 externalsWarnConfig ,
@@ -1046,6 +1097,7 @@ export async function composeCreateRsbuildConfig(
10461097 'banner' ,
10471098 'footer' ,
10481099 'dts' ,
1100+ 'shims' ,
10491101 ] ) ,
10501102 ) ,
10511103 } ;
0 commit comments