-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: css order problem in async chunk #9949
Conversation
@poyoho @IanVS pinging you since you have been looking into this problem before. Related PRs: This PR from @sanyuan0704 looks good to me. Looking at your previous PRs @poyoho I think I may be missing something here. But seeing the same scheme but in the aggregation of module preload and CSS for HTML entries, we are also doing the same order as this PR is proposing: vite/packages/vite/src/node/plugins/html.ts Line 657 in 85fa2c8
|
Thanks for the ping, @patak-dev. I built this branch and tried it out in my app, and unfortunately it doesn't seem to fix the issue for me. I think it would be great if the tests from #9278 could be added in this PR as well, and that might illuminate the problem. |
I found the reproduction repo in your issue https://stackblitz.com/edit/vite-gu874q?file=main.jsx. And in this pr branch the css problem in the repo can be resolved. |
I don't doubt that, but there are other cases that @poyoho and I identified as he's been working extensively on this issue, which we've worked together to create test cases for. For instance, mixing css and css module imports, or one that's particularly tricky: multiple imports that share a common css import. |
@sanyuan0704 Thank you very much for studying this question. But the real situation is much more complicated than that test case. So I think this PR does not solve the problem of CSS order. Because this PR is the same as my first commit. If you are interested in following up on this issue, you can refer to our latest test case #9278 |
Sure I don't see why not, as long as the issue stays open. We can update the reproduction with a different test case. |
This change can pass some test case. However, the most fundamental reason is that the loading of dev is I am worried that it will affect users who used to run well with this bug. |
I think the problem solved by this pr is that the css of the asynchronous chunk itself should not be overwritten by the dependent chunk style, which should be a very reasonable scene.Because in current version, Vite will inject css link by internal preload helper function as following sequence:
But the correct way is opposit:
|
@poyoho about this,
They should already be affected by this issue if that is the case. Right now, if you have If you do |
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.
I agree with #9949 (comment) and the change looks good to me.
Adding this one to the 3.2 milestone to avoid potential issues. I think we should start the 3.2 beta sooner this time so we can ship this fix and others that are already piling up. |
The loading is parallel in prod, but the CSS's precedence is determined by the order of link/style tags. So it does not matter whether the loading is parallel or serial.
Do you mean something like below? // main.jsx
setTimeout(() => {
import('./components/GreenButton').then(({ GreenButton }) => {
render(<GreenButton />, document.querySelector('#green'))
})
}, 1000)
import('./components/BlueButton').then(({ BlueButton }) => {
render(<BlueButton />, document.querySelector('#blue'))
}) This worked with this PR. I think it's working because Or do you mean something like below? // GreenButton.jsx
import React from 'react'
import { Button } from './Button'
import './GreenButton.css'
await new Promise(resolve => setTimeout(resolve, 1000))
export function GreenButton() {
return <Button className="green">I'm a green button</Button>
} This seems to work with this PR too. (
// GreenButton.jsx
import React from 'react'
import './GreenButton.css'
import { Button } from './Button'
export function GreenButton() {
return <Button className="green">I'm a green button</Button>
} I think you mean the code above. Yes, this does not work with this PR. (green button should be black but it's green) |
import { getColor, isBuild, page } from '~utils' | ||
|
||
describe.runIf(isBuild)('build', () => { | ||
test('should apply correct style', async () => { |
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.
Should this maybe go in the css
playground, as a subfolder, instead of its own playground?
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.
The ideal would be if this could be added directly to the css
playground without React. But if not, we may want to give this new playground a more generic name so we can test other things on it (it is similar to the preload
that uses Vue also)
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.
Ah yes I didn't notice this is using React. The tests in @poyoho's PRs didn't use react, I think this could be refactored to remove React in a similar way.
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.
The ideal would be if this could be added directly to the
css
playground without React. But if not, we may want to give this new playground a more generic name so we can test other things on it (it is similar to thepreload
that uses Vue also)
Need i update the test file in the pr and remove react?
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.
@sanyuan0704 maybe you could remove the new playground and use the one from @poyoho testing this particular bug here #9278. They are already coded for the css playground
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.
Okay, get 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.
i have optimized the test cases and remove react in them.
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.
@sapphi-red Thank you following of it. After @patak-dev comment and your comment I think my worries may be superfluous. This PR maybe can resolve a lot issues and the impact is not big too. Although there are still some unsolved problems, it should be a step in the right direction.
And my test case is from #9278. setTimeout case like this. I think it is impossible to guarantee the same CSS order under the current CSS loading logic. So my approach is to make the CSS loading logic of Dev and prod consistent. And maybe we can use more runtime to fix it had the same order. But I still think it's hard to do 🤦 |
This problem has bothered me for a long time. When I use UnoCSS and UI component library together, UnoCSS can correctly override UI component library styles in development environment, but not in production environment, the reason is that Vite development and production CSS are not in the same order (This text is a machine translation, sorry, my English is not very good) |
85ed65d
f471fdf
to
81044bf
Compare
I'm confused that after |
This reverts commit 81044bf.
Thanks! I do not know why it didn't work in your environment, but I've pushed a commit after running |
My local pnpm version was 7.9 which matched the vite repo.But it still cannot work.Anyway, thank you for updating the file |
In some case which use async chunk, the style of current chunk could be covered by the dependencies, cause page style to fail.
Example: #3924
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).