-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat: inline const variables in CSS #178
Conversation
awesome change! you'll want to merge master back in from the postcss stuff (and you have debugger statements in your tests - i think they made CI explode hehe) |
329cd1d
to
4360cf5
Compare
@@ -21,6 +21,8 @@ export const getIdentifierText = ( | |||
return ((node as ts.Identifier).escapedText as string) || (node as ts.Identifier).text; | |||
}; | |||
|
|||
export const getDeclarationValue = (node: ts.VariableDeclaration) => node.initializer?.getText(); |
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.
if we want it without the quotes we can do (code wont be 100% correct):
export const getDeclarationValue = (node: ts.VariableDeclaration) => {
if (ts.isLiteral(node.initializer) {
return node.initializer.text;
}
throw createNodeError(node, 'should have been a literal node');
};
we can then remove the remove quotes func.
fyi totally recommend if you havent already to use https://ts-ast-viewer.com - helps sooo much
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.
Thanks so much, I spent way too much time understanding the nodes and types.
cssVariableExpression = joinToBinaryExpression( | ||
ts.createStringLiteral(before.variablePrefix), | ||
span.expression | ||
if (isConst(value)) { |
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.
we might want to move this into the isStringLiteral/isNumbericLiteral if statement - else this might be inlining object
and function
stuff?
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.
got it!
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.
This reduced the large diff too, which was coming cuz of all the nesting of if-else!
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.
left some comments :) awesome first pass |
ea89324
to
259385f
Compare
@Madou this is ready for review now. :) |
@@ -332,8 +332,8 @@ describe('class names transformer', () => { | |||
); | |||
`); | |||
|
|||
expect(actual).toInclude('style={{ "--var-test-fontsize20": fontSize + "px" }}'); | |||
expect(actual).toInclude('.css-test{font-size:var(--var-test-fontsize20)}'); | |||
expect(actual).toInclude('<div style={{}} className={"css-test"}>hello, world!</div>'); |
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.
can you update this test to use a dnamic value? maybe throw in a hook use
const [fontSize] = useState(12);
'<div style={{ "--var-test-color": color }} className={"css-test"}>hello, world!</div>' | ||
); | ||
expect(actual).toInclude('.css-test{color:blue}'); | ||
expect(actual).toInclude('<div style={{}} className={"css-test"}>hello, world!</div>'); |
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.
can you make this test use a dynamic value?
const [color] = useState('blue');
@@ -89,7 +89,10 @@ describe('css prop transformer', () => { | |||
const Component = ({ className, style }) => <div className={className} style={style} css={{ fontSize: 12, color: red }}>hello world</div>; | |||
`); | |||
|
|||
expect(actual).toInclude('style={{ ...style, "--var-test-red": red }}'); | |||
expect(actual).toInclude( | |||
'<div className={"css-test" + (className ? " " + className : "")} style={style}>hello world</div>' |
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.
dynamic value in test pls
@@ -101,7 +104,8 @@ describe('css prop transformer', () => { | |||
const Component = ({ className, ...props }) => <div className={className} style={props.style} css={{ fontSize: 12, color: red }}>hello world</div>; | |||
`); | |||
|
|||
expect(actual).toInclude('style={{ ...props.style, "--var-test-red": red }}'); | |||
expect(actual).toInclude('style={props.style}'); |
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.
dynamic value in test pls
@@ -124,7 +128,8 @@ describe('css prop transformer', () => { | |||
const Component = ({ className, style }) => <div className={className} style={{ ...style, display: 'block' }} css={{ fontSize: 12, color: red }}>hello world</div>; | |||
`); | |||
|
|||
expect(actual).toInclude(`style={{ ...style, display: 'block', \"--var-test-red\": red }}`); | |||
expect(actual).toInclude(`style={{ ...style, display: 'block' }}`); |
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.
dynamic value in test pls
@@ -252,9 +260,9 @@ describe('css prop transformer', () => { | |||
`); | |||
|
|||
expect(actual).toInclude( | |||
`background-image:linear-gradient(45deg,var(--var-test-n30gray) 25%,transparent 25%),linear-gradient(-45deg,var(--var-test-n30gray) 25%,transparent 25%),linear-gradient(45deg,transparent 75%,var(--var-test-n30gray) 75%),linear-gradient(-45deg,transparent 75%,var(--var-test-n30gray) 75%)` | |||
`background-image:linear-gradient(45deg,gray 25%,transparent 25%),linear-gradient(-45deg,gray 25%,transparent 25%),linear-gradient(45deg,transparent 75%,gray 75%),linear-gradient(-45deg,transparent 75%,gray 75%)` |
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.
can you make another test that uses dynamic values for this one please
expect(actual).toInclude( | ||
'background-image:linear-gradient(45deg,var(--var-test-n30gray) 25%,transparent 25%),linear-gradient(-45deg,var(--var-test-n30gray) 25%,transparent 25%),linear-gradient(45deg,transparent 75%,var(--var-test-n30gray) 75%),linear-gradient(-45deg,transparent 75%,var(--var-test-n30gray) 75%)' | ||
'background-image:linear-gradient(45deg,gray 25%,transparent 25%),linear-gradient(-45deg,gray 25%,transparent 25%),linear-gradient(45deg,transparent 75%,gray 75%),linear-gradient(-45deg,transparent 75%,gray 75%)' |
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.
can you make another test that uses dynamic values pls
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.
Yep, I've added them now. Sorry, I totally forgot removing dynamic cases would remove tests that protect these features :)
awesome work - just left some comments to add some stuff back to tests 😄 |
@Madou I think this is ready for another review. Took longer than what I expected. |
love it |
Fix #167