siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
494 lines (493 loc) • 14.9 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HTMLError = exports.removeInvalidChars = exports.counterpart = exports.assoc = exports.precedence = exports.JSFiles = exports.OPERATORS = exports.getMIMEType = exports.isIllegalCSSIdentifier = exports.isValidIdentifierCharacter = exports.isAlphaNumeric = exports.isOctalDigit = exports.isBinaryDigit = exports.isHexDigit = exports.trace = exports.operators = exports.declarators = exports.JSkeywords = exports.last = exports.lastRealChar = exports.isBracket = exports.isNewLine = exports.isSentenceCase = exports.isAlphabetic = exports.isNum = exports.isDigit = exports.imageExts = exports.stringMarkers = exports.isVoid = exports.HTMLTags = exports.deprecatedTags = exports.EMPTY_SPACE = exports.NEWLINE = exports.isForeignTag = exports.checkForEnd = exports.isSpaceCharac = void 0;
var fs_1 = require("fs");
var path_1 = require("path");
var errors_1 = require("../core/errors");
var fs_utils_1 = require("./fs_utils");
function isSpaceCharac(character) {
return /\u0020|\u0009|\u000A|\u000C|\u000D/.test(character);
}
exports.isSpaceCharac = isSpaceCharac;
function checkForEnd(character, source) {
if (!character)
errors_1.default.enc("ABRUPT", source);
}
exports.checkForEnd = checkForEnd;
function isForeignTag(tagName) {
return tagName ? ["script", "style"].includes(tagName) : false;
}
exports.isForeignTag = isForeignTag;
exports.NEWLINE = "\n";
exports.EMPTY_SPACE = /\s|\r|\n/;
var voidTags = {
"!DOCTYPE": true,
area: true,
base: true,
br: true,
col: true,
command: true,
embed: true,
hr: true,
img: true,
input: true,
keygen: true,
link: true,
meta: true,
param: true,
source: true,
track: true,
wbr: true,
module: true,
};
exports.deprecatedTags = {
acronym: true,
applet: true,
basefont: true,
big: true,
dir: true,
font: true,
frame: true,
frameset: true,
noframes: true,
tt: true,
center: true,
strike: true,
};
exports.HTMLTags = __assign(__assign(__assign({}, voidTags), exports.deprecatedTags), { a: true, abbr: true, address: true, article: true, aside: true, audio: true, b: true, bdi: true, bdo: true, blockquote: true, body: true, button: true, canvas: true, caption: true, cite: true, code: true, colgroup: true, data: true, datalist: true, dd: true, del: true, details: true, dfn: true, dialog: true, div: true, dl: true, dt: true, em: true, fieldset: true, figcaption: true, figure: true, footer: true, form: true, h1: true, h2: true, h3: true, h4: true, h5: true, h6: true, head: true, header: true, html: true, i: true, iframe: true, ins: true, kbd: true, label: true, legend: true, li: true, main: true, mark: true, menu: true, meter: true, nav: true, noscript: true, object: true, ol: true, optgroup: true, option: true, output: true, p: true, picture: true, pre: true, progress: true, q: true, rp: true, rt: true, ruby: true, s: true, samp: true, script: true, section: true, select: true, small: true, source: true, span: true, strong: true, style: true, sub: true, summary: true, sup: true, svg: true, table: true, tbody: true, td: true, template: true, textarea: true, tfoot: true, th: true, thead: true, title: true, time: true, tr: true, u: true, ul: true, var: true, video: true });
function isVoid(tagName) {
return tagName ? voidTags[tagName] === true : false;
}
exports.isVoid = isVoid;
exports.stringMarkers = ["'", "`", '"'];
exports.imageExts = {
png: true,
jpeg: true,
jpg: true,
bmp: true,
svg: true,
gif: true,
tiff: true,
webp: true,
};
function isDigit(char) {
return char ? char.charCodeAt(0) >= 48 && char.charCodeAt(0) <= 57 : false;
}
exports.isDigit = isDigit;
function isNum(char) {
return char
? char.replace(/[0-9]/g, "").replace(/\./, "") === "" &&
!Number.isNaN(Number(char))
: false;
}
exports.isNum = isNum;
function isAlphabetic(char) {
return char ? char.toLowerCase() !== char.toUpperCase() : false;
}
exports.isAlphabetic = isAlphabetic;
function isSentenceCase(char) {
return char[0].toUpperCase() === char[0];
}
exports.isSentenceCase = isSentenceCase;
function isNewLine(char) {
return char ? char === "\n" : false;
}
exports.isNewLine = isNewLine;
function isBracket(char) {
return char.length === 1 && /\(|\)|\[|\]/.test(char);
}
exports.isBracket = isBracket;
function lastRealChar(str) {
var i = str.length - 1;
while (str[i] && /\n|\r|\s/.test(str[i]))
i--;
return { character: str[i], index: i };
}
exports.lastRealChar = lastRealChar;
function last(array) {
return array[array.length - 1];
}
exports.last = last;
exports.JSkeywords = {
ES1: [
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"new",
"null",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
],
ES3: [
"abstract",
"boolean",
"byte",
"char",
"class",
"double",
"enum",
"export",
"extends",
"final",
"float",
"goto",
"implements",
"import",
"int",
"interface",
"long",
"native",
"package",
"private",
"protected",
"public",
"short",
"static",
"super",
"synchronized",
"throws",
"transient",
"volatile",
],
ES5: [
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"implements",
"import",
"in",
"instanceof",
"interface",
"let",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"static",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
"yield",
],
};
exports.declarators = ["const", "var", "let"];
exports.operators = {
_ignore1_: [
"+",
"-",
"*",
"/",
"%",
"|",
":",
"&",
"^",
"=",
".",
"^",
">",
"<",
",",
";",
],
_suceeding1_: ["!", "~", "(", "{", "["],
_preceeding1_: ["}", "]", ")"],
_ignore2_: [
"&&",
"||",
"??",
"~~",
"**",
"+=",
"=>",
"-=",
"?.",
"|=",
"!=",
"*=",
"%=",
"&=",
"^=",
">=",
"<=",
"==",
">>",
"<<",
],
_suceeding2_: ["++", "--"],
_ignore3_: ["===", "!==", "<<=", ">>=", "**=", ">>>", "||=", "&&=", "??="],
_suceeding3_: ["..."],
_ignore4_: [">>>="],
};
function trace(source, character) {
var i = 1, line = 1, col = 1;
var sourceText = (0, fs_1.readFileSync)(source).toString();
while (i < character) {
if (sourceText[i] === "\n") {
line++;
col = 0;
}
i++;
col++;
}
return { line: line, col: col + 1 };
}
exports.trace = trace;
function isHexDigit(character) {
return isDigit(character) || /[A-Fa-f]/.test(character);
}
exports.isHexDigit = isHexDigit;
var BINARIES = /0|1/;
function isBinaryDigit(character) {
return BINARIES.test(character);
}
exports.isBinaryDigit = isBinaryDigit;
var OCTALS = /[0-7]/;
function isOctalDigit(character) {
return OCTALS.test(character);
}
exports.isOctalDigit = isOctalDigit;
function isAlphaNumeric(character) {
return isAlphabetic(character) || isDigit(character);
}
exports.isAlphaNumeric = isAlphaNumeric;
function isValidIdentifierCharacter(char) {
return isAlphabetic(char) || isDigit(char) || /\$|\_/.test(char);
}
exports.isValidIdentifierCharacter = isValidIdentifierCharacter;
function isIllegalCSSIdentifier(identifier) {
return (/[0-9]/.test(identifier[0]) ||
/\?|#|\$|`|'|"|&|\(|\)|@|;|,|\[|\]|\%|\+|\*|\=/.test(identifier));
}
exports.isIllegalCSSIdentifier = isIllegalCSSIdentifier;
var MIME_TYPES = {
aac: "audio/aac",
abw: "application/x-abiword",
arc: "audio/x-freearc",
avif: "image/avif",
avi: "video/x-msvideo",
azw: "application/vnd.amazon.ebook",
bin: "application/octet-stream",
bmp: "image/bmp",
bz: "application/x-bzip",
bz2: "application/x-bzip2",
cda: "application/x-cdf",
csh: "application/x-csh",
css: "text/css",
csv: "text/csv",
doc: "application/msword",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
eot: "application/vnd.ms-fontobject",
epub: "application/epub+zip",
gz: "application/gzip",
gif: "image/gif",
html: "text/html",
htm: "text/html",
ico: "image/vnd.microsoft.icon",
ics: "text/calendar",
jar: "application/java-archive",
jpg: "image/jpg",
jpeg: "image/jpg",
js: "text/javascript",
mjs: "text/javascript",
json: "application/json",
jsonld: "application/ld+json",
mid: "audio/midi",
midi: "audio/x-midi",
mp3: "audio/mpeg",
mp4: "video/mp4",
mpeg: "video/mpeg",
mpkg: "application/vnd.apple.installer+xml",
odp: "application/vnd.oasis.opendocument.presentation",
ods: "application/vnd.oasis.opendocument.spreadsheet",
odt: "application/vnd.oasis.opendocument.text",
oga: "audio/ogg",
ogv: "video/ogg",
ogx: "application/ogg",
opus: "audio/opus",
otf: "font/otf",
png: "image/png",
pdf: "application/pdf",
php: "application/x-httpd-php",
ppt: "application/vnd.ms-powerpoint",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
rar: "application/vnd.rar",
rtf: "application/rtf",
sh: "application/x-sh",
svg: "image/svg+xml",
swf: "application/x-shockwave-flash",
tar: "application/x-tar",
tiff: "image/tiff",
tif: "image/tiff",
ts: "video/mp2t",
ttf: "font/ttf",
txt: "text/plain",
vsd: "application/vnd.visio",
wav: "audio/wav",
weba: "audio/webm",
webm: "video/webm",
webp: "image/webp",
woff: "font/woff",
woff2: "font/woff2",
xhtml: "application/xhtml+xml",
xls: "application/vnd.ms-excel",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
xml: "application/xml",
xul: "application/vnd.mozilla.xul+xml",
zip: "application/zip",
"3gp": "video/3gpp2",
"7z": "application/x-7z-compressed",
};
function getMIMEType(ext) {
var _a;
return (_a = MIME_TYPES[ext]) !== null && _a !== void 0 ? _a : "application/octet-stream";
}
exports.getMIMEType = getMIMEType;
exports.OPERATORS = exports.operators._ignore1_.concat(exports.operators._ignore2_, exports.operators._ignore3_, exports.operators._ignore4_, exports.operators._preceeding1_, exports.operators._suceeding1_, exports.operators._suceeding2_, exports.operators._suceeding3_);
exports.JSFiles = {
js: true,
cjs: true,
mjs: true,
jsx: true,
};
exports.precedence = {
"(": 19,
")": 19,
".": 18,
"[": 18,
new: 18,
"?.": 18,
call: 17,
postfix: 16,
prefix: 15,
"!": 15,
"~": 15,
typeof: 15,
void: 15,
delete: 15,
await: 15,
"++": 15,
"--": 15,
"**": 14,
"*": 13,
"/": 13,
"%": 13,
"+": 12,
"-": 12,
"<<": 11,
">>": 11,
">>>": 11,
"<": 10,
">": 10,
"<=": 10,
">=": 10,
in: 10,
of: 10,
instanceof: 10,
"===": 9,
"==": 9,
"!==": 9,
"!=": 9,
"&": 8,
"^": 7,
"|": 6,
"&&": 5,
"||": 4,
"??": 3.5,
"?": 3,
"=": 2,
"=>": 2,
yield: 2,
",": 1,
none: 0,
};
function assoc(operator) {
return /\*\*|\?|\=/.test(operator) ? "RL" : "LR";
}
exports.assoc = assoc;
exports.counterpart = {
"[": "]",
"(": ")",
"{": "}",
"<": ">",
};
function removeInvalidChars(str) {
return str.replace(/(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]/g, " ");
}
exports.removeInvalidChars = removeInvalidChars;
function HTMLError(e) {
var _a;
if (e.root && e.location) {
e.location = (0, path_1.relative)((0, fs_utils_1.relativePath)(e.root, "./"), e.location);
}
e.heading = e.heading.replace(/\\/g, "\\\\");
e.location = (_a = e.location) === null || _a === void 0 ? void 0 : _a.replace(/\\/g, "/");
return "<html style='background: #151515'>\n <head>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>Error</title>\n </head>\n <body style='\n font-family: sans-serif;\n height: fit-content;\n padding: 0 5%;\n padding-top: 60px;\n margin: 0'>\n <h1 style='color: #d62929;'>Siphon Compile Error.</h1>\n <hr />\n <h3 style='color:white'></h3>\n <p style='color: gray; margin-top: 0'></p>\n <script>\n document.querySelector('h3').innerText = \"".concat(e.heading, "\"\n document.querySelector('p').innerText = \n \"").concat(e.position ? "".concat(e.location, ":").concat(e.position.line, ":").concat(e.position.col) : "", "\"\n var error = new Error(`").concat(e.heading, "`);\n throw error;\n </script>\n </body>\n</html>");
}
exports.HTMLError = HTMLError;