@@ -15,7 +15,7 @@ import { FileContent, getEnv, isNativeSupported, SchemaFileRepository } from '@c
1515import { NativeInstance , PythonCtx , transpileJs } from '@cubejs-backend/native' ;
1616import { UserError } from './UserError' ;
1717import { ErrorReporter , ErrorReporterOptions , SyntaxErrorInterface } from './ErrorReporter' ;
18- import { CONTEXT_SYMBOLS , CubeSymbols } from './CubeSymbols' ;
18+ import { CONTEXT_SYMBOLS , CubeDefinition , CubeSymbols } from './CubeSymbols' ;
1919import { ViewCompilationGate } from './ViewCompilationGate' ;
2020import { TranspilerInterface } from './transpilers' ;
2121import { CompilerInterface } from './PrepareCompiler' ;
@@ -302,7 +302,24 @@ export class DataSchemaCompiler {
302302 return results . filter ( f => ! ! f ) ;
303303 } ;
304304
305- const compilePhase = async ( compilers : CompileCubeFilesCompilers , stage : 0 | 1 | 2 | 3 ) => this . compileCubeFiles ( compilers , await transpile ( stage ) , errorsReport ) ;
305+ let cubes : CubeDefinition [ ] = [ ] ;
306+ let exports : Record < string , Record < string , any > > = { } ;
307+ let contexts : Record < string , any > [ ] = [ ] ;
308+ let compiledFiles : Record < string , boolean > = { } ;
309+ let asyncModules : CallableFunction [ ] = [ ] ;
310+
311+ const compilePhase = async ( compilers : CompileCubeFilesCompilers , stage : 0 | 1 | 2 | 3 ) => {
312+ const res = this . compileCubeFiles ( cubes , exports , contexts , compiledFiles , asyncModules , compilers , await transpile ( stage ) , errorsReport ) ;
313+
314+ // clear the objects for the next phase
315+ cubes = [ ] ;
316+ exports = { } ;
317+ contexts = [ ] ;
318+ compiledFiles = { } ;
319+ asyncModules = [ ] ;
320+
321+ return res ;
322+ } ;
306323
307324 return compilePhase ( { cubeCompilers : this . cubeNameCompilers } , 0 )
308325 . then ( ( ) => compilePhase ( { cubeCompilers : this . preTranspileCubeCompilers . concat ( [ this . viewCompilationGate ] ) } , 1 ) )
@@ -348,7 +365,11 @@ export class DataSchemaCompiler {
348365 return this . compilePromise ;
349366 }
350367
351- private async transpileFile ( file : FileContent , errorsReport : ErrorReporter , options : TranspileOptions = { } ) {
368+ private async transpileFile (
369+ file : FileContent ,
370+ errorsReport : ErrorReporter ,
371+ options : TranspileOptions = { }
372+ ) : Promise < ( FileContent | undefined ) > {
352373 if ( file . fileName . endsWith ( '.jinja' ) ||
353374 ( file . fileName . endsWith ( '.yml' ) || file . fileName . endsWith ( '.yaml' ) )
354375 // TODO do Jinja syntax check with jinja compiler
@@ -377,7 +398,11 @@ export class DataSchemaCompiler {
377398 * Right now it is used only for transpilation in native,
378399 * so no checks for transpilation type inside this method
379400 */
380- private async transpileJsFilesBulk ( files : FileContent [ ] , errorsReport : ErrorReporter , { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions ) {
401+ private async transpileJsFilesBulk (
402+ files : FileContent [ ] ,
403+ errorsReport : ErrorReporter ,
404+ { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions
405+ ) : Promise < ( FileContent | undefined ) [ ] > {
381406 // for bulk processing this data may be optimized even more by passing transpilerNames, compilerId only once for a bulk
382407 // but this requires more complex logic to be implemented in the native side.
383408 // And comparing to the file content sizes, a few bytes of JSON data is not a big deal here
@@ -411,7 +436,11 @@ export class DataSchemaCompiler {
411436 } ) ;
412437 }
413438
414- private async transpileJsFile ( file : FileContent , errorsReport : ErrorReporter , { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions ) {
439+ private async transpileJsFile (
440+ file : FileContent ,
441+ errorsReport : ErrorReporter ,
442+ { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage } : TranspileOptions
443+ ) : Promise < ( FileContent | undefined ) > {
415444 try {
416445 if ( getEnv ( 'transpilationNative' ) ) {
417446 const reqData = {
@@ -496,13 +525,16 @@ export class DataSchemaCompiler {
496525 return this . currentQuery ;
497526 }
498527
499- private async compileCubeFiles ( compilers : CompileCubeFilesCompilers , toCompile : FileContent [ ] , errorsReport : ErrorReporter ) {
500- const cubes = [ ] ;
501- const exports = { } ;
502- const contexts = [ ] ;
503- const compiledFiles = { } ;
504- const asyncModules = [ ] ;
505-
528+ private async compileCubeFiles (
529+ cubes : CubeDefinition [ ] ,
530+ exports : Record < string , Record < string , any > > ,
531+ contexts : Record < string , any > [ ] ,
532+ compiledFiles : Record < string , boolean > ,
533+ asyncModules : CallableFunction [ ] ,
534+ compilers : CompileCubeFilesCompilers ,
535+ toCompile : FileContent [ ] ,
536+ errorsReport : ErrorReporter
537+ ) {
506538 toCompile
507539 . forEach ( ( file ) => {
508540 this . compileFile (
@@ -526,7 +558,15 @@ export class DataSchemaCompiler {
526558 }
527559
528560 private compileFile (
529- file : FileContent , errorsReport : ErrorReporter , cubes , exports , contexts , toCompile , compiledFiles , asyncModules , { doSyntaxCheck } = { doSyntaxCheck : false }
561+ file : FileContent ,
562+ errorsReport : ErrorReporter ,
563+ cubes : CubeDefinition [ ] ,
564+ exports : Record < string , Record < string , any > > ,
565+ contexts : Record < string , any > [ ] ,
566+ toCompile : FileContent [ ] ,
567+ compiledFiles : Record < string , boolean > ,
568+ asyncModules : CallableFunction [ ] ,
569+ { doSyntaxCheck } = { doSyntaxCheck : false }
530570 ) {
531571 if ( compiledFiles [ file . fileName ] ) {
532572 return ;
@@ -584,7 +624,17 @@ export class DataSchemaCompiler {
584624 return script ;
585625 }
586626
587- public compileJsFile ( file : FileContent , errorsReport : ErrorReporter , cubes , contexts , exports , asyncModules , toCompile , compiledFiles , { doSyntaxCheck } = { doSyntaxCheck : false } ) {
627+ public compileJsFile (
628+ file : FileContent ,
629+ errorsReport : ErrorReporter ,
630+ cubes : CubeDefinition [ ] ,
631+ contexts : Record < string , any > [ ] ,
632+ exports : Record < string , Record < string , any > > ,
633+ asyncModules : CallableFunction [ ] ,
634+ toCompile : FileContent [ ] ,
635+ compiledFiles : Record < string , boolean > ,
636+ { doSyntaxCheck } = { doSyntaxCheck : false }
637+ ) {
588638 if ( doSyntaxCheck ) {
589639 // There is no need to run syntax check for data model files
590640 // because they were checked during transpilation/transformation phase
0 commit comments