@@ -47,6 +47,24 @@ const getThreadsCount = () => {
47
47
return 3 ; // Default (like the workerpool do)
48
48
} ;
49
49
50
+ const splitFilesToChunks = ( files : FileContent [ ] , chunksCount : number ) : FileContent [ ] [ ] => {
51
+ let chunks : FileContent [ ] [ ] ;
52
+ if ( files . length < chunksCount * chunksCount ) {
53
+ chunks = [ files ] ;
54
+ } else {
55
+ const baseSize = Math . floor ( files . length / chunksCount ) ;
56
+ chunks = [ ] ;
57
+ for ( let i = 0 ; i < chunksCount ; i ++ ) {
58
+ // For the last part, we take the remaining files so we don't lose the extra ones.
59
+ const start = i * baseSize ;
60
+ const end = ( i === chunksCount - 1 ) ? files . length : start + baseSize ;
61
+ chunks . push ( files . slice ( start , end ) ) ;
62
+ }
63
+ }
64
+
65
+ return chunks ;
66
+ } ;
67
+
50
68
export type DataSchemaCompilerOptions = {
51
69
compilerCache : CompilerCache ;
52
70
omitErrors ?: boolean ;
@@ -288,8 +306,27 @@ export class DataSchemaCompiler {
288
306
}
289
307
290
308
if ( transpilationNative ) {
291
- const jsFiles = toCompile . filter ( file => file . fileName . endsWith ( '.js' ) || file . convertedToJs ) ;
292
- let JsFilesTasks = [ ] ;
309
+ const jsFiles : FileContent [ ] = [ ] ;
310
+ const yamlFiles : FileContent [ ] = [ ] ;
311
+ const jinjaFiles : FileContent [ ] = [ ] ;
312
+
313
+ toCompile . forEach ( ( file ) => {
314
+ if ( file . fileName . endsWith ( '.js' ) || file . convertedToJs ) {
315
+ jsFiles . push ( file ) ;
316
+ } else if ( file . fileName . endsWith ( '.jinja' ) ||
317
+ ( file . fileName . endsWith ( '.yml' ) || file . fileName . endsWith ( '.yaml' ) )
318
+ && file . content . match ( JINJA_SYNTAX )
319
+ ) {
320
+ jinjaFiles . push ( file ) ;
321
+ } else if ( file . fileName . endsWith ( '.yml' ) || file . fileName . endsWith ( '.yaml' ) ) {
322
+ yamlFiles . push ( file ) ;
323
+ }
324
+ } ) ;
325
+
326
+ let jsFilesTasks : Promise < ( FileContent | undefined ) [ ] > [ ] = [ ] ;
327
+ let yamlFilesTasks : Promise < ( FileContent | undefined ) [ ] > [ ] = [ ] ;
328
+ // XXX: For now we don't have bulk transpilation for jinja files
329
+ // let jinjaFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
293
330
294
331
if ( jsFiles . length > 0 ) {
295
332
// Warming up swc compiler cache
@@ -300,26 +337,19 @@ export class DataSchemaCompiler {
300
337
301
338
await this . transpileJsFile ( dummyFile , errorsReport , { cubeNames, cubeSymbols, transpilerNames, contextSymbols : CONTEXT_SYMBOLS , compilerId, stage } ) ;
302
339
303
- let jsChunks ;
304
- if ( jsFiles . length < transpilationNativeThreadsCount * transpilationNativeThreadsCount ) {
305
- jsChunks = [ jsFiles ] ;
306
- } else {
307
- const baseSize = Math . floor ( jsFiles . length / transpilationNativeThreadsCount ) ;
308
- jsChunks = [ ] ;
309
- for ( let i = 0 ; i < transpilationNativeThreadsCount ; i ++ ) {
310
- // For the last part, we take the remaining files so we don't lose the extra ones.
311
- const start = i * baseSize ;
312
- const end = ( i === transpilationNativeThreadsCount - 1 ) ? jsFiles . length : start + baseSize ;
313
- jsChunks . push ( jsFiles . slice ( start , end ) ) ;
314
- }
315
- }
316
- JsFilesTasks = jsChunks . map ( chunk => this . transpileJsFilesBulk ( chunk , errorsReport , { transpilerNames, compilerId } ) ) ;
340
+ const jsChunks = splitFilesToChunks ( jsFiles , transpilationNativeThreadsCount ) ;
341
+ jsFilesTasks = jsChunks . map ( chunk => this . transpileJsFilesNativeBulk ( chunk , errorsReport , { transpilerNames, compilerId } ) ) ;
317
342
}
318
343
319
- const nonJsFilesTasks = toCompile . filter ( file => ! file . fileName . endsWith ( '.js' ) && ! file . convertedToJs )
320
- . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames, compilerId } ) ) ;
344
+ if ( yamlFiles . length > 0 ) {
345
+ const yamlChunks = splitFilesToChunks ( yamlFiles , transpilationNativeThreadsCount ) ;
346
+ yamlFilesTasks = yamlChunks . map ( chunk => this . transpileYamlFilesNativeBulk ( chunk , errorsReport , { transpilerNames, compilerId } ) ) ;
347
+ }
348
+
349
+ const jinjaFilesTasks = jinjaFiles
350
+ . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames } ) ) ;
321
351
322
- results = ( await Promise . all ( [ ...nonJsFilesTasks , ...JsFilesTasks ] ) ) . flat ( ) ;
352
+ results = ( await Promise . all ( [ ...jsFilesTasks , ...yamlFilesTasks , ... jinjaFilesTasks ] ) ) . flat ( ) ;
323
353
} else if ( transpilationWorkerThreads ) {
324
354
results = await Promise . all ( toCompile . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames } ) ) ) ;
325
355
} else {
@@ -521,11 +551,7 @@ export class DataSchemaCompiler {
521
551
}
522
552
}
523
553
524
- /**
525
- * Right now it is used only for transpilation in native,
526
- * so no checks for transpilation type inside this method
527
- */
528
- private async transpileJsFilesBulk (
554
+ private async transpileJsFilesNativeBulk (
529
555
files : FileContent [ ] ,
530
556
errorsReport : ErrorReporter ,
531
557
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions
@@ -563,6 +589,36 @@ export class DataSchemaCompiler {
563
589
} ) ;
564
590
}
565
591
592
+ private async transpileYamlFilesNativeBulk (
593
+ files : FileContent [ ] ,
594
+ errorsReport : ErrorReporter ,
595
+ { compilerId } : TranspileOptions
596
+ ) : Promise < ( FileContent | undefined ) [ ] > {
597
+ const reqDataArr = files . map ( file => ( {
598
+ fileName : file . fileName ,
599
+ fileContent : file . content ,
600
+ transpilers : [ ] ,
601
+ compilerId : compilerId || '' ,
602
+ } ) ) ;
603
+ const res = await transpileYaml ( reqDataArr ) ;
604
+
605
+ return files . map ( ( file , index ) => {
606
+ errorsReport . inFile ( file ) ;
607
+ if ( ! res [ index ] ) { // This should not happen in theory but just to be safe
608
+ errorsReport . error ( `No transpilation result received for the file ${ file . fileName } .` ) ;
609
+ return undefined ;
610
+ }
611
+ errorsReport . addErrors ( res [ index ] . errors ) ;
612
+ errorsReport . addWarnings ( res [ index ] . warnings as unknown as SyntaxErrorInterface [ ] ) ;
613
+ errorsReport . exitFile ( ) ;
614
+
615
+ file . content = res [ index ] . code ;
616
+ file . convertedToJs = true ;
617
+
618
+ return { ...file } ;
619
+ } ) ;
620
+ }
621
+
566
622
private async transpileJsFile (
567
623
file : FileContent ,
568
624
errorsReport : ErrorReporter ,
0 commit comments