Skip to content

Instantly share code, notes, and snippets.

@sunnyone
Created October 13, 2023 06:57
Show Gist options
  • Save sunnyone/0680244db6b4544a619285214e68988f to your computer and use it in GitHub Desktop.
Save sunnyone/0680244db6b4544a619285214e68988f to your computer and use it in GitHub Desktop.
#!/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