UNPKG

2.85 kBSource Map (JSON)View Raw
1{
2 "version": 3,
3 "sources": ["../../src/shim/index.ts"],
4 "sourcesContent": ["/**\n * [[include:src/shim/README.md]]\n *\n * @packageDocumentation\n * @module twind/shim\n */\n\nimport type { Configuration } from 'twind'\n\nimport { setup as setupTW, createObserver } from 'twind/observe'\n\nexport * from 'twind'\n\n/**\n * Options for {@link setup}.\n */\nexport interface ShimConfiguration extends Configuration {\n /**\n * The root element to shim (default: `document.documentElement`).\n */\n target?: HTMLElement\n}\n\nif (typeof document !== 'undefined' && typeof addEventListener == 'function') {\n // eslint-disable-next-line no-var\n var onload = () => {\n const script = document.querySelector('script[type=\"twind-config\"]')\n\n setup(script ? JSON.parse(script.innerHTML) : {})\n }\n\n if (document.readyState === 'loading') {\n // Loading hasn't finished yet\n addEventListener('DOMContentLoaded', onload)\n } else {\n // `DOMContentLoaded` has already fired\n // invoke on next tick to allow other setup methods to run\n // eslint-disable-next-line no-var\n var timeoutRef = setTimeout(onload)\n }\n}\n\nconst observer = createObserver()\n\n/**\n * Stop shimming/observing all nodes.\n */\nexport const disconnect = (): void => {\n if (onload) {\n // Removing the callbacks ensures that the setup is called only once\n // either programmatically from userland or by DOMContentLoaded/setTimeout\n removeEventListener('DOMContentLoaded', onload)\n clearTimeout(timeoutRef)\n }\n\n observer.disconnect()\n}\n\n/**\n * Configure the default {@link tw} and starts {@link observe | observing} the\n * {@link ShimConfiguration.target | target element} (default: `document.documentElement`).\n *\n * You do not need to call this method. As an alternativ you can provide a\n * `<script type=\"twind-config\">...</script>` element within the document.\n * The content must be valid JSON and all {@link twind.setup | twind setup options}\n * (including hash) are supported.\n */\nexport const setup = ({\n target = document.documentElement,\n ...config\n}: ShimConfiguration = {}): void => {\n if (Object.keys(config).length) {\n setupTW(config)\n }\n\n // Remove event listeners\n disconnect()\n\n observer.observe(target)\n\n target.hidden = false\n}\n"],
5 "mappings": ";AASA;AAEA;AAYA,IAAI,OAAO,aAAa,eAAe,OAAO,oBAAoB,YAAY;AAExE,WAAS,MAAM;AACjB,UAAM,SAAS,SAAS,cAAc;AAEtC,UAAM,SAAS,KAAK,MAAM,OAAO,aAAa;AAAA;AAGhD,MAAI,SAAS,eAAe,WAAW;AAErC,qBAAiB,oBAAoB;AAAA,SAChC;AAID,iBAAa,WAAW;AAAA;AAAA;AAb1B;AAaE;AAIR,IAAM,WAAW;AAKV,IAAM,aAAa,MAAY;AACpC,MAAI,QAAQ;AAGV,wBAAoB,oBAAoB;AACxC,iBAAa;AAAA;AAGf,WAAS;AAAA;AAYJ,IAAM,QAAQ,CAAC;AAAA,EACpB,SAAS,SAAS;AAAA,KACf;AAAA,IACkB,OAAa;AAClC,MAAI,OAAO,KAAK,QAAQ,QAAQ;AAC9B,YAAQ;AAAA;AAIV;AAEA,WAAS,QAAQ;AAEjB,SAAO,SAAS;AAAA;",
6 "names": []
7}