@@ -31,10 +31,12 @@ interface TypeVersions {
31
31
} ;
32
32
}
33
33
34
+ type PackageJsonExports = Partial < ConditionalExportEntryPoints > & {
35
+ [ key : string ] : Partial < ConditionalExportEntryPoints > ;
36
+ } ;
37
+
34
38
interface 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 ;
38
40
[ TYPES_VERSIONS_ENTRY_POINT ] : TypeVersions ;
39
41
}
40
42
@@ -70,21 +72,26 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
70
72
newPkgJson [ entryPoint ] = newPkgJson [ entryPoint ] . replace ( `${ buildDir } /` , '' ) ;
71
73
} ) ;
72
74
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
+
73
92
if ( 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 ) ;
88
95
} ) ;
89
96
}
90
97
0 commit comments