@@ -10,12 +10,20 @@ const { copy, outputFile, pathExists, readdir, remove } = fs;
1010
1111const versionArg = argv . _ . find ( item => item . startsWith ( 'version=' ) ) ;
1212const VERSION = versionArg ? versionArg . slice ( 'version=' . length ) : undefined ;
13-
14- async function buildType ( typeFilePath , entry , options ) {
13+ const imports = {
14+ es : new Set ( ) ,
15+ stable : new Set ( ) ,
16+ actual : new Set ( ) ,
17+ full : new Set ( ) ,
18+ pure : new Set ( ) ,
19+ } ;
20+
21+ async function buildType ( entry , options ) {
1522 let {
1623 entryFromNamespace,
1724 subset = entryFromNamespace ?? 'full' ,
1825 template, templateStable, templateActual, templateFull, filter, modules, enforceEntryCreation,
26+ customType, tsVersion,
1927 } = options ;
2028
2129 switch ( subset ) {
@@ -50,9 +58,23 @@ async function buildType(typeFilePath, entry, options) {
5058
5159 if ( filter ) modules = modules . filter ( it => filter . has ( it ) ) ;
5260
61+ const tplPure = template ( { ...options , modules, rawModules, level, entry, types, packageName : `${ config . packageName } pure/` } ) ;
5362 const tpl = template ( { ...options , modules, rawModules, level, entry, types, packageName : config . packageName } ) ;
5463
55- await outputFile ( typeFilePath , `${ tpl . dts } ${ tpl . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
64+ types . forEach ( type => {
65+ imports [ subset ] . add ( type ) ;
66+ imports . pure . add ( type ) ;
67+ } ) ;
68+ if ( customType ) {
69+ imports [ subset ] . add ( customType ) ;
70+ imports . pure . add ( customType ) ;
71+ }
72+
73+ const filePath = buildFilePath ( tsVersion , subset ) ;
74+ const filePathPure = buildFilePath ( tsVersion , 'pure' ) ;
75+
76+ await outputFile ( filePath , `${ tpl . dts } ${ tpl . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
77+ await outputFile ( filePathPure , `${ tplPure . dts } ${ tplPure . dts ? '\n\n' : '' } ` , { flag : 'a' } ) ;
5678}
5779
5880async function getVersions ( ) {
@@ -70,19 +92,7 @@ async function buildTypesDirForTSVersion(version) {
7092 const versions = await getVersions ( ) ;
7193 for ( const v of versions ) {
7294 if ( v > version ) break ;
73- await copy ( path . join ( config . srcDir , v . toString ( ) ) , path . join ( config . buildDir , version . toString ( ) ) ) ;
74- }
75- }
76-
77- async function addImports ( filePath , fromPath ) {
78- const entries = await readdir ( fromPath , { withFileTypes : true } ) ;
79- for ( const entry of entries ) {
80- if ( entry . isDirectory ( ) ) {
81- await addImports ( filePath , path . join ( fromPath , entry . name ) ) ;
82- } else {
83- const typePath = path . join ( fromPath , entry . name ) . replace ( config . buildDir , '' ) ;
84- await outputFile ( filePath , `/// <reference path="./${ typePath } " />\n` , { flag : 'a' } ) ;
85- }
95+ await copy ( path . join ( config . srcDir , v . toString ( ) ) , path . join ( config . buildDir , version . toString ( ) , 'types' ) ) ;
8696 }
8797}
8898
@@ -96,38 +106,43 @@ const ESWithProposalsSet = new Set(ESWithProposalsModules);
96106const StableSet = new Set ( StableModules ) ;
97107const ActualSet = new Set ( ActualModules ) ;
98108
109+ function buildFilePath ( tsVersion , subset ) {
110+ return path . join ( config . buildDir , tsVersion . toString ( ) , `${ subset } .d.ts` ) ;
111+ }
112+
113+ async function prependImports ( version ) {
114+ for ( const subset of Object . keys ( imports ) ) {
115+ const filePath = buildFilePath ( version , subset ) ;
116+ const importLines = Array . from ( imports [ subset ] , it => `/// <reference types="./types/${ it } .d.ts" />` ) . join ( '\n' ) ;
117+ const originalContent = await fs . readFile ( filePath , 'utf8' ) ;
118+ await outputFile ( filePath , `${ importLines } \n\n${ originalContent } ` ) ;
119+ }
120+ }
121+
99122async function buildTypesForTSVersion ( tsVersion ) {
100123 tsVersion = tsVersion . toString ( ) . replace ( '.' , '' ) ;
101- const bundleName = `${ config . bundleName } .${ tsVersion === config . latestTsVersion ? 'latest' : tsVersion } .d.ts` ;
102- const bundlePath = path . join ( config . buildDir , bundleName ) ;
103- const typesPath = path . join ( config . buildDir , tsVersion . toString ( ) ) ;
124+ const bundlePath = path . join ( config . buildDir , tsVersion ) ;
104125 if ( await pathExists ( bundlePath ) ) await remove ( bundlePath ) ;
105- if ( await pathExists ( typesPath ) ) await remove ( typesPath ) ;
106126 await buildTypesDirForTSVersion ( tsVersion ) ;
107- await addImports ( bundlePath , typesPath ) ;
108127
109128 for ( const [ entry , definition ] of Object . entries ( features ) ) {
110- await buildType ( bundlePath , `es/${ entry } ` , { ...definition , entryFromNamespace : 'es' } ) ;
111- await buildType ( bundlePath , `stable/${ entry } ` , { ...definition , entryFromNamespace : 'stable' } ) ;
112- await buildType ( bundlePath , `actual/${ entry } ` , { ...definition , entryFromNamespace : 'actual' } ) ;
113- await buildType ( bundlePath , `full/${ entry } ` , { ...definition , entryFromNamespace : 'full' } ) ;
129+ await buildType ( `es/${ entry } ` , { ...definition , entryFromNamespace : 'es' , tsVersion } ) ;
130+ await buildType ( `stable/${ entry } ` , { ...definition , entryFromNamespace : 'stable' , tsVersion } ) ;
131+ await buildType ( `actual/${ entry } ` , { ...definition , entryFromNamespace : 'actual' , tsVersion } ) ;
132+ await buildType ( `full/${ entry } ` , { ...definition , entryFromNamespace : 'full' , tsVersion } ) ;
114133 }
115134
116135 for ( const [ name , definition ] of Object . entries ( proposals ) ) {
117- await buildType ( bundlePath , `proposals/${ name } ` , { ...definition , template : $proposal } ) ;
136+ await buildType ( `proposals/${ name } ` , { ...definition , template : $proposal , tsVersion } ) ;
118137 }
119138
120- await buildType ( bundlePath , 'stage/3' , { template : $path , modules : ActualModules , subset : 'es-stage' } ) ;
121- await buildType ( bundlePath , 'stage/2.7' , { template : $path , modules : modulesToStage ( StableModules , 2.7 ) , subset : 'es-stage' } ) ;
122- await buildType ( bundlePath , 'stage/2' , { template : $path , modules : modulesToStage ( StableModules , 2 ) , subset : 'es-stage' } ) ;
123- await buildType ( bundlePath , 'stage/1' , { template : $path , modules : modulesToStage ( StableModules , 1 ) , subset : 'es-stage' } ) ;
124- await buildType ( bundlePath , 'stage/0' , { template : $path , modules : AllModules , subset : 'es-stage' } ) ;
125-
126- await buildType ( bundlePath , 'es/index' , { template : $path , modules : ESModules , subset : 'es' } ) ;
127- await buildType ( bundlePath , 'stable/index' , { template : $path , modules : StableModules , subset : 'stable' } ) ;
128- await buildType ( bundlePath , 'actual/index' , { template : $path , modules : ActualModules } ) ;
129- await buildType ( bundlePath , 'full/index' , { template : $path , modules : AllModules } ) ;
130- await buildType ( bundlePath , 'index' , { template : $path , modules : ActualModules } ) ;
139+ await buildType ( 'es/index' , { template : $path , modules : ESModules , subset : 'es' , tsVersion } ) ;
140+ await buildType ( 'stable/index' , { template : $path , modules : StableModules , subset : 'stable' , tsVersion } ) ;
141+ await buildType ( 'actual/index' , { template : $path , modules : ActualModules , tsVersion } ) ;
142+ await buildType ( 'full/index' , { template : $path , modules : AllModules , tsVersion } ) ;
143+ await buildType ( 'index' , { template : $path , modules : ActualModules , tsVersion } ) ;
144+
145+ await prependImports ( tsVersion ) ;
131146}
132147
133148if ( VERSION ) {
0 commit comments