UNPKG

shaman-website-compiler

Version:

Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.

74 lines 3.8 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HandlebarsService = void 0; var _handlebars = require("handlebars"); var _fsx = require("fs-extra"); var _moment = require("moment-timezone"); var inversify_1 = require("inversify"); var app_composition_1 = require("../composition/app.composition"); var handlebars_functions_1 = require("../functions/handlebars.functions"); var handlebars_functions_2 = require("../functions/handlebars.functions"); var file_functions_1 = require("../functions/file.functions"); var HandlebarsService = /** @class */ (function () { function HandlebarsService() { var _this = this; this.registerPartials = function (files) { files.forEach(function (file) { return _handlebars.registerPartial(file.name, file.content); }); return files; }; this.registerHelpers = function (files) { var operations = files.map(function (file) { return _fsx.readFile(file.path, "utf8"); }); return Promise.all(operations).then(function (contents) { contents.forEach(function (content) { var func = new Function('handlebars', 'moment', content); func(_handlebars, _moment); }); }); }; this.renderTemplate = function (file, model, query) { var compiler = _handlebars.compile(file.content); return compiler({ model: model || file.model, query: query }); }; this.loadHandlebars = function () { _this.config.handlebars(_handlebars); }; /* istanbul ignore next */ this.registerDefaultHelpers = function () { var production = _this.config.production; _handlebars.registerHelper('bundles', function (bundles) { if (!production) return (0, handlebars_functions_1.CreateBundleTags)(bundles); return (0, handlebars_functions_1.CreateMinifiedBundleTags)(bundles); }); _handlebars.registerHelper('script', function (name) { if (production) name = (0, file_functions_1.ChangeExtension)(name, 'js', 'min.js'); return (0, handlebars_functions_2.CreateScript)(name); }); _handlebars.registerHelper('style', function (name) { if (production) name = (0, file_functions_1.ChangeExtension)(name, 'css', 'min.css'); return (0, handlebars_functions_2.CreateStyle)(name); }); }; this.config = app_composition_1.IoC.get(app_composition_1.TYPES.WebsiteConfig); this.loadHandlebars(); this.registerDefaultHelpers(); } HandlebarsService = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", []) ], HandlebarsService); return HandlebarsService; }()); exports.HandlebarsService = HandlebarsService; //# sourceMappingURL=handlebars.service.js.map