diff --git a/.changepacks/changepack_log_next_turbo_theme_reload.json b/.changepacks/changepack_log_next_turbo_theme_reload.json new file mode 100644 index 000000000..87c256d4f --- /dev/null +++ b/.changepacks/changepack_log_next_turbo_theme_reload.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch"},"note":"Reload the theme under Turbopack next dev when devup.json, a config it extends or the project's Tailwind CSS changes, and re-run the loaders that depend on it","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 125ea63b9..a3973d1f4 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -1651,6 +1651,93 @@ describe('coordinator per-bucket completion', () => { }) }) +describe('coordinator theme reload', () => { + async function start(theme: CoordinatorOptions['theme']) { + const coordinator = startCoordinator(makeOptions({ theme })) + await new Promise((r) => setTimeout(r, 100)) + const port = parseInt( + (writeFileSyncSpy.mock.calls[0] as [string, string])[1], + ) + return { coordinator, port } + } + + function extract(port: number) { + return httpRequest( + port, + 'POST', + '/extract', + JSON.stringify({ + filename: 'src/A.tsx', + code: 'c', + resourcePath: join(process.cwd(), 'src', 'A.tsx'), + }), + ) + } + + it('registers the theme again once one of its files changes', async () => { + codeExtractSpy.mockReturnValue({ + code: 'code', + map: undefined, + cssFile: undefined, + updatedBaseStyle: false, + free: mock(), + [Symbol.dispose]: mock(), + }) + getCssSpy.mockReturnValue('css') + const devupFile = join(tmpDir, 'theme-devup.json') + const extended = join(tmpDir, 'theme-extended.json') + await Bun.write(devupFile, '{}') + // `extended` does not exist yet: creating it is a change too. + const reload = mock(() => [devupFile, extended]) + const { coordinator, port } = await start({ + files: [devupFile, extended], + reload, + }) + + await httpRequest(port, 'GET', '/css') + await extract(port) + expect(reload).not.toHaveBeenCalled() + + await Bun.write(devupFile, '{"theme":{"colors":{}}}') + expect((await httpRequest(port, 'GET', '/css')).status).toBe(200) + expect(reload).toHaveBeenCalledTimes(1) + await httpRequest(port, 'GET', '/css') + expect(reload).toHaveBeenCalledTimes(1) + + await Bun.write(extended, '{}') + expect((await extract(port)).status).toBe(200) + expect(reload).toHaveBeenCalledTimes(2) + + coordinator.close() + }) + + it('reports a theme that fails to load and retries on the next request', async () => { + const devupFile = join(tmpDir, 'theme-broken.json') + await Bun.write(devupFile, '{}') + const reload = mock((): string[] => { + throw new Error('invalid theme') + }) + const { coordinator, port } = await start({ files: [devupFile], reload }) + + await Bun.write(devupFile, '{"theme":1}') + const css = await httpRequest(port, 'GET', '/css') + expect(css).toEqual({ status: 500, body: 'invalid theme' }) + reload.mockImplementation(() => { + throw 'still invalid' + }) + expect(await httpRequest(port, 'GET', '/css')).toEqual({ + status: 500, + body: 'still invalid', + }) + const extracted = await extract(port) + expect(extracted.status).toBe(500) + expect(JSON.parse(extracted.body)).toEqual({ error: 'still invalid' }) + expect(reload).toHaveBeenCalledTimes(3) + + coordinator.close() + }) +}) + describe('coordinator deleted sources', () => { it('forgets the files deleted since when a file is extracted again', async () => { // Given: three extracted files, two of which are deleted later diff --git a/packages/next-plugin/src/__tests__/css-loader.test.ts b/packages/next-plugin/src/__tests__/css-loader.test.ts index a9fa200a0..8d43b8e88 100644 --- a/packages/next-plugin/src/__tests__/css-loader.test.ts +++ b/packages/next-plugin/src/__tests__/css-loader.test.ts @@ -183,6 +183,7 @@ describe('devupUICssLoader', () => { p === portFile ? String(port) : '{}', ) + const addDependency = mock() const result = await new Promise((resolve, reject) => { const asyncCallback = mock((err: Error | null, content?: string) => { if (err) reject(err) @@ -191,10 +192,12 @@ describe('devupUICssLoader', () => { devupUICssLoader.bind({ async: () => asyncCallback, addContextDependency: mock(), + addDependency, getOptions: () => ({ ...defaultOptions, watch: true, coordinatorPortFile: portFile, + themeFiles: ['/project/devup.json', '/project/app.css'], }), resourcePath: 'devup-ui-1.css', } as unknown as CssLoaderThis)( @@ -205,6 +208,11 @@ describe('devupUICssLoader', () => { }) expect(result).toBe('.a{color:red}') + // A theme edit re-fetches the sheet with its new variables. + expect(addDependency.mock.calls).toEqual([ + ['/project/devup.json'], + ['/project/app.css'], + ]) // NO WASM functions should be called expect(getCssSpy).not.toHaveBeenCalled() diff --git a/packages/next-plugin/src/__tests__/loader.test.ts b/packages/next-plugin/src/__tests__/loader.test.ts index 6eb97ec17..15b5a008a 100644 --- a/packages/next-plugin/src/__tests__/loader.test.ts +++ b/packages/next-plugin/src/__tests__/loader.test.ts @@ -599,6 +599,7 @@ export const a =
` watch: true, singleCss: true, coordinatorPortFile: 'coordinator.port', + themeFiles: [resolve('devup.json'), resolve('base.json')], }), async: mock().mockReturnValue(asyncCallback), resourcePath: join(process.cwd(), 'src', 'App.tsx'), @@ -618,6 +619,9 @@ export const a =
` ) }) + // A theme edit re-runs the loader under Turbopack. + expect(t.addDependency).toHaveBeenCalledWith(resolve('devup.json')) + expect(t.addDependency).toHaveBeenCalledWith(resolve('base.json')) expect(t.addDependency).toHaveBeenCalledWith(resolve('src/tokens.ts')) expect(t.addDependency).not.toHaveBeenCalledWith(resolve('1')) diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 096069c64..a4437be4a 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -712,6 +712,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: {}, defaultClassMap: {}, defaultFileMap: {}, @@ -739,6 +740,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], watch: false, singleCss: false, theme: {}, @@ -795,6 +797,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: {}, defaultClassMap: {}, defaultFileMap: {}, @@ -849,6 +852,7 @@ describe('DevupUINextPlugin', () => { properties: {}, }, themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], }, }, ], @@ -886,6 +890,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: 'theme', defaultClassMap: {}, defaultFileMap: {}, @@ -940,6 +945,7 @@ describe('DevupUINextPlugin', () => { properties: {}, }, themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], }, }, ], @@ -1277,7 +1283,18 @@ export const box = style({ color: 'red' })` prewarmedFiles: [], prewarmedOutputs: expect.any(Map), sourceMap: true, + theme: { + files: [resolve('devup.json')], + reload: expect.any(Function), + }, }) + // The coordinator registers an edited theme through the same path. + const { theme } = startCoordinatorSpy.mock.calls[0]![0] as { + theme: { reload: () => string[] } + } + registerThemeSpy.mockClear() + expect(theme.reload()).toEqual([resolve('devup.json')]) + expect(registerThemeSpy).toHaveBeenCalledWith({}) expect(codeExtractSpy).not.toHaveBeenCalled() expect(codeExtractWithoutSourceMapSpy).not.toHaveBeenCalled() @@ -1300,6 +1317,35 @@ export const box = style({ color: 'red' })` } }) + it('registers the Tailwind CSS theme and watches its files', () => { + process.env.TURBOPACK = '1' + const tailwindSpy = spyOn( + importGraphModule, + 'findTailwindCss', + ).mockReturnValue({ + file: resolve('app.css'), + files: [resolve('app.css'), resolve('tokens.css')], + css: '@import "tailwindcss";', + }) + try { + const config = DevupUI({}) + + expect(registerThemeSpy).toHaveBeenCalledWith({ + tailwindCss: '@import "tailwindcss";', + }) + const rule = config.turbopack?.rules?.['*.{tsx,ts,jsx,js,mjs}'] as { + loaders: { options: { themeFiles: string[] } }[] + } + expect(rule.loaders[0].options.themeFiles).toEqual([ + resolve('devup.json'), + resolve('app.css'), + resolve('tokens.css'), + ]) + } finally { + tailwindSpy.mockRestore() + } + }) + it('hands the coordinator the full compiled-file set, not the static-only route map', () => { process.env.TURBOPACK = '1' process.env.DEVUP_UI_PROFILE = '1' diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 22266f499..4f4d0ada0 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -1,4 +1,4 @@ -import { existsSync, writeFile, writeFileSync } from 'node:fs' +import { existsSync, statSync, writeFile, writeFileSync } from 'node:fs' import { createServer, type IncomingMessage, type Server } from 'node:http' import { basename, dirname, join, relative } from 'node:path' @@ -67,6 +67,16 @@ export interface CoordinatorOptions { * open. Defaults to 60000. Exposed for tests; the plugin omits it. */ maxWaitMs?: number + /** + * Development only: the files the registered theme was read from, and how + * to register it again. Before each `/css` and `/extract` the coordinator + * re-registers the theme once one of those files changed, since Next does + * not reload next.config for devup.json. `reload` returns the new file list. + */ + theme?: { + files: string[] + reload: () => string[] + } } export interface PrewarmedOutput { @@ -172,6 +182,36 @@ function readBody(req: IncomingMessage): Promise { let server: Server | null = null +interface ThemeWatch { + files: string[] + stamp: string + reload: () => string[] +} + +let themeWatch: ThemeWatch | undefined + +/** What changes when a theme input is edited, added or deleted. */ +function themeStamp(files: string[]): string { + return files + .map((file) => { + try { + const stats = statSync(file) + return `${stats.mtimeMs}:${stats.size}` + } catch { + return 'missing' + } + }) + .join('|') +} + +/** Register the theme again when one of its input files has changed. */ +function refreshTheme(): void { + if (!themeWatch || themeStamp(themeWatch.files) === themeWatch.stamp) return + // A throwing reload keeps the old stamp, so the next request retries. + const files = themeWatch.reload() + themeWatch = { ...themeWatch, files, stamp: themeStamp(files) } +} + // Extraction tracking for waitForIdle. // // The CSS loader fetches the live sheet from `/css?waitForIdle=true`. In @@ -394,6 +434,10 @@ export function startCoordinator(options: CoordinatorOptions): { for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) extractedSources.clear() fileNumToBucket.clear() + themeWatch = options.theme && { + ...options.theme, + stamp: themeStamp(options.theme.files), + } // A deleted file is never extracted again, so its styles would stay in the // sheet; its importers are, once they stop importing it. A repeated @@ -433,6 +477,14 @@ export function startCoordinator(options: CoordinatorOptions): { const shouldWait = url.searchParams.get('waitForIdle') === 'true' const fileNum = fileNumParam != null ? parseInt(fileNumParam) : undefined + try { + refreshTheme() + } catch (error) { + res.writeHead(500, { 'Content-Type': 'text/plain' }) + res.end(error instanceof Error ? error.message : String(error)) + return + } + if (shouldWait) { if (fileNum != null && fileNumToBucket.has(fileNum)) { // Deterministic: block until every member of this collapsed bucket @@ -479,6 +531,7 @@ export function startCoordinator(options: CoordinatorOptions): { resourcePath: string } extractedFilename = filename + refreshTheme() const repeated = extractedSources.has(filename) extractedSources.set(filename, resourcePath) @@ -738,6 +791,7 @@ export const resetCoordinator = () => { bucketToMembers = new Map() canonicalMapRef = {} expectedBaseFiles = new Set() + themeWatch = undefined writeChain.clear() latestContent.clear() } diff --git a/packages/next-plugin/src/css-loader.ts b/packages/next-plugin/src/css-loader.ts index 5a414c8cd..6ce0cae5c 100644 --- a/packages/next-plugin/src/css-loader.ts +++ b/packages/next-plugin/src/css-loader.ts @@ -25,6 +25,8 @@ export interface DevupUICssLoaderOptions { classMapFile: string fileMapFile: string themeFile: string + /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + themeFiles?: string[] theme?: object defaultSheet: object defaultClassMap: object @@ -86,6 +88,7 @@ const devupUICssLoader: RawLoaderDefinitionFunction = classMapFile, fileMapFile, themeFile, + themeFiles = [], theme, defaultClassMap, defaultFileMap, @@ -94,6 +97,8 @@ const devupUICssLoader: RawLoaderDefinitionFunction = // Coordinator mode: fetch live CSS from coordinator (disk content is stale) if (coordinatorPortFile) { + // The sheet holds the theme's variables: re-run when the theme changes. + for (const file of themeFiles) this.addDependency(file) const callback = this.async() // Check both resourcePath and resourceQuery for ?fileNum=N // Turbopack may embed query in resourcePath or provide it via resourceQuery diff --git a/packages/next-plugin/src/loader.ts b/packages/next-plugin/src/loader.ts index 917b43c87..81ee44e3b 100644 --- a/packages/next-plugin/src/loader.ts +++ b/packages/next-plugin/src/loader.ts @@ -33,6 +33,8 @@ export interface DevupUILoaderOptions { classMapFile: string fileMapFile: string themeFile: string + /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + themeFiles?: string[] watch: boolean singleCss: boolean coordinatorPortFile?: string @@ -156,6 +158,7 @@ const devupUILoader: RawLoaderDefinitionFunction = singleCss, coordinatorPortFile, theme, + themeFiles = [], defaultClassMap, defaultFileMap, defaultSheet, @@ -165,6 +168,9 @@ const devupUILoader: RawLoaderDefinitionFunction = // Coordinator mode: delegate to HTTP server if (coordinatorPortFile) { this.addDependency(coordinatorPortFile) + // Extraction reads theme tokens: re-run when the theme changes (the + // coordinator registers the new theme before it answers). + for (const file of themeFiles) this.addDependency(file) const callback = this.async() const tryCoordinator = (retries: number) => { if (!existsSync(coordinatorPortFile)) { diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index 21aa6042a..eed5c86f4 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -18,13 +18,13 @@ import { createThemeInterfaceArgs, type DevupUIBasePluginOptions, extractedNeedles, + findTailwindCss, loadDevupConfigSync, mergeImportAliases, normalizeShorthands, readJsxImportSource, seedFileNumbers, type StaticImportGraph, - withTailwindCss, } from '@devup-ui/plugin-utils' import { type NextConfig } from 'next' @@ -282,18 +282,27 @@ export function DevupUI( // No previous session state (first run) or corrupt files, start fresh } - const devupConfig = loadDevupConfigSync(devupFile) - - const theme: any = withTailwindCss(devupConfig.theme ?? {}, devupConfig) // Register current theme after importing previous state, // since importSheet replaces the entire sheet including its theme. - registerTheme(theme) - const themeInterface = getThemeInterface( - ...createThemeInterfaceArgs(libPackage), - ) - // Written even when empty, so tokens removed from the theme leave no - // stale declarations behind. - writeFileSync(join(distDir, 'theme.d.ts'), themeInterface) + // Returns every file the theme was read from: devup.json, the configs it + // extends and the project's Tailwind CSS. + const registerProjectTheme = () => { + const inputs = new Set() + const devupConfig = loadDevupConfigSync(devupFile, inputs) + const tailwind = findTailwindCss(devupConfig) + const theme: any = tailwind + ? { ...devupConfig.theme, tailwindCss: tailwind.css } + : (devupConfig.theme ?? {}) + registerTheme(theme) + const themeInterface = getThemeInterface( + ...createThemeInterfaceArgs(libPackage), + ) + // Written even when empty, so tokens removed from the theme leave no + // stale declarations behind. + writeFileSync(join(distDir, 'theme.d.ts'), themeInterface) + return { theme, files: [...inputs, ...(tailwind?.files ?? [])] } + } + const { theme, files: themeFiles } = registerProjectTheme() // disable turbo parallel const excludeRegex = createNodeModulesExcludeRegex(include, '.mdx.[tj]sx?$') @@ -499,6 +508,11 @@ export function DevupUI( prewarmedFiles, prewarmedOutputs, sourceMap, + // Next does not reload next.config when devup.json changes, so under + // `next dev` the coordinator registers the theme again itself. + theme: watch + ? { files: themeFiles, reload: () => registerProjectTheme().files } + : undefined, }) // Cleanup on exit @@ -575,6 +589,7 @@ export function DevupUI( classMapFile, fileMapFile, themeFile: devupFile, + themeFiles, defaultSheet, defaultClassMap, defaultFileMap, @@ -600,6 +615,7 @@ export function DevupUI( classMapFile, fileMapFile, themeFile: devupFile, + themeFiles, defaultSheet, defaultClassMap, defaultFileMap,