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 modules imports in client components #42077

Merged
merged 3 commits into from Oct 28, 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
Expand Up @@ -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(
entryModule
)) {
Expand All @@ -288,6 +312,7 @@ export class FlightClientEntryPlugin {
layoutOrPageRequest,
compilation,
dependency: layoutOrPageDependency,
clientEntryDependencyMap,
})

Object.assign(flightCSSManifest, cssImports)
Expand Down Expand Up @@ -327,10 +352,12 @@ export class FlightClientEntryPlugin {
layoutOrPageRequest,
compilation,
dependency,
clientEntryDependencyMap,
}: {
layoutOrPageRequest: string
compilation: any
dependency: any /* Dependency */
clientEntryDependencyMap?: Record<string, any>
}): [ClientComponentImports, CssImports] {
/**
* Keep track of checked modules to avoid infinite loops with recursive imports.
Expand Down Expand Up @@ -369,13 +396,12 @@ export class FlightClientEntryPlugin {
if (!visitedBySegment[layoutOrPageRequest]) {
visitedBySegment[layoutOrPageRequest] = new Set()
}
if (
!modRequest ||
visitedBySegment[layoutOrPageRequest].has(modRequest)
) {
const storeKey =
(inClientComponentBoundary ? '0' : '1') + ':' + modRequest
if (!modRequest || visitedBySegment[layoutOrPageRequest].has(storeKey)) {
return
}
visitedBySegment[layoutOrPageRequest].add(modRequest)
visitedBySegment[layoutOrPageRequest].add(storeKey)

const isClientComponent = isClientComponentModule(mod)

Expand Down Expand Up @@ -404,6 +430,14 @@ export class FlightClientEntryPlugin {
if ((!inClientComponentBoundary && isClientComponent) || isCSS) {
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
}

Expand Down
@@ -0,0 +1,3 @@
.yuge {
font-size: 100px;
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/app/app/css/css-client/inner/foo.js
@@ -0,0 +1,11 @@
'use client'

import styles from './ClientComponent.module.css'

export default function ClientComponent() {
return (
<div id="client-component" className={styles.yuge}>
Client Component
</div>
)
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app/app/css/css-client/inner/page.js
@@ -0,0 +1,5 @@
import Foo from './foo'

export default function Page() {
return <Foo />
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -1407,6 +1407,18 @@ describe('app dir', () => {
).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', () => {
Expand Down