Unverified Commit 409d3761 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Enable css import in rsc server side (#38418)

* Enable css in server components

* inject server css into flight

* refactor and fix test

* fix lint

* resolve css from module deps

* fix dev & prod inconsistentce, collect client css

* simplify

* dedupe duplicated css chunks

* remove ssr link injection and css flight
Co-authored-by: 's avatarShu Ding <g@shud.in>
parent f694e6c4
...@@ -360,7 +360,11 @@ export const css = curry(async function css( ...@@ -360,7 +360,11 @@ export const css = curry(async function css(
sideEffects: true, sideEffects: true,
test: regexCssGlobal, test: regexCssGlobal,
issuer: { issuer: {
and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/], or: [
{ and: [ctx.rootDirectory, /\.(js|mjs|jsx|ts|tsx)$/] },
// Also match the virtual client entry which doesn't have file path
(filePath) => !filePath,
],
}, },
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer), use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
}), }),
......
...@@ -6,13 +6,15 @@ export default async function transformSource(this: any): Promise<string> { ...@@ -6,13 +6,15 @@ export default async function transformSource(this: any): Promise<string> {
modules = modules ? [modules] : [] modules = modules ? [modules] : []
} }
return ( const requests = modules as string[]
modules const code =
.map( requests
(request: string) => `import(/* webpackMode: "eager" */ '${request}')` .map((request) => `import(/* webpackMode: "eager" */ '${request}')`)
) .join(';\n') +
.join(';') +
` `
export const __next_rsc_css__ = ${JSON.stringify(
requests.filter((request) => request.endsWith('.css'))
)};
export const __next_rsc__ = { export const __next_rsc__ = {
server: false, server: false,
__webpack_require__ __webpack_require__
...@@ -25,5 +27,6 @@ export default async function transformSource(this: any): Promise<string> { ...@@ -25,5 +27,6 @@ export default async function transformSource(this: any): Promise<string> {
: ssr : ssr
? `export const __N_SSP = true;` ? `export const __N_SSP = true;`
: `export const __N_SSG = true;`) : `export const __N_SSG = true;`)
)
return code
} }
...@@ -22,6 +22,7 @@ type Options = { ...@@ -22,6 +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$/
export class ClientEntryPlugin { export class ClientEntryPlugin {
dev: boolean = false dev: boolean = false
...@@ -77,11 +78,15 @@ export class ClientEntryPlugin { ...@@ -77,11 +78,15 @@ export class ClientEntryPlugin {
const module = compilation.moduleGraph.getResolvedModule(dependency) const module = compilation.moduleGraph.getResolvedModule(dependency)
if (!module) return if (!module) return
if (visited.has(module.userRequest)) return const modRequest = module.userRequest
visited.add(module.userRequest) if (visited.has(modRequest)) return
visited.add(modRequest)
if (clientComponentRegex.test(module.userRequest)) { if (
clientComponentImports.push(module.userRequest) clientComponentRegex.test(modRequest) ||
regexCssGlobal.test(modRequest)
) {
clientComponentImports.push(modRequest)
} }
compilation.moduleGraph compilation.moduleGraph
...@@ -149,13 +154,12 @@ export class ClientEntryPlugin { ...@@ -149,13 +154,12 @@ export class ClientEntryPlugin {
) )
} }
// Inject the entry to the server compiler. // Inject the entry to the server compiler (__sc_client__).
const clientComponentEntryDep = ( const clientComponentEntryDep = (
webpack as any webpack as any
).EntryPlugin.createDependency( ).EntryPlugin.createDependency(clientLoader, {
clientLoader, name: name + NEXT_CLIENT_SSR_ENTRY_SUFFIX,
name + NEXT_CLIENT_SSR_ENTRY_SUFFIX })
)
promises.push( promises.push(
new Promise<void>((res, rej) => { new Promise<void>((res, rej) => {
compilation.addEntry( compilation.addEntry(
......
...@@ -9,7 +9,6 @@ import { webpack, sources } from 'next/dist/compiled/webpack/webpack' ...@@ -9,7 +9,6 @@ import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { FLIGHT_MANIFEST } from '../../../shared/lib/constants' import { FLIGHT_MANIFEST } from '../../../shared/lib/constants'
import { clientComponentRegex } from '../loaders/utils' import { clientComponentRegex } from '../loaders/utils'
import { relative } from 'path' import { relative } from 'path'
import { getEntrypointFiles } from './build-manifest-plugin'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
// This is the module that will be used to anchor all client references to. // This is the module that will be used to anchor all client references to.
...@@ -79,13 +78,7 @@ export class FlightManifestPlugin { ...@@ -79,13 +78,7 @@ export class FlightManifestPlugin {
mod: any mod: any
) { ) {
const resource: string = mod.resource const resource: string = mod.resource
if (!resource) return
// TODO: Hook into deps instead of the target module.
// That way we know by the type of dep whether to include.
// It also resolves conflicts when the same module is in multiple chunks.
if (!resource || !clientComponentRegex.test(resource)) {
return
}
const moduleExports: any = manifest[resource] || {} const moduleExports: any = manifest[resource] || {}
const moduleIdMapping: any = manifest.__ssr_module_mapping__ || {} const moduleIdMapping: any = manifest.__ssr_module_mapping__ || {}
...@@ -98,6 +91,67 @@ export class FlightManifestPlugin { ...@@ -98,6 +91,67 @@ export class FlightManifestPlugin {
if (!ssrNamedModuleId.startsWith('.')) if (!ssrNamedModuleId.startsWith('.'))
ssrNamedModuleId = `./${ssrNamedModuleId}` ssrNamedModuleId = `./${ssrNamedModuleId}`
if (
mod.request &&
/(?<!\.module)\.css$/.test(mod.request) &&
(dev
? mod.loaders.some((item: any) =>
item.loader.includes('next-style-loader/index.js')
)
: mod.loaders.some((item: any) =>
item.loader.includes('mini-css-extract-plugin/loader.js')
))
) {
if (!manifest[resource]) {
if (dev) {
const chunkIdNameMapping = (chunk.ids || []).map((chunkId) => {
return (
chunkId +
':' +
(chunk.name || chunk.id) +
(dev ? '' : '-' + chunk.hash)
)
})
manifest[resource] = {
default: {
id,
name: 'default',
chunks: chunkIdNameMapping,
},
}
moduleIdMapping[id]['default'] = {
id: ssrNamedModuleId,
name: 'default',
chunks: chunkIdNameMapping,
}
manifest.__ssr_module_mapping__ = moduleIdMapping
} else {
const chunks = [...chunk.files].filter((f) => f.endsWith('.css'))
manifest[resource] = {
default: {
id,
name: 'default',
chunks,
},
}
moduleIdMapping[id]['default'] = {
id: ssrNamedModuleId,
name: 'default',
chunks,
}
manifest.__ssr_module_mapping__ = moduleIdMapping
}
}
return
}
// TODO: Hook into deps instead of the target module.
// That way we know by the type of dep whether to include.
// It also resolves conflicts when the same module is in multiple chunks.
if (!clientComponentRegex.test(resource)) {
return
}
const exportsInfo = compilation.moduleGraph.getExportsInfo(mod) const exportsInfo = compilation.moduleGraph.getExportsInfo(mod)
const cjsExports = [ const cjsExports = [
...new Set( ...new Set(
...@@ -132,16 +186,36 @@ export class FlightManifestPlugin { ...@@ -132,16 +186,36 @@ export class FlightManifestPlugin {
) )
.filter((name) => name !== null) .filter((name) => name !== null)
// Get all CSS files imported in that chunk. // Get all CSS files imported from the module's dependencies.
const cssChunks: string[] = [] const visitedModule = new Set()
for (const entrypoint of chunk.groupsIterable) { const cssChunks: Set<string> = new Set()
const files = getEntrypointFiles(entrypoint)
for (const file of files) { function collectClientImportedCss(module: any) {
if (file.endsWith('.css')) { if (!module) return
cssChunks.push(file)
} const modRequest = module.userRequest
if (visitedModule.has(modRequest)) return
visitedModule.add(modRequest)
if (/\.css$/.test(modRequest)) {
// collect relative imported css chunks
compilation.chunkGraph.getModuleChunks(module).forEach((c) => {
;[...c.files]
.filter((file) => file.endsWith('.css'))
.forEach((file) => cssChunks.add(file))
})
} }
const connections = Array.from(
compilation.moduleGraph.getOutgoingConnections(module)
)
connections.forEach((connection) => {
collectClientImportedCss(
compilation.moduleGraph.getResolvedModule(connection.dependency!)
)
})
} }
collectClientImportedCss(mod)
moduleExportedKeys.forEach((name) => { moduleExportedKeys.forEach((name) => {
let requiredChunks = [] let requiredChunks = []
...@@ -167,7 +241,7 @@ export class FlightManifestPlugin { ...@@ -167,7 +241,7 @@ export class FlightManifestPlugin {
moduleExports[name] = { moduleExports[name] = {
id, id,
name, name,
chunks: requiredChunks.concat(cssChunks), chunks: requiredChunks.concat([...cssChunks]),
} }
} }
if (!moduleIdMapping[id][name]) { if (!moduleIdMapping[id][name]) {
......
...@@ -30,8 +30,7 @@ __webpack_require__.u = (chunkId: any) => { ...@@ -30,8 +30,7 @@ __webpack_require__.u = (chunkId: any) => {
self.__next_require__ = __webpack_require__ self.__next_require__ = __webpack_require__
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
// @ts-expect-error TODO: fix type ;(self as any).__next_chunk_load__ = (chunk: string) => {
self.__next_chunk_load__ = (chunk) => {
if (chunk.endsWith('.css')) { if (chunk.endsWith('.css')) {
const link = document.createElement('link') const link = document.createElement('link')
link.rel = 'stylesheet' link.rel = 'stylesheet'
...@@ -153,7 +152,52 @@ function useInitialServerResponse(cacheKey: string) { ...@@ -153,7 +152,52 @@ function useInitialServerResponse(cacheKey: string) {
nextServerDataRegisterWriter(controller) nextServerDataRegisterWriter(controller)
}, },
}) })
const newResponse = createFromReadableStream(readable)
async function loadCss(cssChunkInfoJson: string) {
const data = JSON.parse(cssChunkInfoJson)
await Promise.all(
data.chunks.map((chunkId: string) => {
// load css related chunks
return (self as any).__next_chunk_load__(chunkId)
})
)
// In development mode, import css in dev when it's wrapped by style loader.
// In production mode, css are standalone chunk that doesn't need to be imported.
if (data.id) {
;(self as any).__next_require__(data.id)
}
}
const loadCssFromStreamData = (data: string) => {
const seg = data.split(':')
if (seg[0] === 'CSS') {
loadCss(seg.slice(1).join(':'))
}
}
let buffer = ''
const loadCssFromFlight = new TransformStream({
transform(chunk, controller) {
const data = new TextDecoder().decode(chunk)
buffer += data
let index
while ((index = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, index)
buffer = buffer.slice(index + 1)
loadCssFromStreamData(line)
}
if (!data.startsWith('CSS:')) {
controller.enqueue(chunk)
}
},
flush() {
loadCssFromStreamData(buffer)
},
})
const newResponse = createFromReadableStream(
readable.pipeThrough(loadCssFromFlight)
)
rscCache.set(cacheKey, newResponse) rscCache.set(cacheKey, newResponse)
return newResponse return newResponse
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
renderToInitialStream, renderToInitialStream,
createBufferedTransformStream, createBufferedTransformStream,
continueFromInitialStream, continueFromInitialStream,
createSuffixStream,
} 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'
...@@ -124,7 +125,6 @@ function useFlightResponse( ...@@ -124,7 +125,6 @@ function useFlightResponse(
rscCache.set(id, entry) rscCache.set(id, entry)
let bootstrapped = false let bootstrapped = false
let remainingFlightResponse = ''
const forwardReader = forwardStream.getReader() const forwardReader = forwardStream.getReader()
const writer = writable.getWriter() const writer = writable.getWriter()
function process() { function process() {
...@@ -144,40 +144,11 @@ function useFlightResponse( ...@@ -144,40 +144,11 @@ function useFlightResponse(
writer.close() writer.close()
} else { } else {
const responsePartial = decodeText(value) const responsePartial = decodeText(value)
const css = responsePartial
.split('\n')
.map((partialLine) => {
const line = remainingFlightResponse + partialLine
remainingFlightResponse = ''
try {
const match = line.match(/^M\d+:(.+)/)
if (match) {
return JSON.parse(match[1])
.chunks.filter((chunkId: string) =>
chunkId.endsWith('.css')
)
.map(
(file: string) =>
`<link rel="stylesheet" href="/_next/${file}">`
)
.join('')
}
return ''
} catch (err) {
// The JSON is partial
remainingFlightResponse = line
return ''
}
})
.join('')
writer.write( writer.write(
encodeText( encodeText(
css + `<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString( JSON.stringify([1, id, responsePartial])
JSON.stringify([1, id, responsePartial]) )})</script>`
)})</script>`
) )
) )
process() process()
...@@ -217,8 +188,9 @@ function createServerComponentRenderer( ...@@ -217,8 +188,9 @@ function createServerComponentRenderer(
globalThis.__next_chunk_load__ = () => Promise.resolve() globalThis.__next_chunk_load__ = () => Promise.resolve()
} }
let RSCStream: ReadableStream<Uint8Array> const cssFlight = getCssFlight(ComponentMod, serverComponentManifest)
let RSCStream: ReadableStream<Uint8Array>
const createRSCStream = () => { const createRSCStream = () => {
if (!RSCStream) { if (!RSCStream) {
RSCStream = renderToReadableStream( RSCStream = renderToReadableStream(
...@@ -227,7 +199,7 @@ function createServerComponentRenderer( ...@@ -227,7 +199,7 @@ function createServerComponentRenderer(
{ {
context: serverContexts, context: serverContexts,
} }
) ).pipeThrough(createSuffixStream(cssFlight))
} }
return RSCStream return RSCStream
} }
...@@ -350,6 +322,30 @@ function getSegmentParam(segment: string): { ...@@ -350,6 +322,30 @@ function getSegmentParam(segment: string): {
return null return null
} }
function getCssFlight(ComponentMod: any, serverComponentManifest: any) {
const importedServerCSSFiles: string[] =
ComponentMod.__client__?.__next_rsc_css__ || []
const cssFiles = importedServerCSSFiles.map(
(css) => serverComponentManifest[css].default
)
if (process.env.NODE_ENV === 'development') {
return cssFiles.map((css) => `CSS:${JSON.stringify(css)}`).join('\n')
}
// Multiple css chunks could be merged into one by mini-css-extract-plugin,
// we use a set here to dedupe the css chunks in production.
const cssSet = cssFiles.reduce((res, css) => {
res.add(...css.chunks)
return res
}, new Set())
const cssFlight = Array.from(cssSet)
.map((css) => `CSS:${JSON.stringify({ chunks: [css] })}`)
.join('\n')
return cssFlight
}
export async function renderToHTML( export async function renderToHTML(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -764,6 +760,7 @@ export async function renderToHTML( ...@@ -764,6 +760,7 @@ export async function renderToHTML(
return [actualSegment] return [actualSegment]
} }
const cssFlight = getCssFlight(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(
...@@ -772,9 +769,9 @@ export async function renderToHTML( ...@@ -772,9 +769,9 @@ export async function renderToHTML(
] ]
return new RenderResult( return new RenderResult(
renderToReadableStream(flightData, serverComponentManifest).pipeThrough( renderToReadableStream(flightData, serverComponentManifest)
createBufferedTransformStream() .pipeThrough(createSuffixStream(cssFlight))
) .pipeThrough(createBufferedTransformStream())
) )
} }
......
...@@ -125,12 +125,13 @@ export async function loadComponents( ...@@ -125,12 +125,13 @@ export async function loadComponents(
if (hasServerComponents) { if (hasServerComponents) {
try { try {
// Make sure to also load the client entry in cache. // Make sure to also load the client entry in cache.
await requirePage( const __client__ = await requirePage(
normalizePagePath(pathname) + NEXT_CLIENT_SSR_ENTRY_SUFFIX, normalizePagePath(pathname) + NEXT_CLIENT_SSR_ENTRY_SUFFIX,
distDir, distDir,
serverless, serverless,
appDirEnabled appDirEnabled
) )
ComponentMod.__client__ = __client__
} catch (_) { } catch (_) {
// This page might not be a server component page, so there is no // This page might not be a server component page, so there is no
// client entry to load. // client entry to load.
......
import './style.css'
import './global.css'
export default function DashboardLayout(props) { export default function DashboardLayout(props) {
return ( return (
<> <div className="dangerous-text">
<h1>Dashboard</h1> <h1 className="green">Dashboard</h1>
{props.children} {props.children}
</> </div>
) )
} }
export default function DashboardPage(props) { export default function DashboardPage(props) {
return ( return (
<> <>
<p>hello from app/dashboard</p> <p className="p">hello from app/dashboard</p>
<p className="green">this is green</p>
</> </>
) )
} }
...@@ -393,6 +393,24 @@ describe('app dir', () => { ...@@ -393,6 +393,24 @@ describe('app dir', () => {
}) })
describe('css support', () => { describe('css support', () => {
it('should support global css inside server component 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)')
})
it('should support css modules inside client layouts', async () => { it('should support css modules inside client layouts', async () => {
const browser = await webdriver(next.url, '/client-nested') const browser = await webdriver(next.url, '/client-nested')
......
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