fix: css order problem in async chunk (#9949)

This commit is contained in:
yangxingyuan 2022-09-22 17:10:42 +08:00 committed by GitHub
parent 42ecf3759e
commit 6c7b83434f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 56 additions and 2 deletions

View File

@ -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)!

View File

@ -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)
})

View File

@ -0,0 +1,3 @@
.async-blue {
color: blue;
}

View File

@ -0,0 +1,4 @@
import { createButton } from './base'
import './async-1.css'
createButton('async-blue')

View File

@ -0,0 +1,3 @@
.async-green {
color: green;
}

View File

@ -0,0 +1,4 @@
import { createButton } from './base'
import './async-2.css'
createButton('async-green')

View File

@ -0,0 +1,4 @@
import { createButton } from './base'
import styles from './async-3.module.css'
createButton(`${styles['async-pink']} modules-pink`)

View File

@ -0,0 +1,3 @@
.async-pink {
color: pink;
}

View File

@ -0,0 +1,3 @@
.btn {
color: black;
}

View 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}`
}

View File

@ -0,0 +1,3 @@
import('./async-1.js')
import('./async-2.js')
import('./async-3.js')

View File

@ -109,3 +109,5 @@ document
.classList.add(aliasModule.aliasedModule)
import './unsupported.css'
import './async/index'