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

refactor: reduce code complexity for css generation [SPA-2573] #999

Open
wants to merge 12 commits into
base: development
Choose a base branch
from

Conversation

Chaoste
Copy link
Contributor

@Chaoste Chaoste commented Feb 19, 2025

Purpose

Improve the readability of critical code, reduce duplicated code, remove not needed code.

Approach

This is just a step towards the right direction. We're still running similar logic in multiple places without generalising it. I tried to reduce it slightly already but didn't mean to spend much more time on it atm.

  • Remove debugging comments of actual CSS code
  • Generalise repeated code for creating CSS code, e.g. filtering for undefined rules & checking for structures with relative height

TODO

  • Find out why we're creating default styles in ssrStyles.ts for pattern nodes, cc @anwaar931

@Chaoste Chaoste requested review from a team as code owners February 19, 2025 10:55
Copy link

vercel bot commented Feb 19, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nextjs-marketing-demo-bug-test ✅ Ready (Inspect) Visit Preview Feb 28, 2025 10:52am
3 Skipped Deployments
Name Status Preview Updated (UTC)
experience-builder-test-app ⬜️ Ignored (Inspect) Feb 28, 2025 10:52am
studio-nextjs-marketing-demo ⬜️ Ignored (Inspect) Feb 28, 2025 10:52am
studio-react-vite-template ⬜️ Ignored (Inspect) Feb 28, 2025 10:52am

