@@ -31,10 +31,12 @@ interface TypeVersions {
3131 } ;
3232}
3333
34+ type PackageJsonExports = Partial < ConditionalExportEntryPoints > & {
35+ [ key : string ] : Partial < ConditionalExportEntryPoints > ;
36+ } ;
37+
3438interface PackageJson extends Record < string , unknown > , PackageJsonEntryPoints {
35- [ EXPORT_MAP_ENTRY_POINT ] : Partial < ConditionalExportEntryPoints > & {
36- [ key : string ] : Partial < ConditionalExportEntryPoints > ;
37- } ;
39+ [ EXPORT_MAP_ENTRY_POINT ] : PackageJsonExports ;
3840 [ TYPES_VERSIONS_ENTRY_POINT ] : TypeVersions ;
3941}
4042
@@ -70,21 +72,26 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
7072 newPkgJson [ entryPoint ] = newPkgJson [ entryPoint ] . replace ( `${ buildDir } /` , '' ) ;
7173} ) ;
7274
75+ /**
76+ * Recursively traverses the exports object and rewrites all string values to remove the build directory.
77+ */
78+ function rewriteConditionalExportEntryPoint (
79+ exportsObject : Record < string , string | Record < string , string > > ,
80+ key : string ,
81+ ) : void {
82+ const exportsField = exportsObject [ key ] ;
83+ if ( typeof exportsField === 'string' ) {
84+ exportsObject [ key ] = exportsField . replace ( `${ buildDir } /` , '' ) ;
85+ return ;
86+ }
87+ Object . keys ( exportsField ) . forEach ( subfieldKey => {
88+ rewriteConditionalExportEntryPoint ( exportsField , subfieldKey ) ;
89+ } ) ;
90+ }
91+
7392if ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) {
74- Object . entries ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) . forEach ( ( [ key , val ] ) => {
75- if ( typeof val === 'string' ) {
76- // case 1: key is already a conditional export entry point
77- // @ts -expect-error I'm too dumb for TS :'D
78- newPkgJson [ EXPORT_MAP_ENTRY_POINT ] [ key ] = val . replace ( `${ buildDir } /` , '' ) ;
79- return ;
80- }
81- // case 2: key is a sub-path export
82- newPkgJson [ EXPORT_MAP_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce (
83- ( acc , [ key , val ] ) => {
84- return { ...acc , [ key ] : val . replace ( `${ buildDir } /` , '' ) } ;
85- } ,
86- { } as typeof val ,
87- ) ;
93+ Object . keys ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) . forEach ( key => {
94+ rewriteConditionalExportEntryPoint ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] , key ) ;
8895 } ) ;
8996}
9097
0 commit comments