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(
sideEffects: true,
test: regexCssGlobal,
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),
}),
......
......@@ -6,13 +6,15 @@ export default async function transformSource(this: any): Promise<string> {
modules = modules ? [modules] : []
}
return (
modules
.map(
(request: string) => `import(/* webpackMode: "eager" */ '${request}')`
)
.join(';') +
const requests = modules as string[]
const code =
requests
.map((request) => `import(/* webpackMode: "eager" */ '${request}')`)
.join(';\n') +
`
export const __next_rsc_css__ = ${JSON.stringify(
requests.filter((request) => request.endsWith('.css'))
)};
export const __next_rsc__ = {
server: false,
__webpack_require__
......@@ -25,5 +27,6 @@ export default async function transformSource(this: any): Promise<string> {
: ssr
? `export const __N_SSP = true;`
: `export const __N_SSG = true;`)
)
return code
}
......@@ -22,6 +22,7 @@ type Options = {
const PLUGIN_NAME = 'ClientEntryPlugin'
export const injectedClientEntries = new Map()
const regexCssGlobal = /(?<!\.module)\.css$/
export class ClientEntryPlugin {
dev: boolean = false
......@@ -77,11 +78,15 @@ export class ClientEntryPlugin {
const module = compilation.moduleGraph.getResolvedModule(dependency)
if (!module) return
if (visited.has(module.userRequest)) return
visited.add(module.userRequest)
const modRequest = module.userRequest
if (visited.has(modRequest)) return
visited.add(modRequest)
if (clientComponentRegex.test(module.userRequest)) {
clientComponentImports.push(module.userRequest)
if (
clientComponentRegex.test(modRequest) ||
regexCssGlobal.test(modRequest)
) {
clientComponentImports.push(modRequest)
}
compilation.moduleGraph
......@@ -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 = (
webpack as any
).EntryPlugin.createDependency(
clientLoader,
name + NEXT_CLIENT_SSR_ENTRY_SUFFIX
)
).EntryPlugin.createDependency(clientLoader, {
name: name + NEXT_CLIENT_SSR_ENTRY_SUFFIX,
})
promises.push(
new Promise<void>((res, rej) => {
compilation.addEntry(
......
......@@ -9,7 +9,6 @@ import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { FLIGHT_MANIFEST } from '../../../shared/lib/constants'
import { clientComponentRegex } from '../loaders/utils'
import { relative } from 'path'
import { getEntrypointFiles } from './build-manifest-plugin'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
// This is the module that will be used to anchor all client references to.
......@@ -79,13 +78,7 @@ export class FlightManifestPlugin {
mod: any
) {
const resource: string = mod.resource
// 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
}
if (!resource) return
const moduleExports: any = manifest[resource] || {}
const moduleIdMapping: any = manifest.__ssr_module_mapping__ || {}
......@@ -98,6 +91,67 @@ export class FlightManifestPlugin {
if (!ssrNamedModuleId.startsWith('.'))
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 cjsExports = [
...new Set(
......@@ -132,16 +186,36 @@ export class FlightManifestPlugin {
)
.filter((name) => name !== null)
// Get all CSS files imported in that chunk.
const cssChunks: string[] = []
for (const entrypoint of chunk.groupsIterable) {
const files = getEntrypointFiles(entrypoint)
for (const file of files) {
if (file.endsWith('.css')) {
cssChunks.push(file)
}
// Get all CSS files imported from the module's dependencies.
const visitedModule = new Set()
const cssChunks: Set<string> = new Set()
function collectClientImportedCss(module: any) {
if (!module) return
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) => {
let requiredChunks = []
......@@ -167,7 +241,7 @@ export class FlightManifestPlugin {
moduleExports[name] = {
id,
name,
chunks: requiredChunks.concat(cssChunks),
chunks: requiredChunks.concat([...cssChunks]),
}
}
if (!moduleIdMapping[id][name]) {
......
......@@ -30,8 +30,7 @@ __webpack_require__.u = (chunkId: any) => {
self.__next_require__ = __webpack_require__
// eslint-disable-next-line no-undef
// @ts-expect-error TODO: fix type
self.__next_chunk_load__ = (chunk) => {
;(self as any).__next_chunk_load__ = (chunk: string) => {
if (chunk.endsWith('.css')) {
const link = document.createElement('link')
link.rel = 'stylesheet'
......@@ -153,7 +152,52 @@ function useInitialServerResponse(cacheKey: string) {
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)
return newResponse
......
......@@ -16,6 +16,7 @@ import {
renderToInitialStream,
createBufferedTransformStream,
continueFromInitialStream,
createSuffixStream,
} from './node-web-streams-helper'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { tryGetPreviewData } from './api-utils/node'
......@@ -124,7 +125,6 @@ function useFlightResponse(
rscCache.set(id, entry)
let bootstrapped = false
let remainingFlightResponse = ''
const forwardReader = forwardStream.getReader()
const writer = writable.getWriter()
function process() {
......@@ -144,40 +144,11 @@ function useFlightResponse(
writer.close()
} else {
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(
encodeText(
css +
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([1, id, responsePartial])
)})</script>`
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([1, id, responsePartial])
)})</script>`
)
)
process()
......@@ -217,8 +188,9 @@ function createServerComponentRenderer(
globalThis.__next_chunk_load__ = () => Promise.resolve()
}
let RSCStream: ReadableStream<Uint8Array>
const cssFlight = getCssFlight(ComponentMod, serverComponentManifest)
let RSCStream: ReadableStream<Uint8Array>
const createRSCStream = () => {
if (!RSCStream) {
RSCStream = renderToReadableStream(
......@@ -227,7 +199,7 @@ function createServerComponentRenderer(
{
context: serverContexts,
}
)
).pipeThrough(createSuffixStream(cssFlight))
}
return RSCStream
}
......@@ -350,6 +322,30 @@ function getSegmentParam(segment: string): {
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(
req: IncomingMessage,
res: ServerResponse,
......@@ -764,6 +760,7 @@ export async function renderToHTML(
return [actualSegment]
}
const cssFlight = getCssFlight(ComponentMod, serverComponentManifest)
const flightData: FlightData = [
// TODO: change walk to output without ''
walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState).slice(
......@@ -772,9 +769,9 @@ export async function renderToHTML(
]
return new RenderResult(
renderToReadableStream(flightData, serverComponentManifest).pipeThrough(
createBufferedTransformStream()
)
renderToReadableStream(flightData, serverComponentManifest)
.pipeThrough(createSuffixStream(cssFlight))
.pipeThrough(createBufferedTransformStream())
)
}
......
......@@ -125,12 +125,13 @@ export async function loadComponents(
if (hasServerComponents) {
try {
// Make sure to also load the client entry in cache.
await requirePage(
const __client__ = await requirePage(
normalizePagePath(pathname) + NEXT_CLIENT_SSR_ENTRY_SUFFIX,
distDir,
serverless,
appDirEnabled
)
ComponentMod.__client__ = __client__
} catch (_) {
// This page might not be a server component page, so there is no
// client entry to load.
......
import './style.css'
import './global.css'
export default function DashboardLayout(props) {
return (
<>
<h1>Dashboard</h1>
<div className="dangerous-text">
<h1 className="green">Dashboard</h1>
{props.children}
</>
</div>
)
}
export default function DashboardPage(props) {
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', () => {
})
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 () => {
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