@Chaoste Chaoste changed the title refactor: css generation refactor: reduce code complexity for css generation Feb 19, 2025
@Chaoste Chaoste changed the title refactor: reduce code complexity for css generation refactor: reduce code complexity for css generation [SPA-2573] Feb 19, 2025
Base automatically changed from refactor/preview-style-media-queries to development February 19, 2025 12:14
expect(styles).toBe(
'.cf-ae92b9eb2c20a262573a8085db3dbd67{box-sizing:border-box;margin:2rem;padding:2rem;background-color:white;width:50%;height:25%;border:1px solid black;gap:2rem 2rem;font-size:1rem;color:black;}@media(max-width:992px){.cf-1fe22311d2b832dd529a9ce5e8d77281{box-sizing:border-box;margin:1.5rem;padding:1.5rem;background-color:black;width:75%;height:50%;border:3px solid black;gap:1.5rem 1.5rem;font-size:0.75rem;color:green;}}@media(max-width:576px){.cf-63009ce9ca644a23e001721d6358e491{box-sizing:border-box;margin:1rem;padding:1rem;background-color:red;width:100%;height:100%;border:0px solid transparent;gap:1rem 1rem;font-size:1.5rem;color:orange;}}',
expect(styles).toMatchInlineSnapshot(
`".cf-7b1d28fe46d9488fa595e4ff21270f2e{box-sizing:border-box;margin:2rem;padding:2rem;background-color:white;width:50%;height:25%;border:1px solid black;gap:2rem 2rem;font-size:1rem;color:black;}@media(max-width:992px){.cf-af21918d8656639646a9eb86a7ca06e2{box-sizing:border-box;margin:1.5rem;padding:1.5rem;background-color:black;width:75%;height:50%;border:3px solid black;gap:1.5rem 1.5rem;font-size:0.75rem;color:green;}}@media(max-width:576px){.cf-5a0d66d5b7e5c60dac49f3a86e4c1c8d{box-sizing:border-box;margin:1rem;padding:1rem;background-color:red;width:100%;height:100%;border:0px solid transparent;gap:1rem 1rem;font-size:1.5rem;color:orange;}}"`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

the fact that the hashes have changed here make me anxious. If this PR was a pure refactor, the hash would be the same

Copy link
Contributor Author

@Chaoste Chaoste Feb 27, 2025

Choose a reason for hiding this comment

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

Mmh, not sure what would be the correct prefix instead of refactor to be honest. Do you have a specific one in mind?

The hash generation changed as before we used the object of styles and stringified it before calling md5(). I changed it to use the resulting CSS string right away, so we have one less stringifying. The string itself is different (JS object vs. CSS rules) which is why the hash is different.

Comment on lines +1428 to +1429
expect(styles).toMatchInlineSnapshot(
`".cf-fb5147aff29cf3474d7f68cd382ba916{box-sizing:border-box;margin:0 Auto 0 Auto;padding:10px 10px 10px 10px;background-color:rgba(255, 255, 255, 0);width:100%;max-width:1192px;border:0px outside rgba(255, 255, 255, 0);gap:10px 10px;}.cf-1031d744e26de7b4e9422de0e74ae5cd{box-sizing:border-box;padding:0 0 0 0;background-color:rgba(255, 255, 255, 0);grid-column:span 6;border:0px outside rgba(255, 255, 255, 0);gap:0px 0px;align-items:start;justify-content:safe center;flex-direction:column;flex-wrap:nowrap;}.cf-4c0b9d7e44ec5898b6f5480ac5b1a634{box-sizing:border-box;padding:0 0 0 0;background-color:rgba(255, 255, 255, 0);grid-column:span 6;border:0px outside rgba(255, 255, 255, 0);gap:0px 0px;align-items:center;justify-content:safe center;flex-direction:column;flex-wrap:nowrap;}.cf-2745e04133568665b6d779d76342d59f{box-sizing:border-box;margin:0 0 0 0;padding:0 0 0 0;width:100%;max-width:none;}.cf-fe878a9e705110f2fee7b5698943918a{box-sizing:border-box;margin:0 0 0 0;padding:0 0 0 0;width:100%;max-width:none;font-size:16px;font-weight:400;line-height:20px;letter-spacing:0px;color:rgba(0, 0, 0, 1);text-align:left;text-transform:none;}.cf-0326e88429ff35544d5acf70391db2b2{box-sizing:border-box;margin:0 0 0 0;padding:0 0 0 0;}@media(max-width:992px){.cf-92b718a315702272accd18eb39a4a427{box-sizing:border-box;}}@media(max-width:576px){.cf-8187cd93307f9e0ceffff3cad17ce4d3{box-sizing:border-box;}}"`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same question here - what made the hashes change?

'font-size': '1rem',
};

const res = createJoinedCSSRules(cssRules);
Copy link
Collaborator

Choose a reason for hiding this comment

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

to me this function name sounds very crypric. What is the Joined CSS rule and what is the joined vs not joined css rule.

Maybe we call it toMinifiedCSS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking of the function Array.join which turns an array into a joined string. Happy to rename it though if that makes it easier to understand. As it does creat both normal CSS and minified CSS, I would like to go with stringifyCSSProperties (thinking of JSON.stringify) or something similar to that. Or is toCSS maybe enough?

Copy link
Collaborator

@Spring3 Spring3 left a comment

Choose a reason for hiding this comment

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

I left many comments, but I generally have these points as a reason why I don't think that this PR needs to be merged in this current state:

  1. The comments were left there on purpose, so that in a couple of months when we read that code, we can see a visual example of how the data gets transformed and understand what is happening without adding any extra logging. I am puzzled to understand why they are being deleted.
  2. I personally find certain namings too cryptic
  • for example buildCfStyles vs buildCfStylesWIthSpecialCasing - I feel like we need to have only 1 function - buildCfStyles. The rest are additional transformers that we can call as a chain after calling buildCfStyles, but now we have 2 functions that do the same, just 1 is smarter than the other and now I, as a developer, need to know when to call one and when the other (and that makes me question - why do we have 2 functions?)
  • I would rename buildCfStyles to _buildCfStyles and then rename buildCfStylesWIthSpecialCasing to buildCfStyles and export that one publicly.
  1. Hash changes in the tests. The goal was to make sure that hashes stayed unchanged. The fact that they have changed in this PR, makes me alert, because that means that output styles are going to change to everyone who is using this SDK in prod with SSR. What made them change?
  2. The fact that instead of having hardcoded hashes, we are now testing their format. That check will never fail and that's not the purpose of those tests

@@ -247,28 +147,6 @@ export const detachExperienceStyles = (experience: Experience): string | undefin
);
},
});
/**
* propsByBreakpoint {
Copy link
Contributor

Choose a reason for hiding this comment

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

When I was reviewing the original PR, I found these comments quite helpful. Could we not remove them?
If we don't want them to bloat the code here, they could also be moved to the docstring of the respective functions that produce each data structure.
So this propsByBreakpoint example can be moved to the indexByBreakpoint function.
And the stylesForBreakpoint example later on can go into the doc string of builtCfStyles this way they are easy to see every time you use each function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was having a hard time to visually parse the whole function. So moving it to the docstring of each function would be great. Will do that!

@Chaoste
Copy link
Contributor Author

Chaoste commented Feb 28, 2025

Hey @Spring3 @SofiaMargariti
Thanks for your reviews and sorry for these big changes that might require some time to go through. I tried to incorporate all your feedback:

  • Rename cryptic function names and follow existing terminology (example "rules")
  • Keep example data format comments. I shrinked them by a bit but added more of them.
  • I also added docstrings to bigger functions and added example inputs & outputs as requested.
  • I reverted the test changes that replace the actual hash strings by regular expressions. We're checking the hashes again in every place but we're using toMatchInlineSnapshots as this allows us to manually update them in one go without having to go manually replace each string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants