fluxel
Version:
An ultra-lightweight, high-performance library for efficient DOM building and dynamic web UIs
111 lines (110 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return renderToString;
}
});
const _jsdom = require("jsdom");
const _jsxEnsureNode = /*#__PURE__*/ _interop_require_default(require("../jsxEnsureNode.js"));
const _index = /*#__PURE__*/ _interop_require_default(require("../index.js"));
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function renderToString(renderer, options = {}) {
const metadata = options === null || options === void 0 ? void 0 : options.metadata;
const reactive = !!(options === null || options === void 0 ? void 0 : options.reactive);
const query = (options === null || options === void 0 ? void 0 : options.search) || "";
const hash = (options === null || options === void 0 ? void 0 : options.hash) || "";
const url = `http://localhost.dummy${(options === null || options === void 0 ? void 0 : options.pathname) || "/"}${query ? `?${query}` : ""}${hash ? `#${hash}` : ""}`;
const jsdom = new _jsdom.JSDOM("", {
url
});
const renderingContext = {
createElementCount: 0
};
const document = new Proxy(jsdom.window.document, {
get (target, p) {
if (p === "createElement") {
return (tagName, props)=>{
const element = target.createElement(tagName, props);
if (reactive) {
element.dataset["fluxelEid"] = (renderingContext.createElementCount++).toString();
}
return element;
};
}
return target[p];
}
});
Object.defineProperties(global, {
Node: {
get: ()=>jsdom.window.Node,
configurable: true,
enumerable: false
},
HTMLElement: {
get: ()=>jsdom.window.HTMLElement,
configurable: true,
enumerable: false
},
document: {
get: ()=>document,
configurable: true,
enumerable: false
},
location: {
get: ()=>jsdom.window.location,
configurable: true,
enumerable: false
}
});
const fsc = _index.default.forwardStyleCache;
_index.default.forwardStyleCache = [];
function nodeToString(node) {
if ("dataset" in node && node.dataset instanceof jsdom.window.DOMStringMap) {
node.dataset.fluxelSsr = "true";
node.dataset.fluxelMetadata = metadata || "";
if (reactive) {
node.dataset.fluxelECount = renderingContext.createElementCount.toString();
}
}
const container = jsdom.window.document.createElement("div");
container.appendChild(node);
return container.innerHTML;
}
const node = (0, _jsxEnsureNode.default)(renderer());
const style = _index.default.forwardStyleCache.join("\n");
Object.defineProperties(global, {
Node: {
value: null,
configurable: true,
enumerable: false
},
HTMLElement: {
value: null,
configurable: true,
enumerable: false
},
document: {
value: null,
configurable: true,
enumerable: false
},
location: {
value: null,
configurable: true,
enumerable: false
}
});
_index.default.forwardStyleCache = fsc;
return {
dom: nodeToString(node),
style
};
}