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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
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
17 changes: 16 additions & 1 deletion playground/css/__tests__/css.spec.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions playground/css/async/async-1.css
@@ -0,0 +1,3 @@
.async-blue {
color: blue;
}
4 changes: 4 additions & 0 deletions playground/css/async/async-1.js
@@ -0,0 +1,4 @@
import { createButton } from './base'
import './async-1.css'

createButton('async-blue')
3 changes: 3 additions & 0 deletions playground/css/async/async-2.css
@@ -0,0 +1,3 @@
.async-green {
color: green;
}
4 changes: 4 additions & 0 deletions playground/css/async/async-2.js
@@ -0,0 +1,4 @@
import { createButton } from './base'
import './async-2.css'

createButton('async-green')
4 changes: 4 additions & 0 deletions playground/css/async/async-3.js
@@ -0,0 +1,4 @@
import { createButton } from './base'
import styles from './async-3.module.css'

createButton(`${styles['async-pink']} modules-pink`)
3 changes: 3 additions & 0 deletions playground/css/async/async-3.module.css
@@ -0,0 +1,3 @@
.async-pink {
color: pink;
}
3 changes: 3 additions & 0 deletions playground/css/async/base.css
@@ -0,0 +1,3 @@
.btn {
color: black;
}
8 changes: 8 additions & 0 deletions playground/css/async/base.js
@@ -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 changes: 3 additions & 0 deletions playground/css/async/index.js
@@ -0,0 +1,3 @@
import('./async-1.js')
import('./async-2.js')
import('./async-3.js')
2 changes: 2 additions & 0 deletions playground/css/main.js
Expand Up @@ -109,3 +109,5 @@ document
.classList.add(aliasModule.aliasedModule)

import './unsupported.css'

import './async/index'