Created
October 13, 2023 06:57
-
-
Save sunnyone/0680244db6b4544a619285214e68988f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
# Replace linaria styled`` to vanilla-extract style({}) | |
text = DATA.read | |
text.gsub!(%r|const (\w+) = styled.*?`(.*?)`|m) do | |
name = $1 | |
css = $2 | |
list = css.split(/;/).map do |s| | |
pair = s.split(/:/) | |
pair[0].gsub!(%r|-([a-z])|) { $1.upcase } | |
pair[1].gsub!(%r|^(\s*)(.*?)(\s*)$|, '\1"\2"\3') if pair[1] | |
pair.join(":") | |
end | |
name = name.gsub(/^Styled/, '') | |
new_name = name[0].downcase + name[1..-1] | |
"export const #{new_name}Style = style({" + list.join(",") + "})" | |
end | |
puts %q(import {style} from "@vanilla-extract/css";) | |
puts text | |
__END__ | |
const FooDiv = styled.div` | |
display: flex; | |
flex-direction: column; | |
`; | |
const BarSpan = styled.span` | |
color: red; | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment