@@ -15,31 +15,28 @@ const NPM_IGNORE = fs.existsSync('.npmignore') ? '.npmignore' : '../../.npmignor
1515
1616const ASSETS = [ 'README.md' , 'LICENSE' , 'package.json' , NPM_IGNORE ] as const ;
1717const ENTRY_POINTS = [ 'main' , 'module' , 'types' , 'browser' ] as const ;
18+ const CONDITIONAL_EXPORT_ENTRY_POINTS = [ 'import' , 'require' , ...ENTRY_POINTS ] as const ;
1819const EXPORT_MAP_ENTRY_POINT = 'exports' ;
1920const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions' ;
2021
2122const packageWithBundles = process . argv . includes ( '--bundles' ) ;
2223const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR ;
2324
2425type PackageJsonEntryPoints = Record < ( typeof ENTRY_POINTS ) [ number ] , string > ;
26+ type ConditionalExportEntryPoints = Record < ( typeof CONDITIONAL_EXPORT_ENTRY_POINTS ) [ number ] , string > ;
2527
2628interface TypeVersions {
2729 [ key : string ] : {
2830 [ key : string ] : string [ ] ;
2931 } ;
3032}
3133
34+ type PackageJsonExports = Partial < ConditionalExportEntryPoints > & {
35+ [ key : string ] : Partial < ConditionalExportEntryPoints > ;
36+ } ;
37+
3238interface 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 ;
4340 [ TYPES_VERSIONS_ENTRY_POINT ] : TypeVersions ;
4441}
4542
@@ -75,22 +72,37 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
7572 newPkgJson [ entryPoint ] = newPkgJson [ entryPoint ] . replace ( `${ buildDir } /` , '' ) ;
7673} ) ;
7774
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+
7892if ( 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 ) ;
8695 } ) ;
8796}
8897
8998if ( newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] ) {
9099 Object . entries ( newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] ) . forEach ( ( [ key , val ] ) => {
91100 newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce ( ( acc , [ key , val ] ) => {
92101 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+ } ;
94106 } , { } ) ;
95107 } ) ;
96108}
0 commit comments