@@ -71,6 +71,7 @@ export type DataSchemaCompilerOptions = {
7171 allowNodeRequire ?: boolean ;
7272 compiledScriptCache : LRUCache < string , vm . Script > ;
7373 compiledYamlCache : LRUCache < string , string > ;
74+ compiledJinjaCache : LRUCache < string , string > ;
7475} ;
7576
7677export type TranspileOptions = {
@@ -148,6 +149,8 @@ export class DataSchemaCompiler {
148149
149150 private readonly compiledYamlCache : LRUCache < string , string > ;
150151
152+ private readonly compiledJinjaCache : LRUCache < string , string > ;
153+
151154 private compileV8ContextCache : vm . Context | null = null ;
152155
153156 // FIXME: Is public only because of tests, should be private
@@ -182,6 +185,7 @@ export class DataSchemaCompiler {
182185 this . compilerId = options . compilerId || 'default' ;
183186 this . compiledScriptCache = options . compiledScriptCache ;
184187 this . compiledYamlCache = options . compiledYamlCache ;
188+ this . compiledJinjaCache = options . compiledJinjaCache ;
185189 }
186190
187191 public compileObjects ( compileServices : CompilerInterface [ ] , objects , errorsReport : ErrorReporter ) {
@@ -710,17 +714,28 @@ export class DataSchemaCompiler {
710714 errorsReport : ErrorReporter ,
711715 options : TranspileOptions
712716 ) : Promise < ( FileContent | undefined ) > {
713- const renderedFile = await this . yamlCompiler . renderTemplate (
714- file ,
715- this . standalone ? { } : this . cloneCompileContextWithGetterAlias ( this . compileContext ) ,
716- this . pythonContext !
717- ) ;
717+ const cacheKey = crypto . createHash ( 'md5' ) . update ( JSON . stringify ( file . content ) ) . digest ( 'hex' ) ;
718+
719+ let renderedFileContent : string ;
720+
721+ if ( this . compiledJinjaCache . has ( cacheKey ) ) {
722+ renderedFileContent = this . compiledJinjaCache . get ( cacheKey ) ! ;
723+ } else {
724+ const renderedFile = await this . yamlCompiler . renderTemplate (
725+ file ,
726+ this . standalone ? { } : this . cloneCompileContextWithGetterAlias ( this . compileContext ) ,
727+ this . pythonContext !
728+ ) ;
729+ renderedFileContent = renderedFile . content ;
730+
731+ this . compiledJinjaCache . set ( cacheKey , renderedFileContent ) ;
732+ }
718733
719734 // We update the jinja/yaml file content to the rendered content
720735 // to reuse the same transpileYamlFile() flow afterward which
721736 // will update the content to the transpiled js content
722737 // avoiding costly YAML/Python parsing again.
723- file . content = renderedFile . content ;
738+ file . content = renderedFileContent ;
724739
725740 return this . transpileYamlFile ( file , errorsReport , options ) ;
726741 }
0 commit comments