-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement css codemods for vue sfc files using postcss #156
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
Open
Sidnioulz
wants to merge
1
commit into
main
Choose a base branch
from
feat/css-codemods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <template> | ||
| <div class="card"> | ||
| <h2 class="card-title">Card Title</h2> | ||
| <p class="card-text">Card content goes here</p> | ||
| <button class="card-button">Action</button> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped>:root { | ||
| --color-text-primary: #333333; | ||
| --color-text-secondary: #666666; | ||
| --color-primary: #007bff; | ||
| --color-primary-dark: #0056b3; | ||
| --color-background: #ffffff; | ||
| --color-border: #e0e0e0; | ||
| --color-disabled: #cccccc; | ||
| --color-background: white; | ||
| --shadow-light: rgba(0, 0, 0, 0.1); | ||
| } | ||
|
|
||
| .card { | ||
| background: var(--color-background); | ||
| border: 1px solid #e0e0e0; | ||
| border-radius: 8px; | ||
| padding: 20px; | ||
| box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
| } | ||
|
|
||
| .card-title { | ||
| color: var(--color-text-primary); | ||
| font-size: 1.25rem; | ||
| margin: 0 0 12px 0; | ||
| } | ||
|
|
||
| .card-text { | ||
| color: var(--color-text-secondary); | ||
| line-height: 1.5; | ||
| margin: 0 0 16px 0; | ||
| } | ||
|
|
||
| .card-button { | ||
| background: var(--color-primary); | ||
| color: var(--color-background); | ||
| border: none; | ||
| padding: 8px 16px; | ||
| border-radius: 4px; | ||
| cursor: pointer; | ||
| transition: background-color 0.2s; | ||
| } | ||
|
|
||
| .card-button:hover { | ||
| background: var(--color-primary-dark); | ||
| } | ||
|
|
||
| .card-button:disabled { | ||
| background: var(--color-disabled); | ||
| cursor: not-allowed; | ||
| } | ||
| </style> |
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,52 @@ | ||
| module.exports = { | ||
| style: function convertToColorVariables(fileInfo, { root, postcss }) { | ||
| // Define color mappings - map actual colors to their variable names | ||
| const colorMap = { | ||
| '#333333': '--color-text-primary', | ||
| '#666666': '--color-text-secondary', | ||
| '#007bff': '--color-primary', | ||
| '#0056b3': '--color-primary-dark', | ||
| '#ffffff': '--color-background', | ||
| '#e0e0e0': '--color-border', | ||
| '#cccccc': '--color-disabled', | ||
| white: '--color-background', | ||
| 'rgba(0, 0, 0, 0.1)': '--shadow-light', | ||
| } | ||
|
|
||
| // Only add variables if they don't already exist | ||
| let rootRule = null | ||
| root.walkRules(':root', (rule) => { | ||
| rootRule = rule | ||
| }) | ||
|
|
||
| if (!rootRule) { | ||
| rootRule = new postcss.Rule({ selector: ':root' }) | ||
|
|
||
| // Create CSS custom properties with actual color values | ||
| Object.entries(colorMap).forEach(([actualColor, varName]) => { | ||
| const decl = new postcss.Declaration({ | ||
| prop: varName, | ||
| value: actualColor, | ||
| }) | ||
| rootRule.append(decl) | ||
| }) | ||
|
|
||
| // Insert the :root rule at the beginning | ||
| root.prepend(rootRule) | ||
| } | ||
|
|
||
| // Replace color values with CSS variable references (but not in the :root rule) | ||
| root.walkDecls((decl) => { | ||
| // Skip declarations inside :root rule to avoid circular references | ||
| if (decl.parent.selector === ':root') { | ||
| return | ||
| } | ||
|
|
||
| Object.entries(colorMap).forEach(([actualColor, varName]) => { | ||
| if (decl.value === actualColor) { | ||
| decl.value = `var(${varName})` | ||
| } | ||
| }) | ||
| }) | ||
| }, | ||
| } |
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,39 @@ | ||
| <template> | ||
| <div class="container"> | ||
| <h1 class="title">Hello World</h1> | ||
| <p class="text">This is a paragraph</p> | ||
| <button class="btn">Click me</button> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped> | ||
| .my-container { | ||
| max-width: 800px; | ||
| margin: 0 auto; | ||
| padding: 20px; | ||
| } | ||
|
|
||
| .my-title { | ||
| font-size: 2rem; | ||
| color: #333; | ||
| margin-bottom: 1rem; | ||
| } | ||
|
|
||
| .my-text { | ||
| line-height: 1.6; | ||
| color: #666; | ||
| } | ||
|
|
||
| .my-btn { | ||
| background: #007bff; | ||
| color: white; | ||
| border: none; | ||
| padding: 10px 20px; | ||
| border-radius: 4px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .my-btn:hover { | ||
| background: #0056b3; | ||
| } | ||
| </style> |
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,16 @@ | ||
| module.exports = { | ||
| style: function prefixClasses(fileInfo, { root }, options) { | ||
| const prefix = options?.prefix || 'app-' | ||
|
|
||
| // Transform class selectors | ||
| root.walkRules((rule) => { | ||
| rule.selector = rule.selector.replace(/\.([a-zA-Z][\w-]*)/g, (match, className) => { | ||
| // Don't prefix if it already has the prefix | ||
| if (className.startsWith(prefix.replace('-', ''))) { | ||
| return match | ||
| } | ||
| return `.${prefix}${className}` | ||
| }) | ||
| }) | ||
| }, | ||
| } | ||
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,38 @@ | ||
| <template> | ||
| <div class="wrapper"> | ||
| <div class="content"> | ||
| <slot /> | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped> | ||
| .wrapper { | ||
| border: 1px solid #ddd; | ||
| padding: 20px; | ||
| } | ||
|
|
||
| .wrapper::v-deep .child-component { | ||
| margin: 10px 0; | ||
| } | ||
|
|
||
| .wrapper::v-deep .child-component h2 { | ||
| color: #333; | ||
| font-size: 1.5rem; | ||
| } | ||
|
|
||
| .wrapper::v-slotted p { | ||
| color: #666; | ||
| line-height: 1.6; | ||
| } | ||
|
|
||
| .content::v-deep .nested .item { | ||
| background: #f5f5f5; | ||
| padding: 8px; | ||
| } | ||
|
|
||
| /* Mix of old and new syntax */ | ||
| .wrapper::v-deep .legacy { | ||
| border: 1px solid red; | ||
| } | ||
| </style> |
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,15 @@ | ||
| module.exports = { | ||
| style: function transformVueSelectors(fileInfo, { root }) { | ||
| // Transform Vue 3 deep and slotted selectors to Vue 2 syntax | ||
| root.walkRules((rule) => { | ||
| rule.selector = rule.selector | ||
| // Convert :deep() to ::v-deep | ||
| .replace(/:deep\(([^)]+)\)/g, '::v-deep $1') | ||
| // Convert :slotted() to ::v-slotted | ||
| .replace(/:slotted\(([^)]+)\)/g, '::v-slotted $1') | ||
| // Normalize existing ::v-deep syntax | ||
| .replace(/::v-deep\s*\(/g, '::v-deep ') | ||
| .replace(/\)$/, '') | ||
| }) | ||
| }, | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Avoid calling
prefix.replace('-', '')inside the loop on every rule. Compute the cleaned prefix once outside ofroot.walkRulesfor better clarity and slight performance gain.