Skip to content
Open
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
9 changes: 9 additions & 0 deletions .changepacks/changepack_log_route_shells_shared_css.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"changes": {
"packages/next-plugin/package.json": "Patch",
"packages/plugin-utils/package.json": "Patch",
"packages/webpack-plugin/package.json": "Patch"
},
"note": "A Next production webpack build of an app with several routes now counts the root `layout` or `template` (or `pages/_app`), which Next renders with every route, as part of those routes when it hoists atoms, instead of as a route of its own. What it imports (a header, a footer) then reaches every route and joins the shared `devup-ui.css` with short class names, instead of a stylesheet of its own that every page loaded anyway. A nested layout that wraps only some routes stays a route of its own, so its styles never load outside it; lazily loaded files and other entries keep their own routes. `next dev`, Turbopack and apps without a layout keep their output; plain webpack and Rsbuild builds are unchanged unless they set the new `routeShells` option.",
"date": "2026-11-19T00:00:00.000Z"
}
27 changes: 4 additions & 23 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,30 +470,11 @@ describe('DevupUINextPlugin', () => {
singleCss: true,
cssDir: resolve('.next/cache', 'devup-ui'),
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
routeShells: true,
})
},
)

it('shares one source cache between the compilers of a production build, none in dev', () => {
// Given
delete process.env.TURBOPACK
const ret = DevupUI({})
// When: Next configures its server and client compilers, then a dev one
ret.webpack!(createWebpackConfig(), createWebpackContext())
ret.webpack!(createWebpackConfig(), createWebpackContext())
ret.webpack!(createWebpackConfig(), {
...createWebpackContext(),
dev: true,
})
// Then
const [server, client, dev] = devupUIWebpackPluginSpy.mock.calls.map(
([options]) => (options as { sourceCache?: unknown }).sourceCache,
)
expect(server).toBeInstanceOf(Map)
expect(client).toBe(server)
expect(dev).toBeUndefined()
})
it.each([
['a one-route production build', {}, {}, { singleCss: true }],
[
Expand Down Expand Up @@ -537,7 +518,7 @@ describe('DevupUINextPlugin', () => {
cssDir: resolve('.next/cache', 'devup-ui'),
engine: 'native',
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
routeShells: true,
})
})

Expand Down Expand Up @@ -641,7 +622,7 @@ describe('DevupUINextPlugin', () => {
engine: 'native',
cssDir: resolve('.next/cache', 'devup-ui'),
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
routeShells: true,
})
})

