Unverified Commit 984bd77b authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix css modules imports in client components (#42077)

parent 80843e1a
...@@ -276,7 +276,31 @@ export class FlightClientEntryPlugin { ...@@ -276,7 +276,31 @@ export class FlightClientEntryPlugin {
}) })
}) })
forEachEntryModule(({ entryModule }) => { forEachEntryModule(({ name, entryModule }) => {
// To collect all CSS imports for a specific entry including the ones
// that are in the client graph, we need to store a map for client boundary
// dependencies.
const clientEntryDependencyMap: Record<string, any> = {}
const entry = compilation.entries.get(name)
entry.includeDependencies.forEach((dep: any) => {
if (
dep.request &&
dep.request.startsWith('next-flight-client-entry-loader?')
) {
const mod: webpack.NormalModule =
compilation.moduleGraph.getResolvedModule(dep)
compilation.moduleGraph
.getOutgoingConnections(mod)
.forEach((connection: any) => {
if (connection.dependency) {
clientEntryDependencyMap[connection.dependency.request] =
connection.dependency
}
})
}
})
for (const connection of compilation.moduleGraph.getOutgoingConnections( for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule entryModule
)) { )) {
...@@ -288,6 +312,7 @@ export class FlightClientEntryPlugin { ...@@ -288,6 +312,7 @@ export class FlightClientEntryPlugin {
layoutOrPageRequest, layoutOrPageRequest,
compilation, compilation,
dependency: layoutOrPageDependency, dependency: layoutOrPageDependency,
clientEntryDependencyMap,
}) })
Object.assign(flightCSSManifest, cssImports) Object.assign(flightCSSManifest, cssImports)
...@@ -327,10 +352,12 @@ export class FlightClientEntryPlugin { ...@@ -327,10 +352,12 @@ export class FlightClientEntryPlugin {
layoutOrPageRequest, layoutOrPageRequest,
compilation, compilation,
dependency, dependency,
clientEntryDependencyMap,
}: { }: {
layoutOrPageRequest: string layoutOrPageRequest: string
compilation: any compilation: any
dependency: any /* Dependency */ dependency: any /* Dependency */
clientEntryDependencyMap?: Record<string, any>
}): [ClientComponentImports, CssImports] { }): [ClientComponentImports, CssImports] {
/** /**
* Keep track of checked modules to avoid infinite loops with recursive imports. * Keep track of checked modules to avoid infinite loops with recursive imports.
...@@ -369,13 +396,12 @@ export class FlightClientEntryPlugin { ...@@ -369,13 +396,12 @@ export class FlightClientEntryPlugin {
if (!visitedBySegment[layoutOrPageRequest]) { if (!visitedBySegment[layoutOrPageRequest]) {
visitedBySegment[layoutOrPageRequest] = new Set() visitedBySegment[layoutOrPageRequest] = new Set()
} }
if ( const storeKey =
!modRequest || (inClientComponentBoundary ? '0' : '1') + ':' + modRequest
visitedBySegment[layoutOrPageRequest].has(modRequest) if (!modRequest || visitedBySegment[layoutOrPageRequest].has(storeKey)) {
) {
return return
} }
visitedBySegment[layoutOrPageRequest].add(modRequest) visitedBySegment[layoutOrPageRequest].add(storeKey)
const isClientComponent = isClientComponentModule(mod) const isClientComponent = isClientComponentModule(mod)
...@@ -404,6 +430,14 @@ export class FlightClientEntryPlugin { ...@@ -404,6 +430,14 @@ export class FlightClientEntryPlugin {
if ((!inClientComponentBoundary && isClientComponent) || isCSS) { if ((!inClientComponentBoundary && isClientComponent) || isCSS) {
clientComponentImports.push(modRequest) clientComponentImports.push(modRequest)
// Here we are entering a client boundary, and we need to collect dependencies
// in the client graph too.
if (isClientComponent && clientEntryDependencyMap) {
if (clientEntryDependencyMap[modRequest]) {
filterClientComponents(clientEntryDependencyMap[modRequest], true)
}
}
return return
} }
......
'use client'
import styles from './ClientComponent.module.css'
export default function ClientComponent() {
return (
<div id="client-component" className={styles.yuge}>
Client Component
</div>
)
}
import Foo from './foo'
export default function Page() {
return <Foo />
}
...@@ -1407,6 +1407,18 @@ describe('app dir', () => { ...@@ -1407,6 +1407,18 @@ describe('app dir', () => {
).toBe('rgb(0, 0, 255)') ).toBe('rgb(0, 0, 255)')
}) })
}) })
describe('client components', () => {
it('should support css modules inside client components', async () => {
const browser = await webdriver(next.url, '/css/css-client/inner')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#client-component')).fontSize`
)
).toBe('100px')
})
})
}) })
describe('searchParams prop', () => { describe('searchParams prop', () => {
......
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