From e4724480f349ca19a07acfe033acb9a167615dd5 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:29:06 +0900 Subject: [PATCH] fix(next-plugin): reserve shared class slots before next dev extracts File numbers are seeded in path order, but a class that several files share still took the next free slot of whichever file Turbopack extracted first. next dev skips the sorted production prewarm and the coordinator extracts in request order, so the same project could get different class names on each start. Under next dev the Turbopack setup now reserves every shared slot with createSharedSlotReservation before the coordinator starts, scanning src, app and pages plus included packages with the coordinator's own filenames and CSS directories, as the webpack, Rsbuild and Vite plugins already do. One-shot builds keep relying on the prewarm, which extracts in path order. A failing reservation falls back to arrival order like the file-number seed. Refs #694 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_next_dev_shared_slots.json | 1 + .../next-plugin/src/__tests__/plugin.test.ts | 56 ++++++++++++++++++ packages/next-plugin/src/plugin.ts | 58 ++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 .changepacks/changepack_log_next_dev_shared_slots.json diff --git a/.changepacks/changepack_log_next_dev_shared_slots.json b/.changepacks/changepack_log_next_dev_shared_slots.json new file mode 100644 index 000000000..daaf9b50e --- /dev/null +++ b/.changepacks/changepack_log_next_dev_shared_slots.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch"},"note":"Reserve shared class slots in path order before next dev extracts, so Turbopack class names no longer depend on request order","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..a3d6a089e 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -1182,6 +1182,62 @@ export const box = style({ color: 'red' })` seedSpy.mockRestore() } }) + it('reserves shared class slots in path order before next dev extracts', () => { + process.env.TURBOPACK = '1' + const reserve = mock() + const reservationSpy = spyOn( + importGraphModule, + 'createSharedSlotReservation', + ).mockReturnValue({ + dependencies: [], + packageManifests: [], + invalidate: mock(), + reserve, + }) + try { + // One-shot builds take their order from the sorted prewarm instead. + DevupUI({}) + expect(reservationSpy).not.toHaveBeenCalled() + + setNodeEnv('development') + DevupUI({}, { include: ['@acme/ui'], singleCss: true }) + expect(reservationSpy).toHaveBeenCalledWith(wasm) + const [discovery, options] = reserve.mock.calls[0] as Parameters< + ReturnType< + typeof importGraphModule.createSharedSlotReservation + >['reserve'] + > + expect(discovery).toMatchObject({ + roots: ['src', 'app', 'pages'].map((dir) => resolve(dir)), + include: ['@acme/ui'], + cwd: process.cwd(), + tsconfigPath: resolve('tsconfig.json'), + package: '@devup-ui/react', + }) + // The coordinator's own filename and CSS directory for each file. + expect(discovery.toId?.(resolve('src', 'a.tsx'))).toBe('src/a.tsx') + expect(options).toMatchObject({ + singleCss: true, + importMainCssInCode: false, + importMainCssInCss: true, + }) + expect( + options.cssDir({ + filename: 'src/app/page.tsx', + physicalPath: resolve('src', 'app', 'page.tsx'), + code: '', + }), + ).toBe('./../../df/devup-ui') + + // A failing reservation leaves extraction in arrival order. + reserve.mockImplementation(() => { + throw new Error('reserve boom') + }) + expect(() => DevupUI({})).not.toThrow() + } finally { + reservationSpy.mockRestore() + } + }) it('should import previous session state on restart', () => { process.env.TURBOPACK = '1' existsSyncSpy diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index aa92f663e..8ab4b75f7 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -15,7 +15,9 @@ import { computeCompiledFiles, createCompatTypes, createNodeModulesExcludeRegex, + createSharedSlotReservation, createThemeInterfaceArgs, + type DevupCompiler, type DevupUIBasePluginOptions, extractedNeedles, loadDevupConfigSync, @@ -24,6 +26,7 @@ import { readJsxImportSource, seedFileNumbers, type StaticImportGraph, + type WasmImportAliases, withTailwindCss, } from '@devup-ui/plugin-utils' import { type NextConfig } from 'next' @@ -135,6 +138,47 @@ function sourceDir(cwd: string): string { : resolve(cwd, 'src') } +interface SharedSlotOptions { + libPackage: string + include: string[] + importAliases: WasmImportAliases + cssDir: string + singleCss: boolean +} + +/** + * Reserve the class slots files share, in path order, with the filenames and + * CSS directories the coordinator extracts them with. + */ +function reserveSharedSlots( + wasm: Pick, + { libPackage, include, importAliases, cssDir, singleCss }: SharedSlotOptions, +): void { + const cwd = process.cwd() + try { + createSharedSlotReservation(wasm).reserve( + { + roots: ['src', 'app', 'pages'].map((dir) => resolve(cwd, dir)), + include, + cwd, + tsconfigPath: resolve(cwd, 'tsconfig.json'), + package: libPackage, + importAliases, + toId: (path) => relative(cwd, path).replaceAll('\\', '/'), + }, + { + singleCss, + importMainCssInCode: false, + importMainCssInCss: true, + cssDir: ({ physicalPath }) => + `./${relative(dirname(physicalPath), cssDir).replaceAll('\\', '/')}`, + }, + ) + } catch { + // Best-effort; shared slots fall back to arrival order. + } +} + /** * Devup UI Next Plugin * @param config @@ -230,7 +274,7 @@ export function DevupUI( } if (requestedAtomHoist) { console.warn( - '[devup-ui] Turbopack atomHoist is unavailable until shared class-slot reservation lands in #752; keeping per-file names, while existing shared slots remain arrival-ordered.', + '[devup-ui] Turbopack atomHoist is unavailable until shared class-slot reservation lands in #752; keeping per-file names.', ) } if (!existsSync(distDir)) @@ -376,6 +420,18 @@ export function DevupUI( } catch { // Best-effort; numbering falls back to arrival order. } + // A class shared by several files takes the next free slot of whichever + // file extracts it first. `next dev` extracts in Turbopack's request + // order, so reserve those slots in path order before any loader runs. + // One-shot builds already extract in path order through the prewarm below. + if (watch) + reserveSharedSlots(wasm, { + libPackage, + include, + importAliases, + cssDir, + singleCss, + }) // Turbopack can request a CSS module before it has scheduled every source // loader. Waiting for a quiet window is not a compilation-complete signal: // a CSS request can itself hold up the next extraction wave. In one-shot