@maskedeng-tom/ssrsx
Version:
server side renderer with tsx
214 lines • 7.37 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentFcId = exports.getCurrentSsrsx = exports.parse = exports.Fragment = void 0;
const jsx_runtime_1 = require("ssrsxjsx/jsx-runtime");
Object.defineProperty(exports, "Fragment", { enumerable: true, get: function () { return jsx_runtime_1.Fragment; } });
const log_1 = require("../src/lib/log");
const styleToString_1 = require("../src/hooks/styleToString/styleToString");
const shortId_1 = require("../src/lib/shortId");
const changeCase_1 = require("../src/hooks/styleToString/changeCase");
////////////////////////////////////////////////////////////////////////////////
let currentSsrsx = undefined;
let currentFcId = undefined;
const getCurrentSsrsx = () => {
return currentSsrsx;
};
exports.getCurrentSsrsx = getCurrentSsrsx;
const getCurrentFcId = () => {
return currentFcId;
};
exports.getCurrentFcId = getCurrentFcId;
////////////////////////////////////////////////////////////////////////////////
const parseAttributes = (tagname, attributes, ssrsx) => {
//
if (!attributes) {
return;
}
//
const uid = (0, shortId_1.shortId)('ev');
//
let needUid = false;
const result = [];
for (const key in attributes) {
const attribute = attributes[key];
//
if (attribute === undefined || attribute === null) {
continue;
}
if (key === '_ssrsxFunctionContext') {
continue;
}
//
if (key === 'href' ||
key === 'src' ||
(tagname === 'form' && key === 'action') ||
key === 'formAction' ||
key === 'icon') {
const baseUrl = ssrsx.baseUrl;
const value = String(attribute);
const href = (value.slice(0, 1) === '/') ? `${baseUrl}${value}` : value;
result.push(`${key}="${href}"`);
continue;
}
//
if (key.slice(0, 2) === 'on') {
if (String(attribute).indexOf('js://') === 0) {
const jsLink = String(attribute).slice(5);
//
const target = uid;
const event = key.slice(2).toLowerCase();
const [f, module] = jsLink.split('@');
ssrsx.events.push({ target, event, module, f: f !== null && f !== void 0 ? f : key });
needUid = true;
//
}
else {
// inline js
result.push(`${key}="${String(attribute)}"`);
}
continue;
}
//
if (key === 'className') {
result.push(`class="${String(attribute)}"`);
continue;
}
if (key === 'htmlFor') {
result.push(`for="${String(attribute)}"`);
continue;
}
if (key === 'style') {
result.push(`style="${typeof attribute === 'string' ?
String(attribute)
:
(typeof attribute === 'object' ?
(0, styleToString_1.styleToString)(attribute)
:
'')}"`);
continue;
}
//
result.push(`${(0, changeCase_1.camel2Kebabu)(key)}="${String(attribute)}"`);
}
if (needUid) {
result.push(`data-ssrsx-event="${uid}"`);
}
if (result.length === 0) {
return '';
}
return ` ${result.join(' ')}`;
};
////////////////////////////////////////////////////////////////////////////////
const appendFcId = (root, fcId) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
if (!root) {
return;
}
if (Array.isArray(root)) {
for (const child of root) {
yield appendFcId(child, fcId);
}
return;
}
if (root === null ||
root === undefined ||
typeof root !== 'object' ||
typeof root === 'string' ||
typeof root === 'number' ||
root instanceof Date) {
return;
}
const ve = yield root;
ve.fcId = (_a = ve.fcId) !== null && _a !== void 0 ? _a : fcId;
//
yield appendFcId((_b = ve.props) === null || _b === void 0 ? void 0 : _b.children, fcId);
yield appendFcId(ve.children, fcId);
//
return;
});
////////////////////////////////////////////////////////////////////////////////
const parseCore = (root, ssrsx) => __awaiter(void 0, void 0, void 0, function* () {
if (!root) {
return '';
}
if (Array.isArray(root)) {
const result = [];
for (const child of root) {
result.push(yield parseCore(child, ssrsx));
}
return result.join('');
}
if (root === null || root === undefined) {
return '';
}
if (typeof root !== 'object') {
return String(root);
}
const ve = yield root;
if (ve.fragment) {
return yield parseCore(ve.children, ssrsx);
}
if (ve.f) {
const _ssrsxFunctionContext = { finalize: () => { } };
const fcId = currentFcId = (0, shortId_1.shortId)('fc');
//
let funcResult = undefined;
try {
funcResult = yield ve.f(Object.assign({ _ssrsxFunctionContext }, ve.props));
}
catch (e) {
(0, log_1.logError)('Functions component error :', e);
}
//
yield appendFcId(funcResult, fcId);
const result = yield parseCore(funcResult, ssrsx);
//
const finalResult = _ssrsxFunctionContext.finalize();
if (finalResult) {
return yield parseCore(finalResult, ssrsx);
}
return result;
}
if (ve.tag) {
const fcId = (['html', 'head', 'meta', 'style', 'script', 'title', 'body'].indexOf(ve.tag) === -1) ?
` data-ssrsx-fcId="${ve.fcId}"`
:
'';
return `<${ve.tag}${parseAttributes(ve.tag, ve.attributes, ssrsx)}${fcId}>${yield parseCore(ve.children, ssrsx)}</${ve.tag}>`;
}
return '';
});
////////////////////////////////////////////////////////////////////////////////
const parse = (root, httpServer, userContext, baseUrl) => __awaiter(void 0, void 0, void 0, function* () {
//
(0, shortId_1.resetShortId)();
//
const ssrsx = {
baseUrl,
context: userContext,
parseContext: {
global: {},
},
server: httpServer,
//
styles: [],
events: [],
};
currentSsrsx = ssrsx;
//
return {
body: yield parseCore(root, ssrsx),
context: ssrsx
};
});
exports.parse = parse;
//# sourceMappingURL=jsx-parser.js.map