mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix: css order problem in async chunk (#9949)
This commit is contained in:
parent
42ecf3759e
commit
6c7b83434f
@ -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)!
|
||||
|
@ -9,7 +9,8 @@ import {
|
||||
page,
|
||||
removeFile,
|
||||
serverLogs,
|
||||
untilUpdated
|
||||
untilUpdated,
|
||||
withRetry
|
||||
} from '~utils'
|
||||
|
||||
// note: tests should retrieve the element at the beginning of test and reuse it
|
||||
@ -455,3 +456,17 @@ test.runIf(isBuild)('warning can be suppressed by esbuild.logOverride', () => {
|
||||
expect(log).not.toMatch('unsupported-css-property')
|
||||
})
|
||||
})
|
||||
|
||||
// NOTE: the match inline snapshot should generate by build mode
|
||||
test('async css order', async () => {
|
||||
await withRetry(async () => {
|
||||
expect(await getColor('.async-green')).toMatchInlineSnapshot('"green"')
|
||||
expect(await getColor('.async-blue')).toMatchInlineSnapshot('"blue"')
|
||||
}, true)
|
||||
})
|
||||
|
||||
test('async css order with css modules', async () => {
|
||||
await withRetry(async () => {
|
||||
expect(await getColor('.modules-pink')).toMatchInlineSnapshot('"pink"')
|
||||
}, true)
|
||||
})
|
||||
|
3
playground/css/async/async-1.css
Normal file
3
playground/css/async/async-1.css
Normal file
@ -0,0 +1,3 @@
|
||||
.async-blue {
|
||||
color: blue;
|
||||
}
|
4
playground/css/async/async-1.js
Normal file
4
playground/css/async/async-1.js
Normal file
@ -0,0 +1,4 @@
|
||||
import { createButton } from './base'
|
||||
import './async-1.css'
|
||||
|
||||
createButton('async-blue')
|
3
playground/css/async/async-2.css
Normal file
3
playground/css/async/async-2.css
Normal file
@ -0,0 +1,3 @@
|
||||
.async-green {
|
||||
color: green;
|
||||
}
|
4
playground/css/async/async-2.js
Normal file
4
playground/css/async/async-2.js
Normal file
@ -0,0 +1,4 @@
|
||||
import { createButton } from './base'
|
||||
import './async-2.css'
|
||||
|
||||
createButton('async-green')
|
4
playground/css/async/async-3.js
Normal file
4
playground/css/async/async-3.js
Normal file
@ -0,0 +1,4 @@
|
||||
import { createButton } from './base'
|
||||
import styles from './async-3.module.css'
|
||||
|
||||
createButton(`${styles['async-pink']} modules-pink`)
|
3
playground/css/async/async-3.module.css
Normal file
3
playground/css/async/async-3.module.css
Normal file
@ -0,0 +1,3 @@
|
||||
.async-pink {
|
||||
color: pink;
|
||||
}
|
3
playground/css/async/base.css
Normal file
3
playground/css/async/base.css
Normal file
@ -0,0 +1,3 @@
|
||||
.btn {
|
||||
color: black;
|
||||
}
|
8
playground/css/async/base.js
Normal file
8
playground/css/async/base.js
Normal file
@ -0,0 +1,8 @@
|
||||
import './base.css'
|
||||
|
||||
export function createButton(className) {
|
||||
const button = document.createElement('button')
|
||||
button.className = `btn ${className}`
|
||||
document.body.appendChild(button)
|
||||
button.textContent = `button ${getComputedStyle(button).color}`
|
||||
}
|
3
playground/css/async/index.js
Normal file
3
playground/css/async/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
import('./async-1.js')
|
||||
import('./async-2.js')
|
||||
import('./async-3.js')
|
@ -109,3 +109,5 @@ document
|
||||
.classList.add(aliasModule.aliasedModule)
|
||||
|
||||
import './unsupported.css'
|
||||
|
||||
import './async/index'
|
||||
|
Loading…
Reference in New Issue
Block a user