@@ -47,6 +47,24 @@ const getThreadsCount = () => {
4747 return 3 ; // Default (like the workerpool do)
4848} ;
4949
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+
5068export type DataSchemaCompilerOptions = {
5169 compilerCache : CompilerCache ;
5270 omitErrors ?: boolean ;
@@ -288,8 +306,31 @@ export class DataSchemaCompiler {
288306 }
289307
290308 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+ const otherFiles : FileContent [ ] = [ ] ;
313+
314+ toCompile . forEach ( ( file ) => {
315+ if ( file . fileName . endsWith ( '.js' ) || file . convertedToJs ) {
316+ jsFiles . push ( file ) ;
317+ } else if ( file . fileName . endsWith ( '.jinja' ) ||
318+ ( file . fileName . endsWith ( '.yml' ) || file . fileName . endsWith ( '.yaml' ) )
319+ && file . content . match ( JINJA_SYNTAX )
320+ ) {
321+ jinjaFiles . push ( file ) ;
322+ } else if ( file . fileName . endsWith ( '.yml' ) || file . fileName . endsWith ( '.yaml' ) ) {
323+ yamlFiles . push ( file ) ;
324+ } else {
325+ otherFiles . push ( file ) ;
326+ }
327+ } ) ;
328+
329+ let jsFilesTasks : Promise < ( FileContent | undefined ) [ ] > [ ] = [ ] ;
330+ let yamlFilesTasks : Promise < ( FileContent | undefined ) [ ] > [ ] = [ ] ;
331+ // XXX: For now we don't have bulk transpilation for jinja files
332+ // let jinjaFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
333+ // let otherFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
293334
294335 if ( jsFiles . length > 0 ) {
295336 // Warming up swc compiler cache
@@ -300,26 +341,19 @@ export class DataSchemaCompiler {
300341
301342 await this . transpileJsFile ( dummyFile , errorsReport , { cubeNames, cubeSymbols, transpilerNames, contextSymbols : CONTEXT_SYMBOLS , compilerId, stage } ) ;
302343
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 } ) ) ;
344+ const jsChunks = splitFilesToChunks ( jsFiles , transpilationNativeThreadsCount ) ;
345+ jsFilesTasks = jsChunks . map ( chunk => this . transpileJsFilesNativeBulk ( chunk , errorsReport , { transpilerNames, compilerId } ) ) ;
317346 }
318347
319- const nonJsFilesTasks = toCompile . filter ( file => ! file . fileName . endsWith ( '.js' ) && ! file . convertedToJs )
320- . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames, compilerId } ) ) ;
348+ if ( yamlFiles . length > 0 ) {
349+ const yamlChunks = splitFilesToChunks ( yamlFiles , transpilationNativeThreadsCount ) ;
350+ yamlFilesTasks = yamlChunks . map ( chunk => this . transpileYamlFilesNativeBulk ( chunk , errorsReport , { transpilerNames, compilerId } ) ) ;
351+ }
321352
322- results = ( await Promise . all ( [ ...nonJsFilesTasks , ...JsFilesTasks ] ) ) . flat ( ) ;
353+ const jinjaAndOtherFilesTasks = jinjaFiles . concat ( otherFiles )
354+ . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames } ) ) ;
355+
356+ results = ( await Promise . all ( [ ...jsFilesTasks , ...yamlFilesTasks , ...jinjaAndOtherFilesTasks ] ) ) . flat ( ) ;
323357 } else if ( transpilationWorkerThreads ) {
324358 results = await Promise . all ( toCompile . map ( f => this . transpileFile ( f , errorsReport , { cubeNames, cubeSymbols, transpilerNames } ) ) ) ;
325359 } else {
@@ -521,11 +555,7 @@ export class DataSchemaCompiler {
521555 }
522556 }
523557
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 (
558+ private async transpileJsFilesNativeBulk (
529559 files : FileContent [ ] ,
530560 errorsReport : ErrorReporter ,
531561 { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions
@@ -563,6 +593,36 @@ export class DataSchemaCompiler {
563593 } ) ;
564594 }
565595
596+ private async transpileYamlFilesNativeBulk (
597+ files : FileContent [ ] ,
598+ errorsReport : ErrorReporter ,
599+ { compilerId } : TranspileOptions
600+ ) : Promise < ( FileContent | undefined ) [ ] > {
601+ const reqDataArr = files . map ( file => ( {
602+ fileName : file . fileName ,
603+ fileContent : file . content ,
604+ transpilers : [ ] ,
605+ compilerId : compilerId || '' ,
606+ } ) ) ;
607+ const res = await transpileYaml ( reqDataArr ) ;
608+
609+ return files . map ( ( file , index ) => {
610+ errorsReport . inFile ( file ) ;
611+ if ( ! res [ index ] ) { // This should not happen in theory but just to be safe
612+ errorsReport . error ( `No transpilation result received for the file ${ file . fileName } .` ) ;
613+ return undefined ;
614+ }
615+ errorsReport . addErrors ( res [ index ] . errors ) ;
616+ errorsReport . addWarnings ( res [ index ] . warnings as unknown as SyntaxErrorInterface [ ] ) ;
617+ errorsReport . exitFile ( ) ;
618+
619+ file . content = res [ index ] . code ;
620+ file . convertedToJs = true ;
621+
622+ return { ...file } ;
623+ } ) ;
624+ }
625+
566626 private async transpileJsFile (
567627 file : FileContent ,
568628 errorsReport : ErrorReporter ,
0 commit comments