@@ -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 ) {
@@ -706,17 +710,28 @@ export class DataSchemaCompiler {
706710 errorsReport : ErrorReporter ,
707711 options : TranspileOptions
708712 ) : Promise < ( FileContent | undefined ) > {
709- const renderedFile = await this . yamlCompiler . renderTemplate (
710- file ,
711- this . standalone ? { } : this . cloneCompileContextWithGetterAlias ( this . compileContext ) ,
712- this . pythonContext !
713- ) ;
713+ const cacheKey = crypto . createHash ( 'md5' ) . update ( JSON . stringify ( file . content ) ) . digest ( 'hex' ) ;
714+
715+ let renderedFileContent : string ;
716+
717+ if ( this . compiledJinjaCache . has ( cacheKey ) ) {
718+ renderedFileContent = this . compiledJinjaCache . get ( cacheKey ) ! ;
719+ } else {
720+ const renderedFile = await this . yamlCompiler . renderTemplate (
721+ file ,
722+ this . standalone ? { } : this . cloneCompileContextWithGetterAlias ( this . compileContext ) ,
723+ this . pythonContext !
724+ ) ;
725+ renderedFileContent = renderedFile . content ;
726+
727+ this . compiledJinjaCache . set ( cacheKey , renderedFileContent ) ;
728+ }
714729
715730 // We update the jinja/yaml file content to the rendered content
716731 // to reuse the same transpileYamlFile() flow afterward which
717732 // will update the content to the transpiled js content
718733 // avoiding costly YAML/Python parsing again.
719- file . content = renderedFile . content ;
734+ file . content = renderedFileContent ;
720735
721736 return this . transpileYamlFile ( file , errorsReport , options ) ;
722737 }
0 commit comments