Skip to content

Commit

Permalink
fix: css order problem in async chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Sep 1, 2022
1 parent 091537c commit 4df9062
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 1 deletion.
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 () => {
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.

0 comments on commit 4df9062

Please sign in to comment.