import { BUILD } from '@app-data'; import { addHostEventListeners, consoleError, forceUpdate, getHostRef, registerHost, styles, supportsShadow, transformTag, } from '@platform'; import type * as d from '../declarations'; import { CMP_FLAGS } from '../utils/constants'; import { createShadowRoot } from '../utils/shadow-root'; import { connectedCallback } from './connected-callback'; import { disconnectedCallback } from './disconnected-callback'; import { patchChildSlotNodes, patchCloneNode, patchPseudoShadowDom, patchSlotAppendChild, patchTextContent, } from './dom-extras'; import { computeMode } from './mode'; import { normalizeWatchers } from './normalize-watchers'; import { proxyComponent } from './proxy-component'; import { PROXY_FLAGS } from './runtime-constants'; import { attachStyles, getScopeId, hydrateScopedToShadow, registerStyle } from './styles'; export const defineCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMetaCompact) => { customElements.define( transformTag(compactMeta[1]), proxyCustomElement(Cstr, compactMeta) as CustomElementConstructor, ); }; export const proxyCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMetaCompact) => { // Set the app start mark on first component (for consistent profiling with lazy build) if (BUILD.profile && performance.mark && performance.getEntriesByName('st:app:start', 'mark').length === 0) { performance.mark('st:app:start'); } const cmpMeta: d.ComponentRuntimeMeta = { $flags$: compactMeta[0], $tagName$: compactMeta[1], }; try { if (BUILD.member) { cmpMeta.$members$ = compactMeta[2]; } if (BUILD.hostListener) { cmpMeta.$listeners$ = compactMeta[3]; } if (BUILD.propChangeCallback) { cmpMeta.$watchers$ = normalizeWatchers(Cstr.$watchers$); cmpMeta.$deserializers$ = Cstr.$deserializers$; cmpMeta.$serializers$ = Cstr.$serializers$; } if (BUILD.reflect) { cmpMeta.$attrsToReflect$ = []; } if (BUILD.shadowDom && !supportsShadow && cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) { // TODO(STENCIL-854): Remove code related to legacy shadowDomShim field cmpMeta.$flags$ |= CMP_FLAGS.needsShadowDomShim; } if (!(cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) && cmpMeta.$flags$ & CMP_FLAGS.hasSlot) { if (BUILD.experimentalSlotFixes) { patchPseudoShadowDom(Cstr.prototype); } else { if (BUILD.slotChildNodesFix) { patchChildSlotNodes(Cstr.prototype); } if (BUILD.cloneNodeFix) { patchCloneNode(Cstr.prototype); } if (BUILD.appendChildSlotFix) { patchSlotAppendChild(Cstr.prototype); } if (BUILD.scopedSlotTextContentFix && cmpMeta.$flags$ & CMP_FLAGS.scopedCssEncapsulation) { patchTextContent(Cstr.prototype); } } } else if (BUILD.cloneNodeFix) { patchCloneNode(Cstr.prototype); } if (BUILD.hydrateClientSide && BUILD.shadowDom) { hydrateScopedToShadow([cmpMeta]); } const originalConnectedCallback = Cstr.prototype.connectedCallback; const originalDisconnectedCallback = Cstr.prototype.disconnectedCallback; Object.assign(Cstr.prototype, { __hasHostListenerAttached: false, __registerHost() { registerHost(this, cmpMeta); }, connectedCallback() { if (!this.__hasHostListenerAttached) { const hostRef = getHostRef(this); if (!hostRef) { return; } addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false); this.__hasHostListenerAttached = true; } connectedCallback(this); if (originalConnectedCallback) { originalConnectedCallback.call(this); } }, disconnectedCallback() { disconnectedCallback(this); if (originalDisconnectedCallback) { originalDisconnectedCallback.call(this); } }, __attachShadow() { if (supportsShadow) { if (!this.shadowRoot) { createShadowRoot.call(this, cmpMeta); } else { // we want to check to make sure that the mode for the shadow // root already attached to the element (i.e. created via DSD) // is set to 'open' since that's the only mode we support if (this.shadowRoot.mode !== 'open') { throw new Error( `Unable to re-use existing shadow root for ${cmpMeta.$tagName$}! Mode is set to ${this.shadowRoot.mode} but Stencil only supports open shadow roots.`, ); } } } else { (this as any).shadowRoot = this; } }, }); Object.defineProperty(Cstr, 'is', { value: cmpMeta.$tagName$, configurable: true, }); return proxyComponent(Cstr, cmpMeta, PROXY_FLAGS.isElementConstructor | PROXY_FLAGS.proxyState); } catch (e) { consoleError(e); return Cstr; } }; export const forceModeUpdate = (elm: d.RenderNode) => { if (BUILD.style && BUILD.mode && !BUILD.lazyLoad) { const mode = computeMode(elm); const hostRef = getHostRef(elm); if (hostRef && hostRef.$modeName$ !== mode) { const cmpMeta = hostRef.$cmpMeta$; const oldScopeId = elm['s-sc']; const scopeId = getScopeId(cmpMeta, mode); const style = (elm.constructor as any).style[mode]; const flags = cmpMeta.$flags$; if (style) { if (!styles.has(scopeId)) { registerStyle(scopeId, style, !!(flags & CMP_FLAGS.shadowDomEncapsulation)); } hostRef.$modeName$ = mode; elm.classList.remove(oldScopeId + '-h', oldScopeId + '-s'); attachStyles(hostRef); forceUpdate(elm); } } } };