From c30f14fc46da0e778f236926cafec64bda9e0206 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:43:02 +0900 Subject: [PATCH] perf(next-plugin): cache the coordinator's latest extraction per file Next compiles a module that both the server and the client graph import once per graph, with the same source. In next dev nothing was prewarmed, so every /extract ran the engine again and rewrote the CSS and state files even though the styles were already in the sheet. The coordinator now keeps the latest extraction of each file with its source, CSS directory and the mtime and size of the files the extraction read through the module resolver. A request with the same source and CSS directory whose dependencies are unchanged is answered from it and, like a prewarm hit, writes nothing. Only the latest source per file is kept, so the cache is bounded by the file count and a hit always means the sheet holds exactly that source's styles; going back to an earlier source extracts again. A failing extraction drops the file's entry. Refs #686 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_next_transform_cache.json | 1 + .../src/__tests__/coordinator.test.ts | 113 ++++++++++++++++++ packages/next-plugin/src/coordinator.ts | 93 +++++++++++--- 3 files changed, 190 insertions(+), 17 deletions(-) create mode 100644 .changepacks/changepack_log_next_transform_cache.json diff --git a/.changepacks/changepack_log_next_transform_cache.json b/.changepacks/changepack_log_next_transform_cache.json new file mode 100644 index 000000000..992878f9d --- /dev/null +++ b/.changepacks/changepack_log_next_transform_cache.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch"},"note":"Answer a repeated Turbopack extraction of an unchanged module (the server and client compiles of one file) from the coordinator's per-file transform cache instead of running the engine again","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/next-plugin/src/__tests__/coordinator.test.ts b/packages/next-plugin/src/__tests__/coordinator.test.ts index 7d346a6ba..058dd2259 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -1650,3 +1650,116 @@ describe('coordinator per-bucket completion', () => { coordinator.close() }) }) + +describe('coordinator transform cache', () => { + function extractResult(code: string, dependencies: string[] = []) { + return { + code, + map: undefined, + cssFile: 'devup-ui-1.css', + updatedBaseStyle: false, + dependencies, + free: mock(), + [Symbol.dispose]: mock(), + } + } + + async function start() { + const coordinator = startCoordinator(makeOptions()) + await new Promise((r) => setTimeout(r, 100)) + const port = parseInt( + (writeFileSyncSpy.mock.calls[0] as [string, string])[1], + ) + return { coordinator, port } + } + + async function extract( + port: number, + code: string, + resourcePath = join(process.cwd(), 'src', 'A.tsx'), + ) { + const res = await httpRequest( + port, + 'POST', + '/extract', + JSON.stringify({ filename: 'src/A.tsx', code, resourcePath }), + ) + return { status: res.status, ...JSON.parse(res.body) } + } + + it('answers the second compile of an unchanged module without WASM', async () => { + getCssSpy.mockReturnValue('css') + exportSheetSpy.mockReturnValue('{}') + exportClassMapSpy.mockReturnValue('{}') + exportFileMapSpy.mockReturnValue('{}') + codeExtractSpy.mockImplementation((_filename: string, code: string) => + extractResult(`out:${code}`), + ) + const { coordinator, port } = await start() + + // Server and client graphs compile the same source. + expect((await extract(port, 'v1')).code).toBe('out:v1') + const writes = writeFileSpy.mock.calls.length + expect(await extract(port, 'v1')).toMatchObject({ + status: 200, + code: 'out:v1', + cssFile: 'devup-ui-1.css', + }) + expect(codeExtractSpy).toHaveBeenCalledTimes(1) + // Nothing changed, so nothing is written again. + expect(writeFileSpy.mock.calls.length).toBe(writes) + + // An edit extracts again, and so does going back: the sheet then holds + // the edit's styles, not the first source's. + await extract(port, 'v2') + await extract(port, 'v1') + expect(codeExtractSpy).toHaveBeenCalledTimes(3) + + // The CSS directory is part of the output. + await extract(port, 'v1', join(process.cwd(), 'src', 'nested', 'A.tsx')) + expect(codeExtractSpy).toHaveBeenCalledTimes(4) + + coordinator.close() + }) + + it('extracts again once a file the extraction read changes', async () => { + const tokens = join(tmpDir, 'cache-tokens.ts') + const later = join(tmpDir, 'cache-later.ts') + await Bun.write(tokens, 'export const a = 1') + codeExtractSpy.mockImplementation(() => + extractResult('out', [tokens, later]), + ) + const { coordinator, port } = await start() + + await extract(port, 'src') + await extract(port, 'src') + expect(codeExtractSpy).toHaveBeenCalledTimes(1) + + await Bun.write(tokens, 'export const a = 22') + await extract(port, 'src') + expect(codeExtractSpy).toHaveBeenCalledTimes(2) + + // A dependency that did not exist and now does counts as a change too. + await Bun.write(later, 'export const b = 1') + await extract(port, 'src') + expect(codeExtractSpy).toHaveBeenCalledTimes(3) + + coordinator.close() + }) + + it('never answers from a cache entry an extraction error replaced', async () => { + codeExtractSpy.mockImplementation(() => extractResult('out')) + const { coordinator, port } = await start() + + await extract(port, 'src') + codeExtractSpy.mockImplementationOnce(() => { + throw new Error('parse error') + }) + // Same filename, new source that fails. + expect((await extract(port, 'broken')).status).toBe(500) + await extract(port, 'src') + expect(codeExtractSpy).toHaveBeenCalledTimes(3) + + coordinator.close() + }) +}) diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 892f8287d..03501252f 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -1,6 +1,6 @@ -import { writeFile, writeFileSync } from 'node:fs' +import { statSync, writeFile, writeFileSync } from 'node:fs' import { createServer, type IncomingMessage, type Server } from 'node:http' -import { basename, dirname, join, relative } from 'node:path' +import { basename, dirname, join, relative, resolve } from 'node:path' import { getFileNumByFilename } from '@devup-ui/plugin-utils' @@ -172,6 +172,48 @@ function readBody(req: IncomingMessage): Promise { let server: Server | null = null +// The latest extraction of each file. Next compiles a module that both the +// server and the client graph import once per graph, with the same source, +// and in no guaranteed order. Re-running WASM for the second copy only +// rebuilds the same styles, so it answers from here instead. Keeping only the +// latest source per file bounds the cache by the project's file count and +// guarantees the sheet still holds exactly that source's styles. +interface CachedExtract { + code: string + relCssDir: string + /** mtime and size of the files the extraction read through the resolver */ + dependencies: string + output: ExtractOutputSnapshot +} + +const transformCache = new Map() + +function dependencyStamp(dependencies: string[] = []): string { + return dependencies + .map((dependency) => { + try { + const stats = statSync(resolve(dependency)) + return `${stats.mtimeMs}:${stats.size}` + } catch { + return 'missing' + } + }) + .join('|') +} + +function cachedExtract( + filename: string, + code: string, + relCssDir: string, +): ExtractOutputSnapshot | undefined { + const cached = transformCache.get(filename) + return cached?.code === code && + cached.relCssDir === relCssDir && + cached.dependencies === dependencyStamp(cached.output.dependencies) + ? cached.output + : undefined +} + // Extraction tracking for waitForIdle. // // The CSS loader fetches the live sheet from `/css?waitForIdle=true`. In @@ -390,6 +432,7 @@ export function startCoordinator(options: CoordinatorOptions): { extractedFiles.clear() for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) fileNumToBucket.clear() + transformCache.clear() const coordinatorServer = createServer(async (req, res) => { const url = new URL(req.url ?? '/', `http://${req.headers.host}`) @@ -466,22 +509,37 @@ export function startCoordinator(options: CoordinatorOptions): { // Require exact source equality because Turbopack may hand a loader // code modified by an earlier transform. const prewarmed = prewarmedOutputs.get(filename) - const cacheHit = prewarmed?.source === code const extractStartedAt = profileStart() - const result = cacheHit - ? prewarmed - : takeExtractOutput( - extract( - filename, - code, - libPackage, - relCssDir, - singleCss, - false, - true, - importAliases, - ), - ) + const cached = + prewarmed?.source === code + ? prewarmed + : cachedExtract(filename, code, relCssDir) + const cacheHit = cached !== undefined + let result: ExtractOutputSnapshot + if (cached) { + result = cached + } else { + // Whatever the engine kept of an earlier source is gone if this throws. + transformCache.delete(filename) + result = takeExtractOutput( + extract( + filename, + code, + libPackage, + relCssDir, + singleCss, + false, + true, + importAliases, + ), + ) + transformCache.set(filename, { + code, + relCssDir, + dependencies: dependencyStamp(result.dependencies), + output: result, + }) + } const extractDurationMs = elapsedMs(extractStartedAt) // When singleCss=false, rewrite per-file CSS imports so Turbopack can resolve them. @@ -708,6 +766,7 @@ export const resetCoordinator = () => { bucketToMembers = new Map() canonicalMapRef = {} expectedBaseFiles = new Set() + transformCache.clear() writeChain.clear() latestContent.clear() }