Skip to content
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

Merged
merged 15 commits into from
May 1, 2020

Conversation

ankeetmaini
Copy link
Contributor

Fix #167

@itsdouges
Copy link
Collaborator

itsdouges commented Apr 29, 2020

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)

@@ -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();
Copy link
Collaborator

@itsdouges itsdouges Apr 29, 2020

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

Copy link
Contributor Author

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)) {
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it!

Copy link
Contributor Author

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!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you turn off whitespace the diff becomes way better

image

@itsdouges
Copy link
Collaborator

left some comments :) awesome first pass

@itsdouges itsdouges added the wip 🚧 Work in progress - don't judge too harshly. label Apr 29, 2020
@ankeetmaini ankeetmaini changed the title [WIP] feat: Inline const variables in CSS [WIP] feat: inline const variables in CSS Apr 29, 2020
@ankeetmaini ankeetmaini changed the title [WIP] feat: inline const variables in CSS feat: inline const variables in CSS Apr 29, 2020
@ankeetmaini
Copy link
Contributor Author

@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>');
Copy link
Collaborator

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>');
Copy link
Collaborator

@itsdouges itsdouges Apr 29, 2020

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>'
Copy link
Collaborator

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}');
Copy link
Collaborator

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' }}`);
Copy link
Collaborator

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%)`
Copy link
Collaborator

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%)'
Copy link
Collaborator

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

Copy link
Contributor Author

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 :)

@itsdouges
Copy link
Collaborator

awesome work - just left some comments to add some stuff back to tests 😄

@ankeetmaini
Copy link
Contributor Author

@Madou I think this is ready for another review. Took longer than what I expected. templateLiteralToCss is a beast :D

@itsdouges itsdouges merged commit 3261f8a into atlassian-labs:master May 1, 2020
@itsdouges
Copy link
Collaborator

love it

@ankeetmaini ankeetmaini deleted the inline-const branch May 2, 2020 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip 🚧 Work in progress - don't judge too harshly.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inline identifiers that are constants into CSS
3 participants