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

CSS modules support improvements for Server Components (#38536)

Improve CSS modules support in server components.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have 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 helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
Co-authored-by: 's avatarJiachi Liu <4800338+huozhi@users.noreply.github.com>
parent 9342a6cc
...@@ -249,22 +249,24 @@ export const css = curry(async function css( ...@@ -249,22 +249,24 @@ export const css = curry(async function css(
}) })
) )
// Throw an error for CSS Modules used outside their supported scope if (!ctx.experimental.appDir) {
fns.push( // Throw an error for CSS Modules used outside their supported scope
loader({ fns.push(
oneOf: [ loader({
markRemovable({ oneOf: [
test: [regexCssModules, regexSassModules], markRemovable({
use: { test: [regexCssModules, regexSassModules],
loader: 'error-loader', use: {
options: { loader: 'error-loader',
reason: getLocalModuleImportError(), options: {
reason: getLocalModuleImportError(),
},
}, },
}, }),
}), ],
], })
}) )
) }
if (ctx.isServer) { if (ctx.isServer) {
fns.push( fns.push(
...@@ -371,6 +373,24 @@ export const css = curry(async function css( ...@@ -371,6 +373,24 @@ export const css = curry(async function css(
], ],
}) })
) )
fns.push(
loader({
oneOf: [
markRemovable({
sideEffects: false,
test: regexCssModules,
issuer: {
or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
// Also match the virtual client entry which doesn't have file path
(filePath) => !filePath,
],
},
use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
}),
],
})
)
} }
} }
......
import { SERVER_RUNTIME } from '../../../lib/constants' import { SERVER_RUNTIME } from '../../../lib/constants'
export default async function transformSource(this: any): Promise<string> { export default async function transformSource(this: any): Promise<string> {
let { modules, runtime, ssr } = this.getOptions() let { modules, runtime, ssr, server } = this.getOptions()
if (!Array.isArray(modules)) { if (!Array.isArray(modules)) {
modules = modules ? [modules] : [] modules = modules ? [modules] : []
} }
...@@ -9,6 +9,7 @@ export default async function transformSource(this: any): Promise<string> { ...@@ -9,6 +9,7 @@ export default async function transformSource(this: any): Promise<string> {
const requests = modules as string[] const requests = modules as string[]
const code = const code =
requests requests
.filter((request) => (server ? !request.endsWith('.css') : true))
.map((request) => `import(/* webpackMode: "eager" */ '${request}')`) .map((request) => `import(/* webpackMode: "eager" */ '${request}')`)
.join(';\n') + .join(';\n') +
` `
......
...@@ -22,7 +22,7 @@ type Options = { ...@@ -22,7 +22,7 @@ type Options = {
const PLUGIN_NAME = 'ClientEntryPlugin' const PLUGIN_NAME = 'ClientEntryPlugin'
export const injectedClientEntries = new Map() export const injectedClientEntries = new Map()
const regexCssGlobal = /(?<!\.module)\.css$/ const regexCSS = /\.css$/
export class ClientEntryPlugin { export class ClientEntryPlugin {
dev: boolean = false dev: boolean = false
...@@ -84,7 +84,7 @@ export class ClientEntryPlugin { ...@@ -84,7 +84,7 @@ export class ClientEntryPlugin {
if ( if (
clientComponentRegex.test(modRequest) || clientComponentRegex.test(modRequest) ||
regexCssGlobal.test(modRequest) regexCSS.test(modRequest)
) { ) {
clientComponentImports.push(modRequest) clientComponentImports.push(modRequest)
} }
...@@ -115,7 +115,7 @@ export class ClientEntryPlugin { ...@@ -115,7 +115,7 @@ export class ClientEntryPlugin {
isDev: this.dev, isDev: this.dev,
}) })
const clientLoader = `next-flight-client-entry-loader?${stringify({ const loaderOptions = {
modules: clientComponentImports, modules: clientComponentImports,
runtime: this.isEdgeServer runtime: this.isEdgeServer
? SERVER_RUNTIME.edge ? SERVER_RUNTIME.edge
...@@ -123,6 +123,13 @@ export class ClientEntryPlugin { ...@@ -123,6 +123,13 @@ export class ClientEntryPlugin {
ssr: pageStaticInfo.ssr, ssr: pageStaticInfo.ssr,
// Adding name here to make the entry key unique. // Adding name here to make the entry key unique.
name, name,
}
const clientLoader = `next-flight-client-entry-loader?${stringify(
loaderOptions
)}!`
const clientSSRLoader = `next-flight-client-entry-loader?${stringify({
...loaderOptions,
server: true,
})}!` })}!`
const bundlePath = 'app' + normalizePagePath(routeInfo.page) const bundlePath = 'app' + normalizePagePath(routeInfo.page)
...@@ -157,7 +164,7 @@ export class ClientEntryPlugin { ...@@ -157,7 +164,7 @@ export class ClientEntryPlugin {
// Inject the entry to the server compiler (__sc_client__). // Inject the entry to the server compiler (__sc_client__).
const clientComponentEntryDep = ( const clientComponentEntryDep = (
webpack as any webpack as any
).EntryPlugin.createDependency(clientLoader, { ).EntryPlugin.createDependency(clientSSRLoader, {
name: name + NEXT_CLIENT_SSR_ENTRY_SUFFIX, name: name + NEXT_CLIENT_SSR_ENTRY_SUFFIX,
}) })
promises.push( promises.push(
......
...@@ -93,7 +93,7 @@ export class FlightManifestPlugin { ...@@ -93,7 +93,7 @@ export class FlightManifestPlugin {
if ( if (
mod.request && mod.request &&
/(?<!\.module)\.css$/.test(mod.request) && /\.css$/.test(mod.request) &&
(dev (dev
? mod.loaders.some((item: any) => ? mod.loaders.some((item: any) =>
item.loader.includes('next-style-loader/index.js') item.loader.includes('next-style-loader/index.js')
......
...@@ -16,7 +16,7 @@ import { ...@@ -16,7 +16,7 @@ import {
renderToInitialStream, renderToInitialStream,
createBufferedTransformStream, createBufferedTransformStream,
continueFromInitialStream, continueFromInitialStream,
createSuffixStream, createPrefixStream,
} from './node-web-streams-helper' } from './node-web-streams-helper'
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { tryGetPreviewData } from './api-utils/node' import { tryGetPreviewData } from './api-utils/node'
...@@ -113,7 +113,8 @@ function useFlightResponse( ...@@ -113,7 +113,8 @@ function useFlightResponse(
writable: WritableStream<Uint8Array>, writable: WritableStream<Uint8Array>,
cachePrefix: string, cachePrefix: string,
req: ReadableStream<Uint8Array>, req: ReadableStream<Uint8Array>,
serverComponentManifest: any serverComponentManifest: any,
cssFlightData: string
) { ) {
const id = cachePrefix + ',' + (React as any).useId() const id = cachePrefix + ',' + (React as any).useId()
let entry = rscCache.get(id) let entry = rscCache.get(id)
...@@ -125,7 +126,10 @@ function useFlightResponse( ...@@ -125,7 +126,10 @@ function useFlightResponse(
rscCache.set(id, entry) rscCache.set(id, entry)
let bootstrapped = false let bootstrapped = false
const forwardReader = forwardStream.getReader() // We only attach CSS chunks to the inlined data.
const forwardReader = forwardStream
.pipeThrough(createPrefixStream(cssFlightData))
.getReader()
const writer = writable.getWriter() const writer = writable.getWriter()
function process() { function process() {
forwardReader.read().then(({ done, value }) => { forwardReader.read().then(({ done, value }) => {
...@@ -188,7 +192,7 @@ function createServerComponentRenderer( ...@@ -188,7 +192,7 @@ function createServerComponentRenderer(
globalThis.__next_chunk_load__ = () => Promise.resolve() globalThis.__next_chunk_load__ = () => Promise.resolve()
} }
const cssFlight = getCssFlight(ComponentMod, serverComponentManifest) const cssFlightData = getCssFlightData(ComponentMod, serverComponentManifest)
let RSCStream: ReadableStream<Uint8Array> let RSCStream: ReadableStream<Uint8Array>
const createRSCStream = () => { const createRSCStream = () => {
...@@ -199,7 +203,7 @@ function createServerComponentRenderer( ...@@ -199,7 +203,7 @@ function createServerComponentRenderer(
{ {
context: serverContexts, context: serverContexts,
} }
).pipeThrough(createSuffixStream(cssFlight)) )
} }
return RSCStream return RSCStream
} }
...@@ -211,7 +215,8 @@ function createServerComponentRenderer( ...@@ -211,7 +215,8 @@ function createServerComponentRenderer(
writable, writable,
cachePrefix, cachePrefix,
reqStream, reqStream,
serverComponentManifest serverComponentManifest,
cssFlightData
) )
const root = response.readRoot() const root = response.readRoot()
return root return root
...@@ -322,7 +327,7 @@ function getSegmentParam(segment: string): { ...@@ -322,7 +327,7 @@ function getSegmentParam(segment: string): {
return null return null
} }
function getCssFlight(ComponentMod: any, serverComponentManifest: any) { function getCssFlightData(ComponentMod: any, serverComponentManifest: any) {
const importedServerCSSFiles: string[] = const importedServerCSSFiles: string[] =
ComponentMod.__client__?.__next_rsc_css__ || [] ComponentMod.__client__?.__next_rsc_css__ || []
...@@ -760,7 +765,10 @@ export async function renderToHTML( ...@@ -760,7 +765,10 @@ export async function renderToHTML(
return [actualSegment] return [actualSegment]
} }
const cssFlight = getCssFlight(ComponentMod, serverComponentManifest) const cssFlightData = getCssFlightData(
ComponentMod,
serverComponentManifest
)
const flightData: FlightData = [ const flightData: FlightData = [
// TODO: change walk to output without '' // TODO: change walk to output without ''
walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState).slice( walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState).slice(
...@@ -770,7 +778,7 @@ export async function renderToHTML( ...@@ -770,7 +778,7 @@ export async function renderToHTML(
return new RenderResult( return new RenderResult(
renderToReadableStream(flightData, serverComponentManifest) renderToReadableStream(flightData, serverComponentManifest)
.pipeThrough(createSuffixStream(cssFlight)) .pipeThrough(createPrefixStream(cssFlightData))
.pipeThrough(createBufferedTransformStream()) .pipeThrough(createBufferedTransformStream())
) )
} }
......
import './style.css'
import styles from './style.module.css'
export default function ClientLayout({ children }) {
return (
<>
<div className={styles['client-css']}>Client Layout: CSS Modules</div>
<div className="client-css">Client Layout: Global CSS</div>
{children}
</>
)
}
export default function Page() {
return null
}
import './style.css'
import styles from './style.module.css'
export default function ServerLayout({ children }) {
return (
<>
<div id="server-cssm" className={styles['server-css']}>
Server Layout: CSS Modules
</div>
<div className="server-css">Server Layout: Global CSS</div>
{children}
</>
)
}
.server-css {
color: green;
}
...@@ -393,66 +393,86 @@ describe('app dir', () => { ...@@ -393,66 +393,86 @@ describe('app dir', () => {
}) })
describe('css support', () => { describe('css support', () => {
it('should support global css inside server component layouts', async () => { describe('server layouts', () => {
const browser = await webdriver(next.url, '/dashboard') it('should support global css inside server layouts', async () => {
const browser = await webdriver(next.url, '/dashboard')
// Should body text in red
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.p')).color`
)
).toBe('rgb(255, 0, 0)')
// Should inject global css for .green selectors
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.green')).color`
)
).toBe('rgb(0, 128, 0)')
})
// Should body text in red it('should support css modules inside server layouts', async () => {
expect( const browser = await webdriver(next.url, '/css/css-nested')
await browser.eval( expect(
`window.getComputedStyle(document.querySelector('.p')).color` await browser.eval(
) `window.getComputedStyle(document.querySelector('#server-cssm')).color`
).toBe('rgb(255, 0, 0)') )
).toBe('rgb(0, 128, 0)')
})
})
// Should inject global css for .green selectors describe.skip('server pages', () => {
expect( it('should support global css inside server pages', async () => {})
await browser.eval( it('should support css modules inside server pages', async () => {})
`window.getComputedStyle(document.querySelector('.green')).color`
)
).toBe('rgb(0, 128, 0)')
}) })
it('should support css modules inside client layouts', async () => { describe('client layouts', () => {
const browser = await webdriver(next.url, '/client-nested') it('should support css modules inside client layouts', async () => {
const browser = await webdriver(next.url, '/client-nested')
// Should render h1 in red // Should render h1 in red
expect( expect(
await browser.eval( await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color` `window.getComputedStyle(document.querySelector('h1')).color`
) )
).toBe('rgb(255, 0, 0)') ).toBe('rgb(255, 0, 0)')
}) })
it('should support css modules inside client pages', async () => { it('should support global css inside client layouts', async () => {
const browser = await webdriver(next.url, '/client-component-route') const browser = await webdriver(next.url, '/client-nested')
// Should render p in red // Should render button in red
expect( expect(
await browser.eval( await browser.eval(
`window.getComputedStyle(document.querySelector('p')).color` `window.getComputedStyle(document.querySelector('button')).color`
) )
).toBe('rgb(255, 0, 0)') ).toBe('rgb(255, 0, 0)')
})
}) })
it('should support global css inside client layouts', async () => { describe('client pages', () => {
const browser = await webdriver(next.url, '/client-nested') it('should support css modules inside client pages', async () => {
const browser = await webdriver(next.url, '/client-component-route')
// Should render button in red // Should render p in red
expect( expect(
await browser.eval( await browser.eval(
`window.getComputedStyle(document.querySelector('button')).color` `window.getComputedStyle(document.querySelector('p')).color`
) )
).toBe('rgb(255, 0, 0)') ).toBe('rgb(255, 0, 0)')
}) })
it('should support global css inside client pages', async () => { it('should support global css inside client pages', async () => {
const browser = await webdriver(next.url, '/client-component-route') const browser = await webdriver(next.url, '/client-component-route')
// Should render `b` in blue // Should render `b` in blue
expect( expect(
await browser.eval( await browser.eval(
`window.getComputedStyle(document.querySelector('b')).color` `window.getComputedStyle(document.querySelector('b')).color`
) )
).toBe('rgb(0, 0, 255)') ).toBe('rgb(0, 0, 255)')
})
}) })
}) })
}) })
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