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

[zero] Fix wrong CSS order by moving import to last #41002

Merged
merged 2 commits into from
Feb 13, 2024

Conversation

siriwatknp
Copy link
Member

Issue

Wrong CSS order injection when nested components are used.

Consider Material UI Button > ButtonBase > TouchRipple

Before the change, the compiled CSS is imported at the top of the file like this:

// compiled Button.js
import 'output.css'
import ButtonBase from '../ButtonBase';

// compiled ButtonBase.js
import 'output.css';
import TouchRipple from './TouchRipple;

This produces CSS is this order:

// page.css
.hashed-button { … }

.hashed-button-base { … }

.hashed-touch-ripple { … }

Which causes the button styles to be overridden by button base and touch ripple

Change

Just move CSS import to the last.

@siriwatknp siriwatknp added bug 🐛 Something doesn't work nextjs package: pigment-css Specific to @pigment-css/* labels Feb 8, 2024
@siriwatknp siriwatknp changed the title Fix/zero css order [zero] Fix wrong CSS order by moving import to last Feb 8, 2024
map: result.sourceMap,
};
}
const cssId = `./${cssFilename}`;
cssFileLookup.set(cssId, cssFilename);
cssLookup.set(cssFilename, cssText);
return {
code: `import ${JSON.stringify(`./${cssFilename}`)};\n${result.code}`,
code: `${result.code}\nimport ${JSON.stringify(`./${cssFilename}`)};`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to change this for vite as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

This line does not affect Vite at the moment because zero-vite-plugin is not using zero-unplugin but I can confirm that by importing CSS first, the order is wrong even with Vite.

- result.code += `\nimport ${JSON.stringify(cssFilename)};\n`; ✅ CSS order is correct.
+ result.code = `import ${JSON.stringify(cssFilename)};\n${result.code}`; ❌ CSS order is wrong.

result.code += `\nimport ${JSON.stringify(cssFilename)};\n`;

So I think CSS should always be imported last.

Copy link
Member

Choose a reason for hiding this comment

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

Is there ways to create unit tests for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

@siriwatknp vite plugin and unplugin both kind of duplicate the same code. So ideally, any changes done in unplugin should also be done in vite plugin.

Copy link
Contributor

Choose a reason for hiding this comment

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

@oliviertassinari We could have a snapshot test for it that verifies the built css file changes. I'll have to look into how we can have a proper unit test for the build tool itself.

@mui-bot
Copy link

mui-bot commented Feb 8, 2024

Netlify deploy preview

https://deploy-preview-41002--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 2c1ccca

@oliviertassinari oliviertassinari added the package: system Specific to @mui/system label Feb 8, 2024
@siriwatknp siriwatknp self-assigned this Feb 13, 2024
@siriwatknp siriwatknp merged commit 36db6ed into mui:master Feb 13, 2024
23 checks passed
@siriwatknp
Copy link
Member Author

@oliviertassinari Why would you add package: system label? it's not related to system.

@oliviertassinari
Copy link
Member

oliviertassinari commented Feb 13, 2024

Is zero different from system? They could be the same projet, where the CSS engine is more important and where Grid, theme, are more secondary/opinionated.

But fair, it's a strategic product decision, it depends on how we want to play it. In Material UI v4, it was decouple. Tailwind CSS couple them (and people seem to like this about it). Styled components/emotion decouple them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work nextjs package: pigment-css Specific to @pigment-css/* package: system Specific to @mui/system
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants