Skip to content

Commit f84eab5

Browse files
committed
Preload Jinja templates to the engine
1 parent 9c873d7 commit f84eab5

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ export class DataSchemaCompiler {
225225
? files.filter(f => this.filesToCompile.includes(f.fileName))
226226
: files;
227227

228+
const jinjaTemplatedFiles = toCompile.filter((file) => file.fileName.endsWith('.jinja') ||
229+
(file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) && file.content.match(JINJA_SYNTAX));
230+
231+
if (jinjaTemplatedFiles.length > 0) {
232+
// Preload Jinja templates to the engine
233+
this.loadJinjaTemplates(jinjaTemplatedFiles);
234+
}
235+
228236
const errorsReport = new ErrorReporter(null, [], this.errorReportOptions);
229237
this.errorsReporter = errorsReport;
230238

@@ -398,8 +406,6 @@ export class DataSchemaCompiler {
398406
foundFile,
399407
errorsReport,
400408
compiledFiles,
401-
[],
402-
[],
403409
{ doSyntaxCheck: true }
404410
);
405411
exports[foundFile.fileName] = exports[foundFile.fileName] || {};
@@ -420,7 +426,7 @@ export class DataSchemaCompiler {
420426
asyncModules = [];
421427
transpiledFiles = await transpile(stage);
422428

423-
const res = this.compileCubeFiles(cubes, contexts, compiledFiles, asyncModules, compilers, transpiledFiles, toCompile, errorsReport);
429+
const res = this.compileCubeFiles(cubes, contexts, compiledFiles, asyncModules, compilers, transpiledFiles, errorsReport);
424430
compilePhaseTimer.end();
425431
return res;
426432
};
@@ -484,6 +490,22 @@ export class DataSchemaCompiler {
484490
return this.compilePromise;
485491
}
486492

493+
private loadJinjaTemplates(files: FileContent[]): void {
494+
if (NATIVE_IS_SUPPORTED !== true) {
495+
throw new Error(
496+
`Native extension is required to process jinja files. ${NATIVE_IS_SUPPORTED.reason}. Read more: ` +
497+
'https://github.com/cube-js/cube/blob/master/packages/cubejs-backend-native/README.md#supported-architectures-and-platforms'
498+
);
499+
}
500+
501+
const jinjaEngine = this.yamlCompiler.getJinjaEngine();
502+
503+
// XXX: Make this as bulk operation in the native side
504+
files.forEach((file) => {
505+
jinjaEngine.loadTemplate(file.fileName, file.content);
506+
});
507+
}
508+
487509
private async transpileFile(
488510
file: FileContent,
489511
errorsReport: ErrorReporter,
@@ -493,31 +515,35 @@ export class DataSchemaCompiler {
493515
return this.transpileJsFile(file, errorsReport, options);
494516
} else if (file.fileName.endsWith('.jinja') ||
495517
(file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml'))
496-
// TODO do Jinja syntax check with jinja compiler
497518
&& file.content.match(JINJA_SYNTAX)
498519
) {
499-
if (NATIVE_IS_SUPPORTED !== true) {
500-
throw new Error(
501-
`Native extension is required to process jinja files. ${NATIVE_IS_SUPPORTED.reason}. Read more: ` +
502-
'https://github.com/cube-js/cube/blob/master/packages/cubejs-backend-native/README.md#supported-architectures-and-platforms'
503-
);
520+
const transpiledFile = await this.yamlCompiler.compileYamlWithJinjaFile(
521+
file,
522+
errorsReport,
523+
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
524+
this.pythonContext!
525+
);
526+
if (transpiledFile) {
527+
// We update the jinja/yaml file content to the transpiled js content
528+
// and raise related flag so it will go JS transpilation flow afterward
529+
// avoiding costly YAML/Python parsing again.
530+
file.content = transpiledFile.content;
531+
file.convertedToJs = true;
504532
}
505533

506-
this.yamlCompiler.getJinjaEngine().loadTemplate(file.fileName, file.content);
507-
508-
return file;
534+
return transpiledFile;
509535
} else if (file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) {
510-
const res = this.yamlCompiler.transpileYamlFile(file, errorsReport);
536+
const transpiledFile = this.yamlCompiler.transpileYamlFile(file, errorsReport);
511537

512-
if (res) {
538+
if (transpiledFile) {
513539
// We update the yaml file content to the transpiled js content
514540
// and raise related flag so it will go JS transpilation flow afterward
515541
// avoiding costly YAML/Python parsing again.
516-
file.content = res.content;
542+
file.content = transpiledFile.content;
517543
file.convertedToJs = true;
518544
}
519545

520-
return res;
546+
return transpiledFile;
521547
} else {
522548
return file;
523549
}
@@ -661,7 +687,6 @@ export class DataSchemaCompiler {
661687
asyncModules: CallableFunction[],
662688
compilers: CompileCubeFilesCompilers,
663689
transpiledFiles: FileContent[],
664-
toCompile: FileContent[],
665690
errorsReport: ErrorReporter
666691
) {
667692
transpiledFiles
@@ -670,8 +695,6 @@ export class DataSchemaCompiler {
670695
file,
671696
errorsReport,
672697
compiledFiles,
673-
asyncModules,
674-
toCompile
675698
);
676699
});
677700
await asyncModules.reduce((a: Promise<void>, b: CallableFunction) => a.then(() => b()), Promise.resolve());
@@ -687,8 +710,6 @@ export class DataSchemaCompiler {
687710
file: FileContent,
688711
errorsReport: ErrorReporter,
689712
compiledFiles: Record<string, boolean>,
690-
asyncModules: CallableFunction[],
691-
toCompile: FileContent[],
692713
{ doSyntaxCheck } = { doSyntaxCheck: false }
693714
) {
694715
if (compiledFiles[file.fileName]) {
@@ -704,28 +725,15 @@ export class DataSchemaCompiler {
704725
this.compileJsFile(file, errorsReport, { doSyntaxCheck });
705726
compileJsFileTimer.end();
706727
} else if (file.fileName.endsWith('.yml.jinja') || file.fileName.endsWith('.yaml.jinja') ||
707-
(
708-
file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')
709-
// TODO do Jinja syntax check with jinja compiler
710-
) && file.content.match(JINJA_SYNTAX)
728+
(file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) &&
729+
file.content.match(JINJA_SYNTAX)
711730
) {
712-
asyncModules.push(async () => {
713-
const transpiledFile = await this.yamlCompiler.compileYamlWithJinjaFile(
714-
file,
715-
errorsReport,
716-
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
717-
this.pythonContext!
718-
);
719-
if (transpiledFile) {
720-
const originalFile = toCompile.find(f => f.fileName === file.fileName);
721-
originalFile!.content = transpiledFile.content;
722-
originalFile!.convertedToJs = true;
723-
724-
this.compileJsFile(transpiledFile, errorsReport);
725-
}
726-
});
731+
const compileJinjaFileTimer = perfTracker.start('compileJinjaFile (actually js)');
732+
// original jinja/yaml file was already transpiled into js
733+
this.compileJsFile(file, errorsReport);
734+
compileJinjaFileTimer.end();
727735
} else if (file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) {
728-
const compileYamlFileTimer = perfTracker.start('compileYamlFile');
736+
const compileYamlFileTimer = perfTracker.start('compileYamlFile (actually js)');
729737
// original yaml file was already transpiled into js
730738
this.compileJsFile(file, errorsReport);
731739
compileYamlFileTimer.end();

0 commit comments

Comments
 (0)