Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_next_dev_shared_slots.json
Original file line number Diff line number Diff line change
@@ -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"}
64 changes: 64 additions & 0 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => ({
Expand Down
65 changes: 64 additions & 1 deletion packages/next-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
computeCompiledFiles,
createCompatTypes,
createNodeModulesExcludeRegex,
createSharedSlotReservation,
createThemeInterfaceArgs,
type DevupCompiler,
type DevupUIBasePluginOptions,
extractedNeedles,
findTailwindCss,
Expand All @@ -25,6 +27,7 @@ import {
readJsxImportSource,
seedFileNumbers,
type StaticImportGraph,
type WasmImportAliases,
} from '@devup-ui/plugin-utils'
import { type NextConfig } from 'next'

Expand Down Expand Up @@ -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<DevupCompiler, 'reserveClassSlots'>,
{ 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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
Loading