Created
June 11, 2019 07:08
-
-
Save Richienb/17a3dbc62c64c9efdb1ac2e0ad3301d2 to your computer and use it in GitHub Desktop.
Auto SASS Compile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (() => { | |
| const sass = new Sass(); | |
| const handleFiles = () => { | |
| Array.from(document.querySelectorAll(`style[type="text/sass"], style[type="text/scss"]`)).map(val => { | |
| sass.compile(val.innerHTML, { | |
| indentedSyntax: val.getAttribute("type") === "text/sass" | |
| }, ({ | |
| text | |
| }) => val.innerHTML = text); | |
| val.removeAttribute("type") | |
| }) | |
| Array.from(document.querySelectorAll(`link[rel="stylesheet"][href][type="text/sass"], link[rel="stylesheet"][href][type="text/scss"]`)).map(val => fetch(val.getAttribute("href")) | |
| .then(res => res.text()) | |
| .then(data => sass.compile(data, { | |
| indentedSyntax: val.getAttribute("type") === "text/sass" | |
| }, ({ | |
| text | |
| }) => { | |
| const el = val.parentNode | |
| const newEl = document.createElement('style'); | |
| newEl.innerHTML = text | |
| el.parentNode.replaceChild(newEl, el); | |
| }))) | |
| } | |
| handleFiles() | |
| new MutationObserver((mutationList, observer) => { | |
| mutationList.forEach((mutation) => { | |
| if (["style", "link"].indexOf(mutation.target.localName) >= 0 && ["text/sass", "text/scss"].indexOf(mutation.target.getAttribute("type")) >= 0) handleFiles() | |
| }); | |
| }).observe(document, { | |
| childList: true, | |
| attributes: true, | |
| subtree: true | |
| }) | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment