siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
118 lines (117 loc) • 5.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var types_1 = require("../../types");
var errors_1 = require("../errors");
var utils_1 = require("../../utils");
var tagNameSearch_1 = require("../transpilers/mimo/tagNameSearch");
var ezra_1 = require("../transpilers/ezra");
var base_1 = require("../transpilers/ezra/bundler/base");
var JavascriptResolve = (function () {
function JavascriptResolve() {
this.outputText = "";
this.isModule = false;
this.trail = [];
}
JavascriptResolve.prototype.resolveJS = function (nodes, source, options, destination) {
this.nodes = nodes;
this.source = source;
this.destination = destination;
this.options = options;
this.start();
return nodes;
};
JavascriptResolve.prototype.start = function () {
var _a;
var _b, _c, _d, _e;
this.scripts = (0, tagNameSearch_1.default)(this.nodes, "script").filter(function (s) { var _a, _b; return (_b = (_a = s.attributes) === null || _a === void 0 ? void 0 : _a.src) === null || _b === void 0 ? void 0 : _b.length; });
var body = (0, tagNameSearch_1.default)(this.nodes, "body")[0];
var script, outputAst = new types_1.Program(0);
var bundler = new base_1.bundler_internals();
if (this.scripts.length === 0)
return this.nodes;
for (var i = 0; this.scripts[i]; i++) {
script = this.scripts[i];
var pathToFile = (0, utils_1.relativePath)(this.source, script.attributes.src);
if (script.attributes.type === "module")
this.isModule = true;
else
errors_1.default.enc("SOMETHING_WENT_WRONG", pathToFile);
if (!(0, utils_1.fileExists)(pathToFile)) {
errors_1.default.enc("MISSING_SCRIPT", this.source, script.start, {
token: script.attributes.src,
});
}
(_a = outputAst.body).push.apply(_a, bundler.bundle(pathToFile, {
allowJSX: this.options.allowJSX,
sourceMaps: false,
writeImagesIntoBundle: this.options.wickedMode,
storeImagesSeparately: this.options.storeImagesSeparately,
}).body);
if (bundler.stylesheets.length) {
var head = (0, tagNameSearch_1.default)(this.nodes, "head")[0];
if (head) {
bundler.stylesheets.forEach(function (stylesheet) {
var _a;
(_a = head.children) === null || _a === void 0 ? void 0 : _a.push({
tagName: "link",
isVoid: true,
parent: head,
childID: head.children.length,
children: [],
attributes: { rel: "stylesheet", href: stylesheet },
});
});
}
}
delete script.type;
}
var outputFolder = (0, utils_1.relativePath)(this.destination, "./");
if (this.options.internalJS || this.options.wickedMode) {
var script_1 = {
tagName: "script",
type: "element",
childID: (_b = body.children) === null || _b === void 0 ? void 0 : _b.length,
parent: body,
attributes: {
type: "module",
},
content: ezra_1.default.generate(outputAst, {
format: this.options.formatFiles && !this.options.wickedMode,
indent: 3,
}) + (outputAst.body.length ? "\n" : ""),
};
(_c = body.children) === null || _c === void 0 ? void 0 : _c.push(script_1);
}
else {
var bundle = (0, utils_1.getFileName)(this.destination) + ".bundle.js";
(0, fs_1.writeFileSync)("".concat(outputFolder, "/").concat(bundle), ezra_1.default.generate(outputAst, {
format: this.options.formatFiles && !this.options.wickedMode,
indent: 0,
}));
var script_2 = {
tagName: "script",
type: "element",
childID: (_d = body.children) === null || _d === void 0 ? void 0 : _d.length,
parent: body,
attributes: {
type: "module",
src: "./".concat(bundle),
},
};
(_e = body.children) === null || _e === void 0 ? void 0 : _e.push(script_2);
}
if (!this.options.wickedMode) {
var imageOutputFolder_1 = outputFolder;
if (this.options.storeImagesSeparately) {
imageOutputFolder_1 = "".concat(outputFolder, "\\img");
(0, utils_1.tryMkingDir)(imageOutputFolder_1);
}
bundler.images.forEach(function (imageSrc, imageName) {
(0, utils_1.copyInBase64)(imageSrc, "".concat(imageOutputFolder_1, "\\").concat(imageName));
});
}
};
return JavascriptResolve;
}());
exports.default = JavascriptResolve;