Unverified Commit cd897b5c authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix CSS modules imports not collected due to race conditions (#42392)

This PR fixes the bug reported by @hanneslund (reproduction:
https://github.com/hanneslund/css-mod-import-test).

There can be multiple modules with the same resource, some of them can
be created via plugins (extracted by the mini-css plugin) and some can
be directly transformed via loaders (e.g. the CSS modules loader). In
these cases, we need to merge the resources generated by all of them.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent a0f37877
...@@ -191,8 +191,8 @@ export class FlightManifestPlugin { ...@@ -191,8 +191,8 @@ export class FlightManifestPlugin {
ssrNamedModuleId = `./${ssrNamedModuleId.replace(/\\/g, '/')}` ssrNamedModuleId = `./${ssrNamedModuleId.replace(/\\/g, '/')}`
if (isCSSModule) { if (isCSSModule) {
if (!manifest[resource]) {
const chunks = [...chunk.files].filter((f) => f.endsWith('.css')) const chunks = [...chunk.files].filter((f) => f.endsWith('.css'))
if (!manifest[resource]) {
manifest[resource] = { manifest[resource] = {
default: { default: {
id, id,
...@@ -200,6 +200,13 @@ export class FlightManifestPlugin { ...@@ -200,6 +200,13 @@ export class FlightManifestPlugin {
chunks, chunks,
}, },
} }
} else {
// It is possible that there are mtuliepl modules with the same resouce,
// e.g. extracted by mini-css-extract-plugin. In that case we need to
// merge the chunks.
manifest[resource].default.chunks = [
...new Set([...manifest[resource].default.chunks, ...chunks]),
]
} }
if (chunkGroup.name) { if (chunkGroup.name) {
......
'use client' 'use client'
import 'next/link'
import './client-page.css' import './client-page.css'
import styles from './inner/ClientComponent.module.css'
export default function Page() { export default function Page() {
return <h1>Page!!!</h1> return (
<>
<h1>Page!!!</h1>
<div id="css-modules" className={styles.yuge}>
huge
</div>
</>
)
} }
...@@ -1487,6 +1487,16 @@ describe('app dir', () => { ...@@ -1487,6 +1487,16 @@ describe('app dir', () => {
}) })
describe('client components', () => { describe('client components', () => {
it('should support css modules inside client page', async () => {
const browser = await webdriver(next.url, '/css/css-client')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#css-modules')).fontSize`
)
).toBe('100px')
})
it('should support css modules inside client components', async () => { it('should support css modules inside client components', async () => {
const browser = await webdriver(next.url, '/css/css-client/inner') const browser = await webdriver(next.url, '/css/css-client/inner')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment