@@ -25,6 +25,7 @@ const imports = {
2525 actual : new Set ( ) ,
2626 full : new Set ( ) ,
2727 pure : new Set ( ) ,
28+ index : new Set ( ) ,
2829} ;
2930
3031async function buildType ( entry , options ) {
@@ -68,22 +69,24 @@ async function buildType(entry, options) {
6869 if ( filter ) modules = modules . filter ( it => filter . has ( it ) ) ;
6970
7071 types . forEach ( type => {
72+ imports . index . add ( type ) ;
7173 imports [ subset ] . add ( type ) ;
7274 imports . pure . add ( path . join ( 'pure' , type ) ) ;
7375 } ) ;
7476 if ( customType ) {
77+ imports . index . add ( customType ) ;
7578 imports [ subset ] . add ( customType ) ;
7679 imports . pure . add ( customType ) ;
7780 }
7881
79- const filePath = buildFilePath ( tsVersion , subset ) ;
80- const filePathPure = buildFilePath ( tsVersion , 'pure' ) ;
82+ const indexPath = buildFilePath ( tsVersion , 'index' ) ;
83+ const purePath = buildFilePath ( tsVersion , 'pure' ) ;
8184
8285 const tplPure = template ( { ...options , modules, rawModules, level, entry, types, packageName : PACKAGE_NAME_PURE , prefix : TYPE_PREFIX } ) ;
8386 const tpl = template ( { ...options , modules, rawModules, level, entry, types, packageName : PACKAGE_NAME } ) ;
8487
85- await outputFile ( filePathPure , `${ tplPure . dts } ${ tplPure . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
86- await outputFile ( filePath , `${ tpl . dts } ${ tpl . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
88+ await outputFile ( indexPath , `${ tpl . dts } ${ tpl . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
89+ await outputFile ( purePath , `${ tplPure . dts } ${ tplPure . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
8790
8891 if ( ! entry . endsWith ( '/' ) ) {
8992 const entryWithExt = `${ entry } .js` ;
@@ -92,19 +95,19 @@ async function buildType(entry, options) {
9295 const tplWithExt = template ( { ...options , modules, rawModules, level, types, entry : entryWithExt ,
9396 packageName : PACKAGE_NAME } ) ;
9497
95- await outputFile ( filePathPure , `${ tplPureWithExt . dts } ${ tplPureWithExt . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
96- await outputFile ( filePath , `${ tplWithExt . dts } ${ tplWithExt . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
98+ await outputFile ( indexPath , `${ tplWithExt . dts } ${ tplWithExt . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
99+ await outputFile ( purePath , `${ tplPureWithExt . dts } ${ tplPureWithExt . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
97100 }
98101
99102 if ( entry . endsWith ( '/index' ) ) {
100- const entryWithoutIndex = entry . replace ( / i n d e x $ / , '' ) ;
103+ const entryWithoutIndex = entry . replace ( / \/ i n d e x $ / , '' ) ;
101104 const tplPureWithoutIndex = template ( { ...options , modules, rawModules, level, entry : entryWithoutIndex , types,
102105 packageName : PACKAGE_NAME_PURE , prefix : TYPE_PREFIX } ) ;
103106 const tplWithoutIndex = template ( { ...options , modules, rawModules, level, types, entry : entryWithoutIndex ,
104107 packageName : PACKAGE_NAME } ) ;
105108
106- await outputFile ( filePathPure , `${ tplPureWithoutIndex . dts } ${ tplPureWithoutIndex . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
107- await outputFile ( filePath , `${ tplWithoutIndex . dts } ${ tplWithoutIndex . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
109+ await outputFile ( indexPath , `${ tplWithoutIndex . dts } ${ tplWithoutIndex . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
110+ await outputFile ( purePath , `${ tplPureWithoutIndex . dts } ${ tplPureWithoutIndex . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
108111 }
109112
110113 if ( proposal ) {
@@ -136,8 +139,11 @@ async function prependImports(version) {
136139 for ( const subset of Object . keys ( imports ) ) {
137140 const filePath = buildFilePath ( version , subset ) ;
138141 const importLines = buildImports ( imports [ subset ] ) ;
139- const originalContent = await fs . readFile ( filePath , 'utf8' ) ;
140- await outputFile ( filePath , `${ importLines } \n\n${ originalContent } ` ) ;
142+ let originalContent = '' ;
143+ if ( await pathExists ( filePath ) ) {
144+ originalContent = await fs . readFile ( filePath , 'utf8' ) ;
145+ }
146+ await outputFile ( filePath , `${ importLines } \n\n${ originalContent } ` , { flag : 'w' } ) ;
141147 }
142148}
143149
@@ -200,17 +206,14 @@ async function buildPackageJson(breakpoints, namespaces) {
200206 } ) ;
201207 packageJson . exports = { } ;
202208 Object . entries ( namespaces ) . forEach ( ( [ namespace , options ] ) => {
203- const namespaceKey = namespace ? `./${ namespace } ${ options . isDir ? '/*' : '' } ` : '.' ;
209+ const namespaceKey = namespace !== 'index' ? `./${ namespace } ${ options . isDir ? '/*' : '' } ` : '.' ;
204210 packageJson . exports [ namespaceKey ] = { } ;
205211 breakpoints . forEach ( ( breakpoint , index ) => {
206212 const isLast = index === breakpoints . length - 1 ;
207213 const breakpointString = `ts${ breakpoint . toString ( ) . replace ( '.' , '-' ) } ` ;
208214 packageJson . exports [ namespaceKey ] [ `types${ isLast ? '' : `@>=${ breakpoint } ` } ` ] = `./dist/${ breakpointString } /${ namespace } ${ options . isDir ? '/*' : '' } .d.ts` ;
209215 } ) ;
210216 packageJson . exports [ namespaceKey ] . default = `./dist/ts${ defaultBreakpoint . toString ( ) . replace ( '.' , '-' ) } /${ namespace } ${ options . isDir ? '/*' : '' } .d.ts` ;
211- if ( options . default ) {
212- packageJson . exports [ '.' ] = packageJson . exports [ namespaceKey ] ;
213- }
214217 } ) ;
215218 const exportsKeys = Object . keys ( packageJson . exports ) . sort ( ) ;
216219 const exports = { } ;
@@ -228,12 +231,13 @@ if (VERSION) {
228231 await remove ( BUILD_DIR ) ;
229232 tsVersionBreakpoints . forEach ( async version => await buildTypesForTSVersion ( version ) ) ;
230233 const namespaces = {
231- es : { isDir : false , default : false } ,
232- stable : { isDir : false , default : false } ,
233- actual : { isDir : false , default : true } ,
234- full : { isDir : false , default : false } ,
235- pure : { isDir : false , default : false } ,
236- proposals : { isDir : true , default : false } ,
234+ es : { isDir : false } ,
235+ stable : { isDir : false } ,
236+ actual : { isDir : false } ,
237+ full : { isDir : false } ,
238+ pure : { isDir : false } ,
239+ proposals : { isDir : true } ,
240+ index : { isDir : false } ,
237241 } ;
238242 await buildPackageJson ( tsVersionBreakpoints , namespaces ) ;
239243}
0 commit comments