@@ -5,6 +5,7 @@ import { getModulesMetadata } from '../build-entries/get-dependencies.mjs';
55import config from './config.mjs' ;
66import { argv , path , fs } from 'zx' ;
77import { expandModules , modulesToStage } from '../build-entries/helpers.mjs' ;
8+ import { preparePureTypes } from './pure.mjs' ;
89
910const { copy, outputFile, pathExists, readdir, remove } = fs ;
1011
@@ -84,25 +85,6 @@ async function buildType(entry, options) {
8485 }
8586}
8687
87- async function getVersions ( ) {
88- const versions = [ ] ;
89- const entries = await readdir ( config . srcDir , { withFileTypes : true } ) ;
90- for ( const entry of entries ) {
91- if ( entry . isDirectory ( ) ) {
92- versions . push ( parseInt ( entry . name , 10 ) ) ;
93- }
94- }
95- return versions . sort ( ) ;
96- }
97-
98- async function buildTypesDirForTSVersion ( version ) {
99- const versions = await getVersions ( ) ;
100- for ( const v of versions ) {
101- if ( v > version ) break ;
102- await copy ( path . join ( config . srcDir , v . toString ( ) ) , path . join ( config . buildDir , version . toString ( ) , 'types' ) ) ;
103- }
104- }
105-
10688const ESModules = AllModules . filter ( it => it . startsWith ( 'es.' ) ) ;
10789const ESWithProposalsModules = AllModules . filter ( it => it . startsWith ( 'es' ) ) ;
10890const StableModules = AllModules . filter ( it => it . match ( / ^ (?: e s | w e b ) \. / ) ) ;
@@ -150,7 +132,7 @@ async function buildTypesForTSVersion(tsVersion) {
150132 tsVersion = tsVersion . toString ( ) . replace ( '.' , '' ) ;
151133 const bundlePath = path . join ( config . buildDir , tsVersion ) ;
152134 if ( await pathExists ( bundlePath ) ) await remove ( bundlePath ) ;
153- await buildTypesDirForTSVersion ( tsVersion ) ;
135+ await copy ( path . join ( config . srcDir , tsVersion ) , path . join ( config . buildDir , tsVersion , 'types' ) ) ;
154136 await fillCustomImportsForPure ( bundlePath , path . join ( bundlePath , 'types' ) ) ;
155137 await preparePureTypes ( bundlePath ) ;
156138
@@ -174,147 +156,6 @@ async function buildTypesForTSVersion(tsVersion) {
174156 await prependImports ( tsVersion ) ;
175157}
176158
177- function extractDeclareGlobalSections ( lines ) {
178- const sections = [ ] ;
179- const outside = [ ] ;
180- for ( let i = 0 ; i < lines . length ; ) {
181- if ( / ^ \s * d e c l a r e \s + g l o b a l \s * \{ / . test ( lines [ i ] ) ) {
182- let depth = 1 ;
183- const section = [ ] ;
184- for ( ++ i ; i < lines . length && depth > 0 ; ++ i ) {
185- depth += ( lines [ i ] . match ( / \{ / g) || [ ] ) . length ;
186- depth -= ( lines [ i ] . match ( / \} / g) || [ ] ) . length ;
187- if ( depth === 0 && / ^ \s * \} \s * $ / . test ( lines [ i ] ) ) break ;
188- if ( depth > 0 ) section . push ( lines [ i ] ) ;
189- }
190- ++ i ;
191- sections . push ( section ) ;
192- } else {
193- outside . push ( lines [ i ++ ] ) ;
194- }
195- }
196- return { sections, outside } ;
197- }
198-
199- function processLines ( lines , prefix ) {
200- const prefixed = [ ] ;
201- let noExport = false ;
202- return lines
203- . map ( line => {
204- const hasOptions = line . includes ( '@type-options' ) ;
205- const optionsStr = hasOptions ? line . match ( / @ t y p e - o p t i o n s \s + (?< options > [ \s \w , - ] + ) / ) ?. groups ?. options : '' ;
206- const options = {
207- noExtends : ! hasOptions ? false : optionsStr . includes ( 'no-extends' ) ,
208- noPrefix : ! hasOptions ? false : optionsStr . includes ( 'no-prefix' ) ,
209- noConstructor : ! hasOptions ? false : optionsStr . includes ( 'no-constructor' ) ,
210- exportBaseConstructor : ! hasOptions ? false : optionsStr . includes ( 'export-base-constructor' ) ,
211- noExport : ! hasOptions ? false : optionsStr . includes ( 'no-export' ) ,
212- noRedefine : ! hasOptions ? false : optionsStr . includes ( 'no-redefine' ) ,
213- } ;
214- if ( noExport && / ^ [ ^ { ] * \} / . test ( line ) ) {
215- noExport = false ;
216- return null ;
217- }
218- if ( noExport ) return null ;
219- if ( options . noExport ) {
220- if ( / ^ [ ^ { ] * $ / . test ( line ) || / \{ [ ^ } ] ? \} / . test ( line ) ) return null ;
221- noExport = true ;
222- return null ;
223- }
224- if ( / ^ \s * (?: d e c l a r e \s + ) ? i n t e r f a c e \s + \w + \s * e x t e n d s / . test ( line ) || options . noExtends && / ^ \s * (?: d e c l a r e \s + ) ? i n t e r f a c e \s + \w + \s * \{ / . test ( line ) ) {
225- if ( ! options . noPrefix ) {
226- const m = line . match ( / i n t e r f a c e \s + (?< name > \w + ) / ) ;
227- if ( m && m . groups ) {
228- prefixed . push ( m . groups . name ) ;
229- }
230- }
231- return line . replace ( / ^ (?< indent > \s * ) (?: d e c l a r e \s + ) ? i n t e r f a c e \s + (?< name > [ \s \w , < = > ] + ) / , `$<indent>export interface ${ ! options . noPrefix ? prefix : '' } $<name>` ) ;
232- }
233- if ( ! options . noExtends && / ^ \s * (?: d e c l a r e \s + ) ? i n t e r f a c e \s + \w + / . test ( line ) ) {
234- const m = line . match ( / ^ (?< indent > \s * ) (?: d e c l a r e \s + ) ? i n t e r f a c e \s + (?< name > \w + ) (?< extend > < [ ^ > ] + > ) ? / ) ;
235- const iIndent = m ?. groups ?. indent ?? '' ;
236- const iName = m ?. groups ?. name ?? '' ;
237- const iExtend = m ?. groups ?. extend ?? '' ;
238- if ( ! options . noPrefix && iName !== '' ) {
239- prefixed . push ( iName ) ;
240- }
241- const genericsForExtends = iExtend . replace ( / \s e x t e n d s \s [ ^ , > ] + / g, '' ) . replace ( / \s ? = \s ? \w + / g, '' ) ;
242- const entityName = `${ ! options . noPrefix ? prefix : '' } ${ iName } ` ;
243- const isConstructor = iName . includes ( 'Constructor' ) ;
244- let constructorDeclaration ;
245- if ( isConstructor ) {
246- constructorDeclaration = ! options . noRedefine ? `${ iIndent } var ${ entityName . replace ( 'Constructor' , '' ) } : ${ entityName } ;\n` : '' ;
247- } else {
248- constructorDeclaration = ! options . noRedefine ? `${ iIndent } var ${ entityName } : ${ options . exportBaseConstructor ? iName : entityName } ${ options . noConstructor ? '' : 'Constructor' } ;\n` : '' ;
249- }
250- return `${ constructorDeclaration } ${ iIndent } export interface ${ entityName } ${ iExtend } extends ${ iName } ${ genericsForExtends } {\n` ;
251- }
252- if ( / ^ \s * (?: d e c l a r e \s + ) ? f u n c t i o n / . test ( line ) ) {
253- return line . replace ( / ^ (?< indent > \s * ) (?: d e c l a r e \s + ) ? f u n c t i o n \s + (?< name > \w + ) / , `$<indent>export function ${ ! options . noPrefix ? prefix : '' } $<name>` ) ;
254- }
255- if ( / : \s * \w / . test ( line ) ) {
256- const sortedPrefixed = prefixed . sort ( ( a , b ) => b . length - a . length ) ;
257- sortedPrefixed . forEach ( item => {
258- const reg = new RegExp ( `: ${ item } ([,;<)])` , 'g' ) ;
259- line = line . replace ( reg , `: ${ prefix } ${ item } $1` ) ;
260- } ) ;
261- }
262- if ( / ^ \s * (?: d e c l a r e ) ? \s v a r / . test ( line ) ) {
263- const m = line . match ( / ^ (?< indent > \s * ) (?: d e c l a r e \s + ) ? v a r \s + (?< name > \w + ) : \s + (?< type > \w + ) / ) ;
264- return `${ m ?. groups ?. indent ?? '' } var ${ ! options . noPrefix ? prefix : '' } ${ m ?. groups ?. name ?? '' } : ${ m ?. groups ?. type } ;\n` ;
265- }
266- return line ;
267- } )
268- . filter ( line => line !== null ) ;
269- }
270-
271- function wrapDTSInNamespace ( content , namespace = 'CoreJS' ) {
272- const lines = content . split ( '\n' ) ;
273- const preamble = [ ] ;
274- let i = 0 ;
275- for ( ; i < lines . length ; i ++ ) {
276- const line = lines [ i ] ;
277- if ( / \/ \/ \/ \s * < r e f e r e n c e t y p e s / . test ( line ) ) {
278- const m = line . match ( / \/ \/ \/ \s * < r e f e r e n c e t y p e s = " (?< path > [ ^ " ] + ) " / ) ;
279- const typePath = m ?. groups ?. path ?? '' ;
280- preamble . push ( line . replace ( typePath , `../${ typePath } ` ) ) ;
281- continue ;
282- }
283- if ( / ^ \s * i m p o r t / . test ( line ) ) {
284- preamble . push ( line . replace ( / ^ \s * i m p o r t \s .* f r o m \s + [ " ' ] .+ [ " ' ] / , ( _ , a , b , c ) => `${ a } ../${ b } ${ c } ` ) ) ;
285- } else if ( / ^ \s * \/ \/ / . test ( line ) || / ^ \s * $ / . test ( line ) ) {
286- preamble . push ( line ) ;
287- } else break ;
288- }
289- const mainLines = lines . slice ( i ) ;
290- const { sections, outside } = extractDeclareGlobalSections ( mainLines ) ;
291- const nsBody = [ ...processLines ( outside , namespace ) , ...sections . flatMap ( s => processLines ( s , namespace ) ) ]
292- . reduce ( ( res , line ) => {
293- if ( ( line && line . trim ( ) !== '' ) || ( res . at ( - 1 ) && res . at ( - 1 ) . trim ( ) !== '' ) ) res . push ( line ) ;
294- return res ;
295- } , [ ] ) . map ( line => line ? ` ${ line } ` : '' ) . join ( '\n' ) ;
296- return `${ preamble . length ? `${ preamble . join ( '\n' ) } \n` : '' } declare namespace ${ namespace } {\n${ nsBody } }\n` ;
297- }
298-
299- async function preparePureTypes ( typesPath ) {
300- const entries = await readdir ( typesPath , { withFileTypes : true } ) ;
301- for ( const entry of entries ) {
302- if ( entry . name === 'pure' ) continue ;
303- if ( entry . isDirectory ( ) ) {
304- await preparePureTypes ( path . join ( typesPath , entry . name ) ) ;
305- } else {
306- if ( entry . name . includes ( 'core-js-types.d.ts' ) ) continue ;
307- const typePath = path . join ( typesPath , entry . name ) ;
308- const resultFilePath = typePath . replace ( entry . name , `pure/${ entry . name } ` ) ;
309- if ( await pathExists ( resultFilePath ) ) continue ;
310- const content = await fs . readFile ( typePath , 'utf8' ) ;
311- if ( content . includes ( 'declare namespace' ) ) continue ;
312- const result = wrapDTSInNamespace ( content ) ;
313- await outputFile ( resultFilePath , result ) ;
314- }
315- }
316- }
317-
318159if ( VERSION ) {
319160 await buildTypesForTSVersion ( VERSION ) ;
320161} else {
0 commit comments