siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
85 lines (84 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path_1 = require("path");
var errors_1 = require("../errors");
var utils_1 = require("../../utils");
var tagNameSearch_1 = require("../transpilers/mimo/tagNameSearch");
var CSS_1 = require("./CSS");
var Javascript_1 = require("./Javascript");
var HTMLModules_1 = require("./HTMLModules");
var Runtime = (function () {
function Runtime(sourceFile, destination, options, assets, injectMode) {
if (assets === void 0) { assets = {}; }
this.resolveModules = HTMLModules_1.default;
this.resolveCSS = CSS_1.default;
this.assets = assets;
this.options = options;
this.source = sourceFile.toString();
this.injectMode = injectMode;
this.destination = destination;
this.outDir = (0, utils_1.relativePath)(destination, "./");
this.assets[(0, path_1.basename)(this.source)] = (0, path_1.resolve)(this.source);
}
Runtime.prototype.resolveImages = function (nodes) {
var _this = this;
var images = (0, tagNameSearch_1.default)(nodes, "img").filter(function (images) {
var _a, _b, _c, _d, _e, _f;
return !(((_b = (_a = images.attributes) === null || _a === void 0 ? void 0 : _a.src) === null || _b === void 0 ? void 0 : _b.startsWith("http://")) ||
((_d = (_c = images.attributes) === null || _c === void 0 ? void 0 : _c.src) === null || _d === void 0 ? void 0 : _d.startsWith("http://")) ||
((_f = (_e = images.attributes) === null || _e === void 0 ? void 0 : _e.src) === null || _f === void 0 ? void 0 : _f.startsWith("data:")));
});
images.forEach(function (image) {
var _a;
var src = (_a = image.attributes) === null || _a === void 0 ? void 0 : _a.src;
var truePath = (0, utils_1.relativePath)(_this.source, src);
if (!(0, utils_1.fileExists)(truePath)) {
errors_1.default.enc("FILE_NON_EXISTENT", truePath);
}
if (_this.options.wickedMode) {
image.attributes.src = (0, utils_1.stringifytoBase64)(truePath);
}
else {
var fileMarker = (0, path_1.basename)(src);
if (_this.options.storeImagesSeparately)
(0, utils_1.tryMkingDir)("".concat(_this.outDir, "/img"));
if (_this.assets[fileMarker] && _this.assets[fileMarker] === truePath) {
image.attributes.src = _this.injectMode
? truePath
: "./".concat(_this.options.storeImagesSeparately ? "img/" : "").concat((0, path_1.basename)(src));
}
else if (_this.assets[fileMarker] &&
_this.assets[fileMarker] !== truePath) {
var a = 1;
while (_this.assets["".concat((0, utils_1.getFileName)(truePath), "-").concat(a).concat((0, path_1.extname)(truePath))]) {
a++;
}
var newname = "".concat((0, utils_1.getFileName)(truePath), "-").concat(a).concat((0, path_1.extname)(truePath));
image.attributes.src = _this.injectMode
? truePath
: "./".concat(_this.options.storeImagesSeparately ? "img/" : "").concat(newname);
(0, utils_1.copyInBase64)(truePath, "".concat(_this.outDir, "/").concat(_this.options.storeImagesSeparately ? "img/" : "").concat(newname));
}
else if (_this.assets[fileMarker] === undefined) {
(0, utils_1.copyInBase64)(truePath, "".concat(_this.outDir, "/").concat(_this.options.storeImagesSeparately ? "img/" : "").concat(fileMarker));
image.attributes.src = _this.injectMode
? truePath
: "./".concat(_this.options.storeImagesSeparately ? "img/" : "").concat((0, path_1.basename)(src));
_this.assets[fileMarker] = truePath;
}
}
});
return nodes;
};
Runtime.prototype.resolve = function (nodes) {
if (this.options.htmlModules) {
nodes = this.resolveModules(nodes, this.source, this.destination, this.options, this.assets);
}
nodes = this.resolveImages(nodes);
nodes = new Javascript_1.default().resolveJS(nodes, this.source, this.options, this.destination);
nodes = this.resolveCSS(nodes, this.source, this.destination, this.options, this.assets);
return nodes;
};
return Runtime;
}());
exports.default = Runtime;