UNPKG

nitropage

Version:

A free and open source, extensible visual page builder based on SolidStart.

63 lines (54 loc) 1.71 kB
import { Plugin } from "vite"; import MagicString from "magic-string"; /** * Inspired by: * https://github.com/jonschlinkert/is-plain-object/blob/master/is-plain-object.js * * Original license: * Copyright (c) 2014-2017, Jon Schlinkert. * Released under the MIT License. */ const isPlainObjectTmpl = ` function isPlainObject(obj) { if (obj.constructor === undefined) return true; const proto = obj.constructor.prototype; if (typeof proto !== "object") return false; if (proto.hasOwnProperty('isPrototypeOf') === false) return false; return true; };`; /** * Patches Solids isWrappable to work across realms * Solid v2 plans to support objects across realms * We "backport" this new logic because it simplifies, * synchronizing state across iframes a ton */ export const solidRealmsPatchPlugin = () => { const regex = new RegExp("(solid-js/store/dist/store)|(solid-js_store)\\.js"); return { name: "solid-realms-patch", transform(code, id, options) { if (options?.ssr) return; if (!id.match(regex)) return; const s = new MagicString(code); s.replace("proto === Object.prototype", "isPlainObject(obj)"); if (!s.hasChanged) { console.error("Failed to patch Solid isWrappable!"); return; } s.replace( "function isWrappable", `${isPlainObjectTmpl}\nfunction isWrappable`, ); const newCode = s.toString(); if (!newCode.includes("function isPlainObject(obj)")) { console.error("Failed to patch Solid isWrappable!"); return; } const result = { code: newCode, map: s.generateMap(), }; return result; }, } as Plugin; };