@web-widget/web-widget
Version:
Web front-end application container
93 lines (91 loc) • 2.9 kB
JavaScript
import {
getClientModuleId,
unsafePropsToAttrs
} from "./chunk-L7YKSBOA.js";
import {
HTMLWebWidgetElement,
INNER_HTML_PLACEHOLDER,
queueMicrotask
} from "./chunk-TIHYKKQL.js";
import {
__objRest,
__privateAdd,
__privateGet,
__privateSet,
__spreadProps,
__spreadValues
} from "./chunk-EAUID5A7.js";
// src/install.ts
import { mountLifecycleCacheLayer } from "@web-widget/lifecycle-cache/client";
function install() {
Object.assign(window, {
HTMLWebWidgetElement
});
customElements.define("web-widget", HTMLWebWidgetElement);
}
mountLifecycleCacheLayer(() => {
queueMicrotask(install);
});
// src/client.ts
function unsafeAttrsToHtml(attrs) {
return Object.entries(attrs).map(
([attrName, attrValue]) => `${attrName}${attrValue === "" ? "" : '="' + attrValue + '"'}`
).join(" ");
}
var _clientImport, _options;
var ClientWebWidgetRenderer = class {
constructor(loader, _a) {
__privateAdd(this, _clientImport);
__privateAdd(this, _options);
this.localName = "web-widget";
var _b = _a, { children = "", renderStage } = _b, options = __objRest(_b, ["children", "renderStage"]);
if (children && options.renderTarget !== "shadow") {
throw new Error(
`Rendering content in a slot requires "renderTarget: 'shadow'".`
);
}
if (renderStage === "server") {
throw new Error(
`"renderStage: 'server'" usually comes from server-side rendering, it doesn't make sense to enable it on the client side.`
);
}
__privateSet(this, _clientImport, getClientModuleId(loader, options));
__privateSet(this, _options, options);
}
get attributes() {
const clientImport = __privateGet(this, _clientImport);
const _a = __privateGet(this, _options), { data: contextdata, meta: contextmeta } = _a, options = __objRest(_a, ["data", "meta"]);
const attrs = unsafePropsToAttrs(__spreadProps(__spreadValues({}, options), {
// base: options.base?.startsWith("file://") ? undefined : options.base,
contextdata: JSON.stringify(contextdata),
contextmeta: JSON.stringify(contextmeta),
import: clientImport,
recovering: false
}));
if (attrs.contextdata === "{}") {
delete attrs.contextdata;
}
if (attrs.contextmeta === "{}") {
delete attrs.contextmeta;
}
return attrs;
}
async renderInnerHTMLToString() {
return INNER_HTML_PLACEHOLDER;
}
async renderOuterHTMLToString() {
const tag = this.localName;
const attributes = this.attributes;
const children = await this.renderInnerHTMLToString();
return `<${tag} ${unsafeAttrsToHtml(attributes)}>${children}</${tag}>`;
}
};
_clientImport = new WeakMap();
_options = new WeakMap();
var WebWidgetRenderer = ClientWebWidgetRenderer;
export {
HTMLWebWidgetElement,
INNER_HTML_PLACEHOLDER,
WebWidgetRenderer
};
//# sourceMappingURL=web-widget.client.js.map