UNPKG

@stencil/core

Version:

A Compiler for Web Components and Progressive Web Apps

58 lines (57 loc) 2.42 kB
/* Stencil Client Patch Browser v4.3.0 | MIT Licensed | https://stenciljs.com */ import { BUILD, NAMESPACE } from '@stencil/core/internal/app-data'; import { consoleDevInfo, doc, promiseResolve, H } from '@stencil/core'; const patchBrowser = () => { // NOTE!! This fn cannot use async/await! if (BUILD.isDev && !BUILD.isTesting) { consoleDevInfo('Running in development mode.'); } if (BUILD.cloneNodeFix) { // opted-in to polyfill cloneNode() for slot polyfilled components patchCloneNodeFix(H.prototype); } if (BUILD.profile && !performance.mark) { // not all browsers support performance.mark/measure (Safari 10) // because the mark/measure APIs are designed to write entries to a buffer in the browser that does not exist, // simply stub the implementations out. // TODO(STENCIL-323): Remove this patch when support for older browsers is removed (breaking) // @ts-ignore performance.mark = performance.measure = () => { /*noop*/ }; performance.getEntriesByName = () => []; } // @ts-ignore const scriptElm = BUILD.scriptDataOpts ? Array.from(doc.querySelectorAll('script')).find((s) => new RegExp(`\/${NAMESPACE}(\\.esm)?\\.js($|\\?|#)`).test(s.src) || s.getAttribute('data-stencil-namespace') === NAMESPACE) : null; const importMeta = import.meta.url; const opts = BUILD.scriptDataOpts ? (scriptElm || {})['data-opts'] || {} : {}; if (importMeta !== '') { opts.resourcesUrl = new URL('.', importMeta).href; } return promiseResolve(opts); }; const patchCloneNodeFix = (HTMLElementPrototype) => { const nativeCloneNodeFn = HTMLElementPrototype.cloneNode; HTMLElementPrototype.cloneNode = function (deep) { if (this.nodeName === 'TEMPLATE') { return nativeCloneNodeFn.call(this, deep); } const clonedNode = nativeCloneNodeFn.call(this, false); const srcChildNodes = this.childNodes; if (deep) { for (let i = 0; i < srcChildNodes.length; i++) { // Node.ATTRIBUTE_NODE === 2, and checking because IE11 if (srcChildNodes[i].nodeType !== 2) { clonedNode.appendChild(srcChildNodes[i].cloneNode(true)); } } } return clonedNode; }; }; export { patchBrowser };