@@ -15,31 +15,28 @@ const NPM_IGNORE = fs.existsSync('.npmignore') ? '.npmignore' : '../../.npmignor
15
15
16
16
const ASSETS = [ 'README.md' , 'LICENSE' , 'package.json' , NPM_IGNORE ] as const ;
17
17
const ENTRY_POINTS = [ 'main' , 'module' , 'types' , 'browser' ] as const ;
18
+ const CONDITIONAL_EXPORT_ENTRY_POINTS = [ 'import' , 'require' , ...ENTRY_POINTS ] as const ;
18
19
const EXPORT_MAP_ENTRY_POINT = 'exports' ;
19
20
const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions' ;
20
21
21
22
const packageWithBundles = process . argv . includes ( '--bundles' ) ;
22
23
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR ;
23
24
24
25
type PackageJsonEntryPoints = Record < ( typeof ENTRY_POINTS ) [ number ] , string > ;
26
+ type ConditionalExportEntryPoints = Record < ( typeof CONDITIONAL_EXPORT_ENTRY_POINTS ) [ number ] , string > ;
25
27
26
28
interface TypeVersions {
27
29
[ key : string ] : {
28
30
[ key : string ] : string [ ] ;
29
31
} ;
30
32
}
31
33
34
+ type PackageJsonExports = Partial < ConditionalExportEntryPoints > & {
35
+ [ key : string ] : Partial < ConditionalExportEntryPoints > ;
36
+ } ;
37
+
32
38
interface PackageJson extends Record < string , unknown > , PackageJsonEntryPoints {
33
- [ EXPORT_MAP_ENTRY_POINT ] : {
34
- [ key : string ] : {
35
- import : string ;
36
- require : string ;
37
- types : string ;
38
- node : string ;
39
- browser : string ;
40
- default : string ;
41
- } ;
42
- } ;
39
+ [ EXPORT_MAP_ENTRY_POINT ] : PackageJsonExports ;
43
40
[ TYPES_VERSIONS_ENTRY_POINT ] : TypeVersions ;
44
41
}
45
42
@@ -75,22 +72,37 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
75
72
newPkgJson [ entryPoint ] = newPkgJson [ entryPoint ] . replace ( `${ buildDir } /` , '' ) ;
76
73
} ) ;
77
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
+
78
92
if ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) {
79
- Object . entries ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) . forEach ( ( [ key , val ] ) => {
80
- newPkgJson [ EXPORT_MAP_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce (
81
- ( acc , [ key , val ] ) => {
82
- return { ...acc , [ key ] : val . replace ( `${ buildDir } /` , '' ) } ;
83
- } ,
84
- { } as typeof val ,
85
- ) ;
93
+ Object . keys ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) . forEach ( key => {
94
+ rewriteConditionalExportEntryPoint ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] , key ) ;
86
95
} ) ;
87
96
}
88
97
89
98
if ( newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] ) {
90
99
Object . entries ( newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] ) . forEach ( ( [ key , val ] ) => {
91
100
newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce ( ( acc , [ key , val ] ) => {
92
101
const newKey = key . replace ( `${ buildDir } /` , '' ) ;
93
- return { ...acc , [ newKey ] : val . map ( v => v . replace ( `${ buildDir } /` , '' ) ) } ;
102
+ return {
103
+ ...acc ,
104
+ [ newKey ] : val . map ( v => v . replace ( `${ buildDir } /` , '' ) ) ,
105
+ } ;
94
106
} , { } ) ;
95
107
} ) ;
96
108
}
0 commit comments