shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
72 lines • 3.21 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.JavascriptContentHandler = void 0;
var uglify_es_1 = require("uglify-es");
var event_handler_1 = require("./event-handler");
var file_functions_1 = require("../functions/file.functions");
var JavascriptContentHandler = /** @class */ (function (_super) {
__extends(JavascriptContentHandler, _super);
function JavascriptContentHandler() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.minify = uglify_es_1.minify;
_this.event = 'file-contents-added';
_this.processEvent = function (file) {
if (file.extension != "js")
return Promise.resolve();
return _this.minfiyJavascript(file).then(function (_) {
_this.logger.log("Content has been updated for file '".concat(file.name, "'."));
return _this.alertFileAvailable(file);
});
};
_this.minfiyJavascript = function (file) {
if (!_this.config.production)
return _this.updateFileContents(file, file.text);
return new Promise(function (res, err) {
var map = {};
map[file.name] = file.text;
var rslt = _this.minify(map, _this.getMinifyOptions(file.name));
if (rslt.error) {
return err(new Error(rslt.error.message));
}
return _this.updateFileContents(file, rslt.code).then(res);
});
};
_this.getMinifyOptions = function (name) {
return {
compress: { passes: 2 },
nameCache: {},
output: {
beautify: false
}
};
};
_this.updateFileContents = function (file, content) {
_this.context.models.files.update(file.name, function (file) {
file.content = content;
if (_this.config.production)
file.routePath = (0, file_functions_1.ChangeExtension)(file.name, 'js', 'min.js');
return file;
});
return _this.context.saveChanges();
};
return _this;
}
return JavascriptContentHandler;
}(event_handler_1.EventHandler));
exports.JavascriptContentHandler = JavascriptContentHandler;
//# sourceMappingURL=javascript-content.handler.js.map