Expand All @@ -663,7 +644,7 @@ describe('DevupUINextPlugin', () => {
engine: 'native',
cssDir: resolve('.next/cache', 'devup-ui'),
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
routeShells: true,
})
expect(webpack).toHaveBeenCalled()
})
Expand Down
73 changes: 29 additions & 44 deletions packages/next-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ import {
normalizeShorthands,
normalizeStylexOptions,
readJsxImportSource,
readSource,
seedFileNumbers,
type SourceCache,
type StaticImportGraph,
type WasmImportAliases,
withSourceCache,
} from '@devup-ui/plugin-utils'
// The published declarations name only these: `next.config.ts` type-checks
// against them, so they come from the entry that loads nothing else
Expand Down Expand Up @@ -65,6 +62,11 @@ export type DevupUINextPluginOptions = Partial<DevupUIBasePluginOptions> & {
* Atom-level route-aware hoisting threshold for webpack (default 2; 0 disables).
* Turbopack keeps per-file names until shared class-slot reservation lands in
* #752; explicit finite positive requests warn once. Ignored in singleCss mode.
*
* A production webpack build counts the root `layout` (or `pages/_app`),
* which wraps every route, with those routes rather than as a route of its
* own, so what it imports joins the shared `devup-ui.css`. A nested layout
* stays a route of its own.
*/
atomHoist?: number
}
Expand Down Expand Up @@ -489,8 +491,6 @@ export function DevupUI(
// `[]` (idle fallback) when no routes are detected or the pre-pass fails.
let expectedBaseFiles: string[] = []
let staticGraph: StaticImportGraph | undefined
// The graph and the prewarm read the same sources
const sourceCache: SourceCache = new Map()
// A one-route production build gets one stylesheet, unless asked otherwise
let singleRoute = false
const graphStartedAt = profileStart()
Expand All @@ -499,10 +499,7 @@ export function DevupUI(
const tsconfigPath = resolve(process.cwd(), 'tsconfig.json')
const cwd = process.cwd()
// One scan+parse of the source tree, shared by all three consumers below.
const graph = withSourceCache(
() => buildStaticImportGraph(srcDir, tsconfigPath),
sourceCache,
)
const graph = buildStaticImportGraph(srcDir, tsconfigPath)
staticGraph = graph
singleRoute =
!watch &&
Expand Down Expand Up @@ -643,18 +640,13 @@ export function DevupUI(
const cwd = process.cwd()
const collectStartedAt =
prewarmStartedAt === undefined ? undefined : performance.now()
const graph = staticGraph
const prewarmFiles = withSourceCache(
() =>
collectProductionPrewarmFiles({
cwd,
graph,
expectedBaseFiles,
libPackage,
include,
}),
sourceCache,
)
const prewarmFiles = collectProductionPrewarmFiles({
cwd,
graph: staticGraph,
expectedBaseFiles,
libPackage,
include,
})
const collectDurationMs = elapsedMs(collectStartedAt)
for (const filename of prewarmFiles) {
const resourcePath = resolve(cwd, filename)
Expand All @@ -664,10 +656,7 @@ export function DevupUI(
).replaceAll('\\', '/')}`
const readStartedAt =
prewarmStartedAt === undefined ? undefined : performance.now()
const source = withSourceCache(
() => readSource(resourcePath),
sourceCache,
)
const source = readFileSync(resourcePath, 'utf-8')
if (readStartedAt !== undefined) {
prewarmReadMs += performance.now() - readStartedAt
prewarmSourceBytes += Buffer.byteLength(source)
Expand Down Expand Up @@ -921,9 +910,6 @@ export function DevupUI(

const { webpack } = config
let productionPrewarmFiles: readonly string[] | undefined
// A production build runs its compilers in turn: their plugins read each
// source once
const sourceCache: SourceCache = new Map()
// A one-route production build gets one stylesheet, unless asked otherwise
let singleRoute = false
config.webpack = (config, _options) => {
Expand All @@ -940,23 +926,21 @@ export function DevupUI(
const cwd = root
const srcDir = sourceDir(cwd)
const tsconfigPath = resolve(cwd, 'tsconfig.json')
productionPrewarmFiles = withSourceCache(() => {
const graph = buildStaticImportGraph(srcDir, tsconfigPath)
singleRoute =
options.singleCss === undefined && isSingleRouteApp(srcDir, graph)
return collectProductionPrewarmFiles({
const graph = buildStaticImportGraph(srcDir, tsconfigPath)
singleRoute =
options.singleCss === undefined && isSingleRouteApp(srcDir, graph)
productionPrewarmFiles = collectProductionPrewarmFiles({
cwd,
graph,
expectedBaseFiles: computeCompiledFiles({
cwd,
srcDir,
tsconfigPath,
graph,
expectedBaseFiles: computeCompiledFiles({
cwd,
srcDir,
tsconfigPath,
graph,
}),
libPackage: options.package ?? '@devup-ui/react',
include: options.include ?? [],
})
}, sourceCache)
}),
libPackage: options.package ?? '@devup-ui/react',
include: options.include ?? [],
})
}
config.plugins.push(
new DevupUIWebpackPlugin({
Expand All @@ -966,8 +950,9 @@ export function DevupUI(
productionPrewarmFiles: _options.dev
? undefined
: productionPrewarmFiles,
sourceCache: _options.dev ? undefined : sourceCache,
...(!_options.dev && singleRoute ? { singleCss: true } : {}),
// A layout counts with the routes it wraps (production only)
...(_options.dev ? {} : { routeShells: true }),
}),
)
if (typeof webpack === 'function') return webpack(config, _options)
Expand Down
6 changes: 3 additions & 3 deletions packages/next-plugin/src/prewarm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { existsSync } from 'node:fs'
import { existsSync, readFileSync } from 'node:fs'
import { extname, isAbsolute, relative, resolve } from 'node:path'

import {
createModuleResolver,
isRuntimePackage,
parseImports,
readSource,
SOURCE_FILE_REGEX,
type StaticImportGraph,
} from '@devup-ui/plugin-utils'
Expand Down Expand Up @@ -103,7 +102,8 @@ export function collectProductionPrewarmFiles({
if (!SOURCE_FILE_REGEX.test(filename) || visited.has(filename)) continue
visited.add(filename)
files.add(toKey(resolvedCwd, filename))
const source = filename === module.path ? module.code : readSource(filename)
const source =
filename === module.path ? module.code : readFileSync(filename, 'utf-8')
for (const imported of parseImports(filename, source)) {
queue.push({ importer: filename, specifier: imported.specifier })
}
Expand Down
Loading
Loading