patch-browser.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Stencil Client Patch Browser v4.20.0 | MIT Licensed | https://stenciljs.com
  3. */
  4. // src/client/client-patch-browser.ts
  5. import { BUILD, NAMESPACE } from "@stencil/core/internal/app-data";
  6. import { consoleDevInfo, doc, H, promiseResolve } from "@stencil/core";
  7. var patchBrowser = () => {
  8. if (BUILD.isDev && !BUILD.isTesting) {
  9. consoleDevInfo("Running in development mode.");
  10. }
  11. if (BUILD.cloneNodeFix) {
  12. patchCloneNodeFix(H.prototype);
  13. }
  14. const scriptElm = BUILD.scriptDataOpts ? Array.from(doc.querySelectorAll("script")).find(
  15. (s) => new RegExp(`/${NAMESPACE}(\\.esm)?\\.js($|\\?|#)`).test(s.src) || s.getAttribute("data-stencil-namespace") === NAMESPACE
  16. ) : null;
  17. const importMeta = import.meta.url;
  18. const opts = BUILD.scriptDataOpts ? (scriptElm || {})["data-opts"] || {} : {};
  19. if (importMeta !== "") {
  20. opts.resourcesUrl = new URL(".", importMeta).href;
  21. }
  22. return promiseResolve(opts);
  23. };
  24. var patchCloneNodeFix = (HTMLElementPrototype) => {
  25. const nativeCloneNodeFn = HTMLElementPrototype.cloneNode;
  26. HTMLElementPrototype.cloneNode = function(deep) {
  27. if (this.nodeName === "TEMPLATE") {
  28. return nativeCloneNodeFn.call(this, deep);
  29. }
  30. const clonedNode = nativeCloneNodeFn.call(this, false);
  31. const srcChildNodes = this.childNodes;
  32. if (deep) {
  33. for (let i = 0; i < srcChildNodes.length; i++) {
  34. if (srcChildNodes[i].nodeType !== 2) {
  35. clonedNode.appendChild(srcChildNodes[i].cloneNode(true));
  36. }
  37. }
  38. }
  39. return clonedNode;
  40. };
  41. };
  42. export {
  43. patchBrowser
  44. };