shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
84 lines • 4.37 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.HtmlContentHandler = void 0;
var event_handler_1 = require("./event-handler");
var app_composition_1 = require("../composition/app.composition");
var html_minifier_1 = require("html-minifier");
var logger_1 = require("../logger");
var dynamic_route_1 = require("../models/dynamic-route");
var HtmlContentHandler = /** @class */ (function (_super) {
__extends(HtmlContentHandler, _super);
function HtmlContentHandler() {
var _this = _super.call(this) || this;
_this.minify = html_minifier_1.minify;
_this.event = 'file-model-processed';
_this.minifyOptions = { collapseWhitespace: true };
_this.processEvent = function (file) {
if (file.extension != 'html')
return Promise.resolve();
if (file.model.shaman.dynamic)
return _this.processDynamicFiles(file);
var options = _this.minifyOptions;
_this.context.models.files.update(file.name, function (f) {
f.content = _this.handlebarsService.renderTemplate(file, null, file.query);
if (!!_this.config.production)
f.content = _this.minify(f.content, options);
return f;
});
return _this.context.saveChanges().then(function (_) { return _this.alertFileAvailable(file); });
};
_this.processDynamicFiles = function (file) {
var query = file.model.shaman.query.find(function (q) { return q.dynamic; });
if (!query) {
var err = "File '".concat(file.name, "' was marked as dynamic, but no queries were found.");
return _this.dynamicWarning(err, file);
}
return _this.queryAdapter.run(query).then(function (models) {
if (models.length == 0) {
var err = "File '".concat(file.name, "' was marked as dynamic, but no models were found.");
return _this.dynamicWarning(err, file);
}
models.forEach(function (model) { return _this.processDynamicFile(file, model); });
return _this.context.saveChanges().then(function (_) { return _this.alertFileAvailable(file); });
});
};
_this.processDynamicFile = function (file, model) {
var options = _this.minifyOptions;
var path = file.model.shaman.dynamic.path;
var key = file.model.shaman.dynamic.name;
var route = new dynamic_route_1.DynamicRoute();
route.path = "".concat(path).concat(model[key]);
route.file = file.name;
model.shaman = file.model.shaman;
route.content = _this.handlebarsService.renderTemplate(file, model);
if (!!_this.config.production)
route.content = (0, html_minifier_1.minify)(route.content, options);
_this.context.models.dynamicRoutes.upsert(route.path, route);
};
_this.dynamicWarning = function (message, file) {
_this.logger.log(message, logger_1.LogLevels.warn);
return _this.alertFileAvailable(file);
};
_this.handlebarsService = app_composition_1.IoC.get(app_composition_1.TYPES.HandlebarsService);
_this.queryAdapter = app_composition_1.IoC.get(app_composition_1.TYPES.QueryAdapter);
return _this;
}
return HtmlContentHandler;
}(event_handler_1.EventHandler));
exports.HtmlContentHandler = HtmlContentHandler;
//# sourceMappingURL=html-content.handler.js.map