@@ -15,13 +15,15 @@ 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 ] : {
@@ -30,15 +32,8 @@ interface TypeVersions {
3032}
3133
3234interface 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- } ;
35+ [ EXPORT_MAP_ENTRY_POINT ] : Partial < ConditionalExportEntryPoints > & {
36+ [ key : string ] : Partial < ConditionalExportEntryPoints > ;
4237 } ;
4338 [ TYPES_VERSIONS_ENTRY_POINT ] : TypeVersions ;
4439}
@@ -77,6 +72,13 @@ ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint =>
7772
7873if ( newPkgJson [ EXPORT_MAP_ENTRY_POINT ] ) {
7974 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
8082 newPkgJson [ EXPORT_MAP_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce (
8183 ( acc , [ key , val ] ) => {
8284 return { ...acc , [ key ] : val . replace ( `${ buildDir } /` , '' ) } ;
@@ -90,7 +92,10 @@ if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) {
9092 Object . entries ( newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] ) . forEach ( ( [ key , val ] ) => {
9193 newPkgJson [ TYPES_VERSIONS_ENTRY_POINT ] [ key ] = Object . entries ( val ) . reduce ( ( acc , [ key , val ] ) => {
9294 const newKey = key . replace ( `${ buildDir } /` , '' ) ;
93- return { ...acc , [ newKey ] : val . map ( v => v . replace ( `${ buildDir } /` , '' ) ) } ;
95+ return {
96+ ...acc ,
97+ [ newKey ] : val . map ( v => v . replace ( `${ buildDir } /` , '' ) ) ,
98+ } ;
9499 } , { } ) ;
95100 } ) ;
96101}
0 commit comments