siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
167 lines (166 loc) • 7.57 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bundler_utils = exports.defaults = void 0;
var fs_1 = require("fs");
var path_1 = require("path");
var errors_1 = require("../../../errors");
var types_1 = require("../../../../types");
var utils_1 = require("../../../../utils");
var creator_1 = require("../traverser/helpers/creator");
exports.defaults = {
sourceMaps: true,
allowJSX: false,
writeImagesIntoBundle: false,
storeImagesSeparately: false,
};
var ID = 0;
var bundler_utils = (function () {
function bundler_utils() {
this.tree = new types_1.Program(0);
this.sourceMappings = new Map();
this.stylesheets = [];
this.images = new Map();
this.globalIdentifierMap = new Map();
this.globalAssetMap = new Map();
this.assets = new Map();
}
bundler_utils.prototype.start = function (file) {
var _a;
var _this = this;
file = (0, path_1.resolve)(file.toString());
var extension = (0, path_1.extname)(file).slice(1);
var asset;
switch (true) {
case utils_1.JSFiles[extension] === true:
asset = this.createJSAsset(file);
break;
case extension === "css":
asset = this.createCSSAsset(file);
break;
case utils_1.imageExts[extension] === true:
if (!this.options.writeImagesIntoBundle) {
asset = this.createImageAsset(file);
break;
}
default:
asset = this.createUnknownAsset(file);
}
this.assets.set(file, asset);
(_a = this.tree.body).push.apply(_a, asset.module);
asset.dependencies.forEach(function (dependency) {
if (!_this.assets.has(dependency.path.toString())) {
_this.start(dependency.path);
}
});
};
bundler_utils.prototype.getDependencyPath = function (node, filename) {
var dependencyPath = (0, utils_1.relativePath)(filename, node.source.value);
if (!(0, utils_1.fileExists)(dependencyPath)) {
switch (true) {
case (0, utils_1.fileExists)(dependencyPath + ".js"):
dependencyPath += ".js";
break;
case (0, utils_1.fileExists)(dependencyPath + "/index.js"):
dependencyPath += "/index.js";
break;
case this.options.allowJSX && (0, utils_1.fileExists)(dependencyPath + ".jsx"):
dependencyPath += ".jsx";
break;
case this.options.allowJSX && (0, utils_1.fileExists)(dependencyPath + "/index.jsx"):
dependencyPath += "/index.jsx";
break;
case (0, fs_1.existsSync)("node_modules/".concat(node.source.value)):
var node_module = "node_modules/".concat(node.source.value);
if ((0, utils_1.fileExists)("".concat(node_module, "/package.json"))) {
var pkgJSON = "".concat(node_module, "/package.json");
var pkg = require((0, path_1.resolve)(pkgJSON));
dependencyPath = pkg.main
? (0, utils_1.relativePath)(pkgJSON, pkg.main)
: (0, path_1.resolve)("".concat(node_module, "/index.js"));
}
else
dependencyPath = (0, path_1.resolve)("".concat(node_module, "/index.js"));
if ((0, utils_1.fileExists)(dependencyPath))
break;
default:
errors_1.default.enc("JS_IMPORTED_MODULE_MISSING", filename, node.source.loc.start, {
token: dependencyPath,
});
}
}
return (0, path_1.resolve)(dependencyPath);
};
bundler_utils.prototype.generateNewImage = function (filename) {
var name = (0, utils_1.getFileName)(filename);
var extension = (0, path_1.extname)(filename.toString());
var organicName = name + extension;
if (filename === this.images.get(organicName))
return organicName;
else if (this.images.has(organicName)) {
for (var i = 1; this.images.has(organicName); i++)
organicName = "".concat(name, "-").concat(i).concat(extension);
}
this.images.set(organicName, filename);
return organicName;
};
bundler_utils.prototype.uniqueIdentifier = function (type) {
if (type === void 0) { type = "init"; }
var identifier = new types_1.Identifier(0);
do
identifier.name = "_e".concat(type).concat(Math.random().toString(16).slice(12)).concat(type === "module" ? "$".concat(ID++) : "", "_");
while (this.globalIdentifierMap.has(identifier.name));
this.globalIdentifierMap.set(identifier.name, true);
return identifier;
};
bundler_utils.prototype.ModuleIdentifierNode = function (filePath) {
filePath = (0, path_1.resolve)(filePath);
var assetId = this.globalAssetMap.get(filePath);
var identifierNode;
if (assetId)
identifierNode = (0, creator_1.newIdentifier)(assetId);
else {
identifierNode = this.uniqueIdentifier();
this.globalAssetMap.set(filePath, identifierNode.name);
}
return identifierNode;
};
bundler_utils.prototype.prepareModule = function (ast, moduleIDNode) {
var Initializer = new types_1.VariableDeclaration(0);
Initializer.kind = "var";
var InitializerDec = new types_1.VariableDeclarator(0);
InitializerDec.id = this.uniqueIdentifier("");
InitializerDec.init = (0, creator_1.numberLiteral)(0);
Initializer.declarations.push(InitializerDec);
var InitPrompt = new types_1.IfStatement(0);
InitPrompt.test = InitializerDec.id;
var InitReturn = new types_1.ReturnStatement(0);
InitReturn.argument = moduleIDNode;
InitPrompt.consequent = InitReturn;
InitPrompt.alternate = (0, creator_1.expressionStatement)((0, creator_1.updateExpression)("++", false, InitializerDec.id));
var ModuleReturn = new types_1.ReturnStatement(0);
ModuleReturn.argument = moduleIDNode;
var ModuleFuntion = new types_1.FunctionDeclaration(0);
ModuleFuntion.id = moduleIDNode;
ModuleFuntion.async = false;
ModuleFuntion.expression = false;
ModuleFuntion.generator = false;
ModuleFuntion.params = [];
ModuleFuntion.body = (0, creator_1.blockStatement)(__spreadArray(__spreadArray([
InitPrompt
], ast.body, true), [
ModuleReturn,
], false));
return [Initializer, ModuleFuntion];
};
return bundler_utils;
}());
exports.bundler_utils = bundler_utils;