@stencil/core
Version:
A Compiler for Web Components and Progressive Web Apps
46 lines (45 loc) • 1.85 kB
JavaScript
/*
Stencil Client Patch Browser v4.8.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);
}
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 };