From 3320366221db17edeae88559350745227455e8e3 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:40:16 +0900 Subject: [PATCH] fix(next-plugin): reload the Turbopack theme when devup.json changes Under Turbopack the theme was registered once, while next.config was evaluated. Next does not reload next.config for devup.json, and in coordinator mode neither loader depended on the theme files, so an edited theme reached neither the extracted classes nor the sheet's variables until the dev server restarted. Setup now collects every file the theme is read from: devup.json, the configs it extends and the project's Tailwind CSS. Both loaders add them as dependencies in coordinator mode, so Turbopack re-runs them when one changes. Under next dev the coordinator gets the same list and a reload callback: before each /css and /extract it compares the files' mtime and size and, when they changed, registers the theme again and rewrites theme.d.ts. A theme that fails to load answers the request with its error and is retried on the next one. Refs #686 Co-Authored-By: Claude Opus 5.5 --- ...hangepack_log_next_turbo_theme_reload.json | 1 + .../src/__tests__/coordinator.test.ts | 87 +++++++++++++++++++ .../src/__tests__/css-loader.test.ts | 8 ++ .../next-plugin/src/__tests__/loader.test.ts | 4 + .../next-plugin/src/__tests__/plugin.test.ts | 46 ++++++++++ packages/next-plugin/src/coordinator.ts | 56 +++++++++++- packages/next-plugin/src/css-loader.ts | 5 ++ packages/next-plugin/src/loader.ts | 6 ++ packages/next-plugin/src/plugin.ts | 36 +++++--- 9 files changed, 238 insertions(+), 11 deletions(-) create mode 100644 .changepacks/changepack_log_next_turbo_theme_reload.json 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 7d346a6ba..61ca886da 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -1650,3 +1650,90 @@ describe('coordinator per-bucket completion', () => { coordinator.close() }) }) + +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() + }) +}) 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 639d7cfe3..eb23fcebb 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -708,6 +708,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: {}, defaultClassMap: {}, defaultFileMap: {}, @@ -735,6 +736,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: {}, @@ -796,6 +798,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: {}, defaultClassMap: {}, defaultFileMap: {}, @@ -855,6 +858,7 @@ describe('DevupUINextPlugin', () => { properties: {}, }, themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], }, }, ], @@ -892,6 +896,7 @@ describe('DevupUINextPlugin', () => { classMapFile: join('df', 'classMap.json'), fileMapFile: join('df', 'fileMap.json'), themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], theme: 'theme', defaultClassMap: {}, defaultFileMap: {}, @@ -951,6 +956,7 @@ describe('DevupUINextPlugin', () => { properties: {}, }, themeFile: 'devup.json', + themeFiles: [resolve('devup.json')], }, }, ], @@ -1288,7 +1294,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() @@ -1311,6 +1328,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 892f8287d..fb49a0c94 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -1,4 +1,4 @@ -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' @@ -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 @@ -390,6 +430,10 @@ export function startCoordinator(options: CoordinatorOptions): { extractedFiles.clear() for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) fileNumToBucket.clear() + themeWatch = options.theme && { + ...options.theme, + stamp: themeStamp(options.theme.files), + } const coordinatorServer = createServer(async (req, res) => { const url = new URL(req.url ?? '/', `http://${req.headers.host}`) @@ -407,6 +451,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 @@ -453,6 +505,7 @@ export function startCoordinator(options: CoordinatorOptions): { resourcePath: string } extractedFilename = filename + refreshTheme() let relCssDir = relative(dirname(resourcePath), cssDir).replaceAll( '\\', @@ -708,6 +761,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 aa92f663e..c30c74e54 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), - ) - if (themeInterface) { - 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), + ) + if (themeInterface) { + 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,