From 187d957fc33b575e01f46ef3220d3e4c515a5b63 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Thu, 19 Nov 2026 15:20:39 +0900 Subject: [PATCH 1/3] perf(next-plugin): keep the setup state out of the serialized Turbopack config Next serializes the resolved next.config into .next/required-server-files.{js,json}, which ships with the app. Every Turbopack loader call runs in coordinator mode, so the rules now pass only the port file, the theme files (and watch for the CSS loader) plus a short hash of the state they used to carry, which still re-keys Turbopack's loader cache when that state changes. Co-Authored-By: Claude Opus 5.5 --- ...pack_log_turbo_compact_loader_options.json | 7 + .../next-plugin/src/__tests__/plugin.test.ts | 175 ++++-------------- packages/next-plugin/src/css-loader.ts | 56 ++++-- packages/next-plugin/src/loader.ts | 63 ++++--- packages/next-plugin/src/plugin.ts | 78 +++----- 5 files changed, 153 insertions(+), 226 deletions(-) create mode 100644 .changepacks/changepack_log_turbo_compact_loader_options.json diff --git a/.changepacks/changepack_log_turbo_compact_loader_options.json b/.changepacks/changepack_log_turbo_compact_loader_options.json new file mode 100644 index 000000000..33424c731 --- /dev/null +++ b/.changepacks/changepack_log_turbo_compact_loader_options.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/next-plugin/package.json": "Patch" + }, + "note": "Pass the Turbopack loaders only what coordinator mode reads (the port file, the theme files and, for the CSS loader, watch), plus a short key of the setup state that re-runs them when it changes. Next serializes the resolved config into .next/required-server-files.{js,json}, which ships with the app; it no longer carries the default sheet, class map, file map, theme or file paths. CSS and JavaScript output is unchanged.", + "date": "2026-11-19T00:00:00.000Z" +} diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index dbe442a52..29e82b563 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -809,25 +809,10 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/css-loader', options: { - engine: 'native', - watch: false, coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], - theme: {}, - defaultClassMap: {}, - defaultFileMap: {}, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, + watch: false, + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -836,36 +821,9 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/loader', options: { - engine: 'native', - package: '@devup-ui/react', - cssDir: resolve('df', 'devup-ui'), coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], - watch: false, - singleCss: false, - theme: {}, - defaultClassMap: {}, - defaultFileMap: {}, - importAliases: { - '@emotion/react': null, - '@emotion/styled': 'styled', - '@emotion/css': null, - '@vanilla-extract/css': null, - '@vanilla-extract/css-utils': null, - 'styled-components': 'styled', - }, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -895,25 +853,10 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/css-loader', options: { - engine: 'native', - watch: false, coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], - theme: {}, - defaultClassMap: {}, - defaultFileMap: {}, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, + watch: false, + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -929,36 +872,9 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/loader', options: { - engine: 'native', - package: '@devup-ui/react', - cssDir: resolve('df', 'devup-ui'), coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - importAliases: { - '@emotion/react': null, - '@emotion/styled': 'styled', - '@emotion/css': null, - '@vanilla-extract/css': null, - '@vanilla-extract/css-utils': null, - 'styled-components': 'styled', - }, - watch: false, - singleCss: false, - theme: {}, - defaultClassMap: {}, - defaultFileMap: {}, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -989,25 +905,10 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/css-loader', options: { - engine: 'native', - watch: false, coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], - theme: 'theme', - defaultClassMap: {}, - defaultFileMap: {}, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, + watch: false, + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -1023,36 +924,9 @@ describe('DevupUINextPlugin', () => { { loader: '@devup-ui/next-plugin/loader', options: { - engine: 'native', - package: '@devup-ui/react', - cssDir: resolve('df', 'devup-ui'), coordinatorPortFile: join('df', 'coordinator.port'), - sheetFile: join('df', 'sheet.json'), - classMapFile: join('df', 'classMap.json'), - fileMapFile: join('df', 'fileMap.json'), - watch: false, - singleCss: false, - theme: 'theme', - defaultClassMap: {}, - defaultFileMap: {}, - importAliases: { - '@emotion/react': null, - '@emotion/styled': 'styled', - '@emotion/css': null, - '@vanilla-extract/css': null, - '@vanilla-extract/css-utils': null, - 'styled-components': 'styled', - }, - defaultSheet: { - css: {}, - font_faces: {}, - global_css_files: [], - imports: {}, - keyframes: {}, - properties: {}, - }, - themeFile: 'devup.json', themeFiles: [resolve('devup.json')], + stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, ], @@ -1583,6 +1457,31 @@ export const box = style({ color: 'red' })` // Verify stale port file was deleted before starting coordinator expect(unlinkSyncSpy).toHaveBeenCalledWith(join('df', 'coordinator.port')) }) + it('keys the loaders on the setup state without shipping it in the config', () => { + process.env.TURBOPACK = '1' + existsSyncSpy.mockReturnValue(true) + mkdirSyncSpy.mockReturnValue('') + writeFileSyncSpy.mockReturnValue(undefined) + const setup = (theme: string) => { + readFileSyncSpy.mockReturnValue(JSON.stringify({ theme })) + const rules = DevupUI({}).turbopack?.rules as { + './df/devup-ui/*.css': { options: { stateKey: string } }[] + '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}': { + loaders: { options: { stateKey: string } }[] + } + } + const css = rules['./df/devup-ui/*.css'][0].options + const source = + rules['*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}'].loaders[0].options + expect(source.stateKey).toBe(css.stateKey) + return { rules, stateKey: source.stateKey } + } + + const first = setup('first') + expect(JSON.stringify(first.rules)).not.toContain('first') + expect(setup('first').stateKey).toBe(first.stateKey) + expect(setup('second').stateKey).not.toBe(first.stateKey) + }) it('writes its own state next to another live process on the same distDir', () => { process.env.TURBOPACK = '1' const stateDir = join('df', `state.${process.pid}`) @@ -1627,10 +1526,10 @@ export const box = style({ color: 'red' })` const rule = config.turbopack?.rules?.[ '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}' ] as { - loaders: { options: { sheetFile: string } }[] + loaders: { options: { coordinatorPortFile: string } }[] } - expect(rule.loaders[0].options.sheetFile).toBe( - join(stateDir, 'sheet.json'), + expect(rule.loaders[0].options.coordinatorPortFile).toBe( + join('df', `coordinator.${process.pid}.port`), ) } finally { portSpy.mockRestore() diff --git a/packages/next-plugin/src/css-loader.ts b/packages/next-plugin/src/css-loader.ts index d6cad7ff0..be0e46cac 100644 --- a/packages/next-plugin/src/css-loader.ts +++ b/packages/next-plugin/src/css-loader.ts @@ -10,11 +10,21 @@ import type { RawLoaderDefinitionFunction } from 'webpack' import { callCoordinator, resetCoordinatorClient } from './coordinator-client' import { loadWasm } from './wasm' -export interface DevupUICssLoaderOptions { +/** Options of a loader that fetches the CSS from the coordinator (Turbopack). */ +export interface DevupUICoordinatorCssLoaderOptions { + coordinatorPortFile: string + /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + themeFiles?: string[] + watch: boolean + /** Changes with the setup state, so Turbopack re-runs the loader then */ + stateKey?: string +} + +/** Options of a loader that reads the CSS in its own process. */ +export interface DevupUIStandaloneCssLoaderOptions { engine?: DevupEngine - // turbo watch: boolean - coordinatorPortFile?: string + coordinatorPortFile?: undefined sheetFile: string classMapFile: string fileMapFile: string @@ -27,6 +37,15 @@ export interface DevupUICssLoaderOptions { defaultFileMap: object } +export type DevupUICssLoaderOptions = + DevupUICoordinatorCssLoaderOptions | DevupUIStandaloneCssLoaderOptions + +function isCoordinatorOptions( + options: DevupUICssLoaderOptions, +): options is DevupUICoordinatorCssLoaderOptions { + return !!options.coordinatorPortFile +} + let initializedCompilers = new WeakSet() function fetchCssFromCoordinator( portFile: string, @@ -65,23 +84,11 @@ function fetchCssFromCoordinator( const devupUICssLoader: RawLoaderDefinitionFunction = function (source, map, meta) { - const { - watch, - engine, - coordinatorPortFile, - sheetFile, - classMapFile, - fileMapFile, - themeFile, - themeFiles = [], - theme, - defaultClassMap, - defaultFileMap, - defaultSheet, - } = this.getOptions() + const options = this.getOptions() // Coordinator mode: fetch live CSS from coordinator (disk content is stale) - if (coordinatorPortFile) { + if (isCoordinatorOptions(options)) { + const { coordinatorPortFile, themeFiles = [], watch } = options // 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() @@ -102,6 +109,19 @@ const devupUICssLoader: RawLoaderDefinitionFunction = return } + const { + watch, + engine, + sheetFile, + classMapFile, + fileMapFile, + themeFile, + theme, + defaultClassMap, + defaultFileMap, + defaultSheet, + } = options + let content: string | typeof source try { const compiler = loadWasm(engine) diff --git a/packages/next-plugin/src/loader.ts b/packages/next-plugin/src/loader.ts index cd367a423..ebda90842 100644 --- a/packages/next-plugin/src/loader.ts +++ b/packages/next-plugin/src/loader.ts @@ -19,7 +19,17 @@ const stateWriter = createStateWriter((path, content, encoding) => encoding ? writeFile(path, content, encoding) : writeFile(path, content), ) -export interface DevupUILoaderOptions { +/** Options of a loader that sends every file to the coordinator (Turbopack). */ +export interface DevupUICoordinatorLoaderOptions { + coordinatorPortFile: string + /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + themeFiles?: string[] + /** Changes with the setup state, so Turbopack re-runs the loader then */ + stateKey?: string +} + +/** Options of a loader that extracts in its own process. */ +export interface DevupUIStandaloneLoaderOptions { engine?: DevupEngine package: string cssDir: string @@ -31,14 +41,22 @@ export interface DevupUILoaderOptions { themeFiles?: string[] watch: boolean singleCss: boolean - coordinatorPortFile?: string - // turbo + coordinatorPortFile?: undefined theme?: object defaultSheet: object defaultClassMap: object defaultFileMap: object importAliases?: Record } + +export type DevupUILoaderOptions = + DevupUICoordinatorLoaderOptions | DevupUIStandaloneLoaderOptions + +function isCoordinatorOptions( + options: DevupUILoaderOptions, +): options is DevupUICoordinatorLoaderOptions { + return !!options.coordinatorPortFile +} let initializedCompilers = new WeakSet() interface CoordinatorResponse { @@ -135,27 +153,11 @@ function coordinatorExtract( const devupUILoader: RawLoaderDefinitionFunction = function (source) { - const { - watch, - engine, - package: libPackage, - cssDir, - sheetFile, - classMapFile, - fileMapFile, - themeFile, - singleCss, - coordinatorPortFile, - theme, - themeFiles = [], - defaultClassMap, - defaultFileMap, - defaultSheet, - importAliases = {}, - } = this.getOptions() + const options = this.getOptions() // Coordinator mode: delegate to HTTP server - if (coordinatorPortFile) { + if (isCoordinatorOptions(options)) { + const { coordinatorPortFile, themeFiles = [] } = options this.addDependency(coordinatorPortFile) // Extraction reads theme tokens: re-run when the theme changes (the // coordinator registers the new theme before it answers). @@ -188,6 +190,23 @@ const devupUILoader: RawLoaderDefinitionFunction = return } + const { + watch, + engine, + package: libPackage, + cssDir, + sheetFile, + classMapFile, + fileMapFile, + themeFile, + singleCss, + theme, + defaultClassMap, + defaultFileMap, + defaultSheet, + importAliases = {}, + } = options + // Non-coordinator mode retains one selected compiler through callbacks. const callback = this.async() try { diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index ff60e8b7a..ec39a7502 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -1,3 +1,4 @@ +import { createHash } from 'node:crypto' import { existsSync, mkdirSync, @@ -764,47 +765,32 @@ export function DevupUI( stateSnapshotStartedAt === undefined ? undefined : performance.now() const defaultSheetJson = exportSheet() const sheetSerializeMs = elapsedMs(sheetSerializeStartedAt) - const sheetParseStartedAt = - stateSnapshotStartedAt === undefined ? undefined : performance.now() - const defaultSheet = JSON.parse(defaultSheetJson) - const sheetParseMs = elapsedMs(sheetParseStartedAt) const classMapSerializeStartedAt = stateSnapshotStartedAt === undefined ? undefined : performance.now() const defaultClassMapJson = exportClassMap() const classMapSerializeMs = elapsedMs(classMapSerializeStartedAt) - const classMapParseStartedAt = - stateSnapshotStartedAt === undefined ? undefined : performance.now() - const defaultClassMap = JSON.parse(defaultClassMapJson) - const classMapParseMs = elapsedMs(classMapParseStartedAt) const fileMapSerializeStartedAt = stateSnapshotStartedAt === undefined ? undefined : performance.now() const defaultFileMapJson = exportFileMap() const fileMapSerializeMs = elapsedMs(fileMapSerializeStartedAt) - const fileMapParseStartedAt = - stateSnapshotStartedAt === undefined ? undefined : performance.now() - const defaultFileMap = JSON.parse(defaultFileMapJson) - const fileMapParseMs = elapsedMs(fileMapParseStartedAt) reportProfile('next.stateSnapshot', { classMapBytes: stateSnapshotStartedAt === undefined ? undefined : Buffer.byteLength(defaultClassMapJson), - classMapParseMs, classMapSerializeMs, durationMs: elapsedMs(stateSnapshotStartedAt), fileMapBytes: stateSnapshotStartedAt === undefined ? undefined : Buffer.byteLength(defaultFileMapJson), - fileMapParseMs, fileMapSerializeMs, sheetBytes: stateSnapshotStartedAt === undefined ? undefined : Buffer.byteLength(defaultSheetJson), - sheetParseMs, sheetSerializeMs, }) // for theme script @@ -817,24 +803,38 @@ export function DevupUI( }) } + // Every loader call runs in coordinator mode: it reads only the port file + // and depends on the theme files. Next serializes the resolved config into + // `.next/required-server-files.{js,json}`, which ships with the app, so the + // rules carry nothing else. The state the loaders used to receive still + // keys Turbopack's loader cache through `stateKey`: a change to it re-runs + // the loaders as before. + const stateKey = createHash('sha256') + .update( + JSON.stringify([ + defaultSheetJson, + defaultClassMapJson, + defaultFileMapJson, + theme, + engine, + libPackage, + cssDir, + sheetFile, + classMapFile, + fileMapFile, + devupFile, + singleCss, + importAliases, + watch, + ]), + ) + .digest('hex') + .slice(0, 16) const rules: NonNullable = { [`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [ { loader: '@devup-ui/next-plugin/css-loader', - options: { - watch, - engine, - coordinatorPortFile, - sheetFile, - classMapFile, - fileMapFile, - themeFile: devupFile, - themeFiles, - defaultSheet, - defaultClassMap, - defaultFileMap, - theme, - }, + options: { coordinatorPortFile, themeFiles, watch, stateKey }, }, ], // Must cover every extension the import-graph pre-pass lists AND the @@ -850,25 +850,7 @@ export function DevupUI( loaders: [ { loader: '@devup-ui/next-plugin/loader', - options: { - package: libPackage, - engine, - cssDir, - coordinatorPortFile, - sheetFile, - classMapFile, - fileMapFile, - themeFile: devupFile, - themeFiles, - defaultSheet, - defaultClassMap, - defaultFileMap, - watch, - singleCss, - // for turbopack, load theme is required on loader - theme, - importAliases: importAliases as unknown as Record, - }, + options: { coordinatorPortFile, themeFiles, stateKey }, }, ], condition: { From 1f18f70cc59172ad3716e749956969036075a18f Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Thu, 19 Nov 2026 15:24:20 +0900 Subject: [PATCH 2/3] perf(next-plugin): pass the Turbopack loaders theme files relative to the project root The serialized config shipped absolute local paths (C:\Users\...). The loaders run in the project root and resolve the relative paths before adding them as dependencies, so dev and HMR still watch the same files. Co-Authored-By: Claude Opus 5.5 --- .../src/__tests__/css-loader.test.ts | 8 +++++--- .../next-plugin/src/__tests__/loader.test.ts | 3 ++- .../next-plugin/src/__tests__/plugin.test.ts | 18 +++++++++--------- packages/next-plugin/src/css-loader.ts | 5 +++-- packages/next-plugin/src/loader.ts | 4 ++-- packages/next-plugin/src/plugin.ts | 17 +++++++++++++++-- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/packages/next-plugin/src/__tests__/css-loader.test.ts b/packages/next-plugin/src/__tests__/css-loader.test.ts index fac51368d..d26eb4b76 100644 --- a/packages/next-plugin/src/__tests__/css-loader.test.ts +++ b/packages/next-plugin/src/__tests__/css-loader.test.ts @@ -1,5 +1,6 @@ import * as fs from 'node:fs' import * as http from 'node:http' +import * as path from 'node:path' import * as wasm from '@devup-ui/wasm' import { @@ -197,7 +198,8 @@ describe('devupUICssLoader', () => { ...defaultOptions, watch: true, coordinatorPortFile: portFile, - themeFiles: ['/project/devup.json', '/project/app.css'], + // Relative to the project root, as the plugin passes them + themeFiles: ['devup.json', 'app.css'], }), resourcePath: 'devup-ui-1.css', } as unknown as CssLoaderThis)( @@ -210,8 +212,8 @@ 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'], + [path.resolve('devup.json')], + [path.resolve('app.css')], ]) // NO WASM functions should be called diff --git a/packages/next-plugin/src/__tests__/loader.test.ts b/packages/next-plugin/src/__tests__/loader.test.ts index 01322d965..83e949048 100644 --- a/packages/next-plugin/src/__tests__/loader.test.ts +++ b/packages/next-plugin/src/__tests__/loader.test.ts @@ -707,7 +707,8 @@ export const a =
` watch: true, singleCss: true, coordinatorPortFile: 'coordinator.port', - themeFiles: [resolve('devup.json'), resolve('base.json')], + // One relative to the project root, as the plugin passes it + themeFiles: ['devup.json', resolve('base.json')], }), async: mock().mockReturnValue(asyncCallback), resourcePath: join(process.cwd(), 'src', 'App.tsx'), diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 29e82b563..caf2a3196 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -810,7 +810,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/css-loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], watch: false, stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, @@ -822,7 +822,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, @@ -854,7 +854,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/css-loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], watch: false, stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, @@ -873,7 +873,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, @@ -906,7 +906,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/css-loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], watch: false, stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, @@ -925,7 +925,7 @@ describe('DevupUINextPlugin', () => { loader: '@devup-ui/next-plugin/loader', options: { coordinatorPortFile: join('df', 'coordinator.port'), - themeFiles: [resolve('devup.json')], + themeFiles: ['devup.json'], stateKey: expect.stringMatching(/^[0-9a-f]{16}$/), }, }, @@ -1664,9 +1664,9 @@ export const box = style({ color: 'red' })` loaders: { options: { themeFiles: string[] } }[] } expect(rule.loaders[0].options.themeFiles).toEqual([ - resolve('devup.json'), - resolve('app.css'), - resolve('tokens.css'), + 'devup.json', + 'app.css', + 'tokens.css', ]) } finally { tailwindSpy.mockRestore() diff --git a/packages/next-plugin/src/css-loader.ts b/packages/next-plugin/src/css-loader.ts index be0e46cac..7a5394b53 100644 --- a/packages/next-plugin/src/css-loader.ts +++ b/packages/next-plugin/src/css-loader.ts @@ -1,4 +1,5 @@ import { existsSync, readFileSync } from 'node:fs' +import { resolve } from 'node:path' import { type DevupCompiler, @@ -13,7 +14,7 @@ import { loadWasm } from './wasm' /** Options of a loader that fetches the CSS from the coordinator (Turbopack). */ export interface DevupUICoordinatorCssLoaderOptions { coordinatorPortFile: string - /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + /** Every file the theme is read from, relative to the project root or absolute */ themeFiles?: string[] watch: boolean /** Changes with the setup state, so Turbopack re-runs the loader then */ @@ -90,7 +91,7 @@ const devupUICssLoader: RawLoaderDefinitionFunction = if (isCoordinatorOptions(options)) { const { coordinatorPortFile, themeFiles = [], watch } = options // The sheet holds the theme's variables: re-run when the theme changes. - for (const file of themeFiles) this.addDependency(file) + for (const file of themeFiles) this.addDependency(resolve(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 ebda90842..a5f3cc4ef 100644 --- a/packages/next-plugin/src/loader.ts +++ b/packages/next-plugin/src/loader.ts @@ -22,7 +22,7 @@ const stateWriter = createStateWriter((path, content, encoding) => /** Options of a loader that sends every file to the coordinator (Turbopack). */ export interface DevupUICoordinatorLoaderOptions { coordinatorPortFile: string - /** Every file the theme is read from (devup.json, its extends, Tailwind CSS) */ + /** Every file the theme is read from, relative to the project root or absolute */ themeFiles?: string[] /** Changes with the setup state, so Turbopack re-runs the loader then */ stateKey?: string @@ -161,7 +161,7 @@ const devupUILoader: RawLoaderDefinitionFunction = 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) + for (const file of themeFiles) this.addDependency(resolve(file)) const callback = this.async() // POSIX-normalize so the engine's bucket key matches the canonical map // and FILE_ROUTES keys (both built with forward slashes). Without this, diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index ec39a7502..be714cb71 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -830,11 +830,20 @@ export function DevupUI( ) .digest('hex') .slice(0, 16) + // Relative to the project root the loaders run in: no local path ships. + const loaderThemeFiles = themeFiles.map((file) => + relative(process.cwd(), file).replaceAll('\\', '/'), + ) const rules: NonNullable = { [`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [ { loader: '@devup-ui/next-plugin/css-loader', - options: { coordinatorPortFile, themeFiles, watch, stateKey }, + options: { + coordinatorPortFile, + themeFiles: loaderThemeFiles, + watch, + stateKey, + }, }, ], // Must cover every extension the import-graph pre-pass lists AND the @@ -850,7 +859,11 @@ export function DevupUI( loaders: [ { loader: '@devup-ui/next-plugin/loader', - options: { coordinatorPortFile, themeFiles, stateKey }, + options: { + coordinatorPortFile, + themeFiles: loaderThemeFiles, + stateKey, + }, }, ], condition: { From 5c0a1d42cd0132b163ba88d5529e2fc1e90cf463 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Thu, 19 Nov 2026 15:49:04 +0900 Subject: [PATCH 3/3] docs(next-plugin): explain the loader rules left in Next's server config snapshot Co-Authored-By: Claude Opus 5.5 --- .../src/app/(detail)/docs/limitations/page.mdx | 1 + packages/next-plugin/README.md | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/landing/src/app/(detail)/docs/limitations/page.mdx b/apps/landing/src/app/(detail)/docs/limitations/page.mdx index 2d63a9e00..374d2a6e3 100644 --- a/apps/landing/src/app/(detail)/docs/limitations/page.mdx +++ b/apps/landing/src/app/(detail)/docs/limitations/page.mdx @@ -100,6 +100,7 @@ A dynamic value such as `width` becomes a CSS variable set in `style`, so a comp - Inside ``, `cx(getClass())` is a build error; put the result in a variable first, which then joins as a class. - `withComponent` takes a tag name or a component written as a name or `a.b`. On a component made with Devup UI's or styled-components' `styled`, a target only the runtime knows, as `withComponent(make())`, is a build error: such a component compiles to a plain function with no `withComponent`. Pass the target directly, or render with `as`. A component made with Emotion's `styled` takes `withComponent` at runtime as well, so any one target works there. Options passed beside the target are a build error. - An `.mdx` file is extracted after your MDX plugin compiles it to JavaScript, never as raw MDX. The Vite (`@mdx-js/rollup`), webpack and Rsbuild (`@mdx-js/loader`, `@rsbuild/plugin-mdx`) plugins and Next.js with webpack (`@next/mdx`) run their MDX step in a later phase: Vite in a `post` transform, webpack and Rsbuild in a `post` loader. Next.js with Turbopack (`@next/mdx`) extracts the compiled document: `@next/mdx` renames its output to `.tsx` and Turbopack runs the source rule on it again, so it works whichever of `withMDX` and `DevupUI` wraps the other. A production Turbopack build cannot extract a document before it compiles, so its stylesheet waits for the MDX route files (when `pageExtensions` lists `mdx`) and the documents they and the compiled sources import by a relative path; a document imported only through a path alias is not waited for. The Bun plugin does not extract MDX yet. The scan that reserves class names before a build reads JavaScript and TypeScript modules only, so a document's own classes are named when it compiles. Put components that need Devup UI styles in a `.tsx` module and use them from the document. +- With Turbopack, Next.js copies its resolved config into `.next/required-server-files.{js,json}` and the standalone `server.js`, so the two Devup UI loader rules (about 500 bytes, the same for any app size, with no local path) ship with the server output. They cannot be removed safely today: Next.js copies the config before any hook the plugin can register runs, and its only option that leaves bundler settings out of that copy also changes how the server reads its deployment id ([vercel/next.js#102045](https://github.com/vercel/next.js/discussions/102045)). Webpack builds add nothing there. - The plugins read `jsxImportSource` from the `tsconfig.json` (or `jsconfig.json`) in the working directory. Opt out with `importAliases: { '@emotion/react/jsx-runtime': false }`. - The outermost `ThemeProvider` also declares its theme on `:root`, in a `