-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert CSS Modules to scoped styles (#38)
* Convert CSS Modules to scoped styles * Update README * Move class scoping into HTML walker * Fix SSR styles test * Fix mustache tags * Update PostCSS plugin name * Add JSDoc comment * Update test
- Loading branch information
Showing
9 changed files
with
238 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Plugin } from 'postcss'; | ||
|
||
interface AstroScopedOptions { | ||
className: string; | ||
} | ||
|
||
interface Selector { | ||
start: number; | ||
end: number; | ||
value: string; | ||
} | ||
|
||
const CSS_SEPARATORS = new Set([' ', ',', '+', '>', '~']); | ||
|
||
/** | ||
* Scope Selectors | ||
* Given a selector string (`.btn>span,.nav>span`), add an additional CSS class to every selector (`.btn.myClass>span.myClass,.nav.myClass>span.myClass`) | ||
* @param {string} selector The minified selector string to parse. Cannot contain arbitrary whitespace (other than child selector syntax). | ||
* @param {string} className The CSS class to apply. | ||
*/ | ||
export function scopeSelectors(selector: string, className: string) { | ||
const c = className.replace(/^\.?/, '.'); // make sure class always has leading '.' | ||
const selectors: Selector[] = []; | ||
let ss = selector; // final output | ||
|
||
// Pass 1: parse selector string; extract top-level selectors | ||
let start = 0; | ||
let lastValue = ''; | ||
for (let n = 0; n < ss.length; n++) { | ||
const isEnd = n === selector.length - 1; | ||
if (isEnd || CSS_SEPARATORS.has(selector[n])) { | ||
lastValue = selector.substring(start, isEnd ? undefined : n); | ||
if (!lastValue) continue; | ||
selectors.push({ start, end: isEnd ? n + 1 : n, value: lastValue }); | ||
start = n + 1; | ||
} | ||
} | ||
|
||
// Pass 2: starting from end, transform selectors w/ scoped class | ||
for (let i = selectors.length - 1; i >= 0; i--) { | ||
const { start, end, value } = selectors[i]; | ||
const head = ss.substring(0, start); | ||
const tail = ss.substring(end); | ||
|
||
// replace '*' with className | ||
if (value === '*') { | ||
ss = head + c + tail; | ||
continue; | ||
} | ||
|
||
// leave :global() alone! | ||
if (value.startsWith(':global(')) { | ||
ss = head + ss.substring(start, end).replace(':global(', '').replace(')', '') + tail; | ||
continue; | ||
} | ||
|
||
// scope everything else | ||
let newSelector = ss.substring(start, end); | ||
const pseudoIndex = newSelector.indexOf(':'); | ||
if (pseudoIndex > 0) { | ||
// if there‘s a pseudoclass (:focus) | ||
ss = head + newSelector.substring(start, pseudoIndex) + c + newSelector.substr(pseudoIndex) + tail; | ||
} else { | ||
ss = head + newSelector + c + tail; | ||
} | ||
} | ||
|
||
return ss; | ||
} | ||
|
||
/** PostCSS Scope plugin */ | ||
export default function astroScopedStyles(options: AstroScopedOptions): Plugin { | ||
return { | ||
postcssPlugin: '@astro/postcss-scoped-styles', | ||
Rule(rule) { | ||
rule.selector = scopeSelectors(rule.selector, options.className); | ||
}, | ||
}; | ||
} |
Oops, something went wrong.