@@ -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' ;
@@ -451,12 +455,16 @@ const composeFormatConfig = (
451455 pkgJson : PkgJson ,
452456) : RsbuildConfig => {
453457 const jsParserOptions = {
454- importMeta : false ,
455- requireResolve : false ,
456- requireDynamic : false ,
457- requireAsExpression : false ,
458- importDynamic : false ,
459- } ;
458+ cjs : {
459+ requireResolve : false ,
460+ requireDynamic : false ,
461+ requireAsExpression : false ,
462+ } ,
463+ esm : {
464+ importMeta : false ,
465+ importDynamic : false ,
466+ } ,
467+ } as const ;
460468
461469 switch ( format ) {
462470 case 'esm' :
@@ -465,7 +473,10 @@ const composeFormatConfig = (
465473 rspack : {
466474 module : {
467475 parser : {
468- javascript : jsParserOptions ,
476+ javascript : {
477+ ...jsParserOptions . esm ,
478+ ...jsParserOptions . cjs ,
479+ } ,
469480 } ,
470481 } ,
471482 optimization : {
@@ -490,12 +501,11 @@ const composeFormatConfig = (
490501 } ;
491502 case 'cjs' :
492503 return {
493- plugins : [ pluginCjsShim ( ) ] ,
494504 tools : {
495505 rspack : {
496506 module : {
497507 parser : {
498- javascript : jsParserOptions ,
508+ javascript : { ... jsParserOptions . esm , ... jsParserOptions . cjs } ,
499509 } ,
500510 } ,
501511 output : {
@@ -545,6 +555,47 @@ const composeFormatConfig = (
545555 }
546556} ;
547557
558+ const composeShimsConfig = ( format : Format , shims ?: Shims ) : RsbuildConfig => {
559+ const resolvedShims = {
560+ cjs : {
561+ 'import.meta.url' : shims ?. cjs ?. [ 'import.meta.url' ] ?? true ,
562+ } ,
563+ esm : {
564+ __filename : shims ?. esm ?. __filename ?? false ,
565+ __dirname : shims ?. esm ?. __dirname ?? false ,
566+ require : shims ?. esm ?. require ?? false ,
567+ } ,
568+ } ;
569+
570+ switch ( format ) {
571+ case 'esm' :
572+ return {
573+ tools : {
574+ rspack : {
575+ node : {
576+ // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`
577+ __dirname : resolvedShims . esm . __dirname ? 'node-module' : false ,
578+ __filename : resolvedShims . esm . __filename ? 'node-module' : false ,
579+ } ,
580+ } ,
581+ } ,
582+ plugins : [ resolvedShims . esm . require && pluginEsmRequireShim ( ) ] . filter (
583+ Boolean ,
584+ ) ,
585+ } ;
586+ case 'cjs' :
587+ return {
588+ plugins : [
589+ resolvedShims . cjs [ 'import.meta.url' ] && pluginCjsImportMetaUrlShim ( ) ,
590+ ] . filter ( Boolean ) ,
591+ } ;
592+ case 'umd' :
593+ return { } ;
594+ default :
595+ throw new Error ( `Unsupported format: ${ format } ` ) ;
596+ }
597+ } ;
598+
548599export const composeModuleImportWarn = ( request : string ) : string => {
549600 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"' ) } .` ;
550601} ;
@@ -854,9 +905,6 @@ const composeTargetConfig = (
854905 tools : {
855906 rspack : {
856907 target : [ 'node' ] ,
857- // "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
858- // and leave them as-is in the rest of the cases. Leave the comments here to explain the behavior.
859- // { node: { __dirname: ..., __filename: ... } }
860908 } ,
861909 } ,
862910 output : {
@@ -931,13 +979,15 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
931979
932980 const {
933981 format,
982+ shims,
934983 banner = { } ,
935984 footer = { } ,
936985 autoExtension = true ,
937986 autoExternal = true ,
938987 externalHelpers = false ,
939988 redirect = { } ,
940989 } = config ;
990+ const shimsConfig = composeShimsConfig ( format ! , shims ) ;
941991 const formatConfig = composeFormatConfig ( format ! , pkgJson ! ) ;
942992 const externalHelpersConfig = composeExternalHelpersConfig (
943993 externalHelpers ,
@@ -990,6 +1040,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
9901040
9911041 return mergeRsbuildConfig (
9921042 formatConfig ,
1043+ shimsConfig ,
9931044 externalHelpersConfig ,
9941045 // externalsWarnConfig should before other externals config
9951046 externalsWarnConfig ,
@@ -1069,6 +1120,7 @@ export async function composeCreateRsbuildConfig(
10691120 'banner' ,
10701121 'footer' ,
10711122 'dts' ,
1123+ 'shims' ,
10721124 ] ) ,
10731125 ) ,
10741126 } ;
0 commit comments