-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Convert CSS Modules to scoped styles #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b62c446
Convert CSS Modules to scoped styles
drwpow d67f0d4
Update README
drwpow dcc241a
Move class scoping into HTML walker
drwpow db7f6a7
Fix SSR styles test
drwpow 69ef958
Fix mustache tags
drwpow 9dada0c
Update PostCSS plugin name
drwpow 57bb90c
Add JSDoc comment
drwpow 5e529ac
Update test
drwpow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { Plugin } from 'postcss'; | ||
|
|
||
| interface AstroScopedOptions { | ||
| className: string; | ||
| } | ||
|
|
||
| interface Selector { | ||
| start: number; | ||
| end: number; | ||
| value: string; | ||
| } | ||
|
|
||
| const CSS_SEPARATORS = new Set([' ', ',', '+', '>', '~']); | ||
|
|
||
| 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 { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could maybe be its own PostCSS plugin eventually? But local for now. |
||
| return { | ||
| postcssPlugin: 'Astro Scoped Styles', | ||
|
drwpow marked this conversation as resolved.
Outdated
|
||
| Rule(rule) { | ||
| rule.selector = scopeSelectors(rule.selector, options.className); | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.