Created
January 2, 2020 05:37
-
-
Save tomhodgins/7ce1cd31ceb329d108c944180461f5bf to your computer and use it in GitHub Desktop.
Exploring one way CSS stylesheet authors can express that certain styles conditionally depend on other CSS stylesheets or JS files being present for them to work. Online demo: http://staticresource.com/cdr.html
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>CSS Dependency Resolver Demo</title> | |
<h1>CSS Dependency Resolver</h1> | |
<style> | |
@supports (--dependencies(url(https://csspl.us/cursory.js))) { | |
* { | |
cursor: none; | |
} | |
:root::before { | |
content: ''; | |
display: block; | |
width: 30px; | |
height: 30px; | |
border-radius: 0% 100% 25% 75% / 0% 75% 25% 100%; | |
position: fixed; | |
background: black; | |
top: calc(var(--cursorY) * 1px); | |
left: calc(var(--cursorX) * 1px); | |
pointer-events: none; | |
} | |
} | |
</style> | |
<script type=module src=css-dependency-resolver.js></script> |
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
// CSS Dependency Resolver | |
// @supports --dependencies(url() url('') url("")) {} | |
// @supports (--dependencies(url() url('') url(""))) {} | |
import {process, all, add} from 'https://unpkg.com/cssomtools' | |
import * as parseCSS from 'https://tomhodgins.github.io/parse-css/index.js' | |
process(all(), cssomRule => { | |
const parsedRule = parseCSS.parseARule(cssomRule.cssText) | |
const prelude = parseCSS.parseAListOfComponentValues(cssomRule.conditionText) | |
const urlFilter = list => list.filter(({tokenType, type}) => tokenType === 'URL' || type === 'FUNCTION') | |
// @supports () {} | |
const supportsAtRule = parsedRule.type === 'AT-RULE' | |
&& parsedRule.name === 'supports' | |
&& prelude | |
// --dependencies() | |
const plainFunction = prelude[0] | |
&& prelude[0].type === 'FUNCTION' | |
&& prelude[0].name === '--dependencies' | |
&& prelude[0].value | |
&& urlFilter(prelude[0].value).length | |
// (--dependencies()) | |
const blockWrappedFunction = prelude[0] | |
&& prelude[0].value | |
&& prelude[0].value[0] | |
&& prelude[0].value[0].type === 'FUNCTION' | |
&& prelude[0].value[0].name === '--dependencies' | |
&& prelude[0].value[0].value | |
&& urlFilter(prelude[0].value[0].value).length | |
if ( | |
supportsAtRule | |
&& (plainFunction || blockWrappedFunction) | |
) { | |
const parent = cssomRule.parentRule | |
? cssomRule.parentRule | |
: cssomRule.parentStyleSheet | |
const tokens = plainFunction | |
? prelude[0].value | |
: prelude[0].value[0].value | |
const urls = urlFilter(tokens).map(token => | |
Array.isArray(token.value) | |
? token.value[0].value | |
: token.value | |
) | |
urls.forEach(async url => { | |
const extension = new URL(url).pathname.match(/\.\w+$/) | |
? new URL(url).pathname.match(/\.\w+$/)[0] | |
: '.css' | |
// Fetch and inline CSS | |
if (extension === '.css') { | |
let link = document.querySelector(`link[rel="stylesheet"][href*="${url}"]`) | |
if (link === null) { | |
link = document.createElement('link') | |
link.rel = 'stylesheet' | |
link.href = url | |
document.documentElement.prepend(link) | |
} | |
} | |
// Add any JS dependencies not found | |
if (extension === '.js') { | |
let script = document.querySelector(`script[src*="${url}"]`) | |
if (script === null) { | |
script = document.createElement('script') | |
script.src = url | |
script.type = 'module' | |
document.documentElement.append(script) | |
} | |
} | |
}) | |
// Add child CSS rules after dependencies have been added | |
add( | |
Array.from(cssomRule.cssRules), | |
parent, | |
Array.from(parent.cssRules).indexOf(cssomRule) | |
) | |
// Remove original @supports rule | |
cssomRule.parentStyleSheet.deleteRule( | |
Array.from(parent.cssRules).indexOf(cssomRule) | |
) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment