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

fix: css order problem in async chunk #9949

Merged
merged 6 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
deps.add(chunk.fileName)
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata.importedCss.forEach((file) => {
deps.add(file)
})
chunk.imports.forEach(addDeps)
} else {
const removedPureCssFiles =
removedPureCssFilesCache.get(config)!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, test } from 'vitest'
import { getColor, isBuild, page } from '~utils'

describe.runIf(isBuild)('build', () => {
test('should apply correct style', async () => {
Copy link
Contributor

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?

Copy link
Member

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)

Copy link
Contributor

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.

Copy link
Contributor Author

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)

Need i update the test file in the pr and remove react?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, get 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.

i have optimized the test cases and remove react in them.

const greenButton = await page.$('.green')
const blueButton = await page.$('.blue')
expect(await getColor(greenButton)).toBe('green')
expect(await getColor(blueButton)).toBe('blue')
})
})
3 changes: 3 additions & 0 deletions playground/async-chunk-css-order/components/BlueButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.blue {
color: blue;
}
7 changes: 7 additions & 0 deletions playground/async-chunk-css-order/components/BlueButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import { Button } from './Button'
import './BlueButton.css'

export function BlueButton() {
return <Button className="blue">I'm a blue button</Button>
}
3 changes: 3 additions & 0 deletions playground/async-chunk-css-order/components/Button.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.btn {
color: black;
}
10 changes: 10 additions & 0 deletions playground/async-chunk-css-order/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import './Button.css'

export function Button({ children, className }) {
return (
<button className={`btn ${className}`} type="button">
{children}
</button>
)
}
3 changes: 3 additions & 0 deletions playground/async-chunk-css-order/components/GreenButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.green {
color: green;
}
7 changes: 7 additions & 0 deletions playground/async-chunk-css-order/components/GreenButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import { Button } from './Button'
import './GreenButton.css'

export function GreenButton() {
return <Button className="green">I'm a green button</Button>
}
15 changes: 15 additions & 0 deletions playground/async-chunk-css-order/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>

<body>
<div id="green"></div>
<div id="blue"></div>
<script type="module" src="/main.jsx"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions playground/async-chunk-css-order/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { render } from 'react-dom'
import React from 'react'

import('./components/GreenButton').then(({ GreenButton }) => {
render(<GreenButton />, document.querySelector('#green'))
})

import('./components/BlueButton').then(({ BlueButton }) => {
render(<BlueButton />, document.querySelector('#blue'))
})
15 changes: 15 additions & 0 deletions playground/async-chunk-css-order/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test-async-chunk-css-order",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
1 change: 1 addition & 0 deletions playground/async-chunk-css-order/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.