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 e495fca8e..4cc97db5b 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -1204,6 +1204,70 @@ 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') + }) + const warnSpy = spyOn(console, 'warn').mockImplementation(() => {}) + try { + expect(() => DevupUI({})).not.toThrow() + expect(warnSpy).toHaveBeenCalledWith( + '[devup-ui] Turbopack shared class-slot reservation failed: reserve boom. Classes several files share take their names in the order `next dev` extracts them, so they can differ between dev sessions.', + ) + } finally { + warnSpy.mockRestore() + } + } finally { + reservationSpy.mockRestore() + } + }) describe('restoreSessionState', () => { const files = ['sheet.json', 'classMap.json', 'fileMap.json'] as const const engine = () => ({ diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index 4ec180fb6..c46b0aa7e 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, findTailwindCss, @@ -25,6 +27,7 @@ import { readJsxImportSource, seedFileNumbers, type StaticImportGraph, + type WasmImportAliases, } from '@devup-ui/plugin-utils' import { type NextConfig } from 'next' @@ -172,6 +175,54 @@ export function restoreSessionState( } } +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 (error) { + // Best-effort; shared slots fall back to arrival order. + console.warn( + setupWarning( + 'shared class-slot reservation', + error, + 'Classes several files share take their names in the order `next dev` extracts them, so they can differ between dev sessions.', + ), + ) + } +} + /** * Devup UI Next Plugin * @param config @@ -269,7 +320,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)) @@ -432,6 +483,18 @@ export function DevupUI( ), ) } + // 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