import { readFile } from 'graceful-fs'; import * as Handlebars from 'handlebars'; import { Observable, Observer } from 'rxjs'; import { shareReplay } from 'rxjs/operators'; const jsStringEscape = require('js-string-escape'); const helpers = require('handlebars-helpers')({ handlebars: Handlebars }); // add custom helpers Handlebars.registerHelper('jsEscape', jsStringEscape); function _compile(aFile: string): Observable { return Observable.create((observer: Observer) => { // read the file readFile(aFile, 'utf-8', (err, data) => { if (err) { observer.error(err); } else { try { observer.next(Handlebars.compile(data)); observer.complete(); } catch (error) { observer.error(error); } } }); }).pipe(shareReplay()) } export { _compile as hbsCompile }