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
9 changes: 9 additions & 0 deletions .changepacks/changepack_log_shared_source_reads.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"changes": {
"packages/plugin-utils/package.json": "Patch",
"packages/webpack-plugin/package.json": "Patch",
"packages/next-plugin/package.json": "Patch"
},
"note": "Read each source file once per build in the passes that run before the bundler. The import graph, the class-slot reservation and the prewarm read the same files, and a Next production build runs them once per compiler (server, edge, client): they now share a stamp-checked source cache (`withSourceCache` / `readSource` in plugin-utils), which Next passes to the webpack plugin of each compiler. Watch and dev builds keep reading the disk on every compilation. Output is unchanged.",
"date": "2026-11-19T00:00:00.000Z"
}
41 changes: 28 additions & 13 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,30 @@ describe('DevupUINextPlugin', () => {
singleCss: true,
cssDir: resolve('.next/cache', 'devup-ui'),
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
})
},
)

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 @@ -517,6 +537,7 @@ describe('DevupUINextPlugin', () => {
cssDir: resolve('.next/cache', 'devup-ui'),
engine: 'native',
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
})
})

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

Expand All @@ -641,6 +663,7 @@ describe('DevupUINextPlugin', () => {
engine: 'native',
cssDir: resolve('.next/cache', 'devup-ui'),
productionPrewarmFiles: [],
sourceCache: expect.any(Map),
})
expect(webpack).toHaveBeenCalled()
})
Expand Down Expand Up @@ -672,19 +695,11 @@ describe('DevupUINextPlugin', () => {
).mockReturnValue(true)
try {
// When
const config = DevupUI({}, options) as {
turbopack: {
rules: Record<
string,
{ loaders: { options: { singleCss: boolean } }[] }
>
}
}
// Then the loaders extract into the layout chosen
expect(
config.turbopack.rules['*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}']
.loaders[0].options.singleCss,
).toBe(expected)
DevupUI({}, options)
// Then the coordinator extracts into the layout chosen
expect(startCoordinatorSpy).toHaveBeenCalledWith(
expect.objectContaining({ singleCss: expected }),
)
} finally {
singleRoute.mockRestore()
graphSpy.mockRestore()
Expand Down
66 changes: 44 additions & 22 deletions packages/next-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ 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 @@ -486,6 +489,8 @@ 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 @@ -494,7 +499,10 @@ 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 = buildStaticImportGraph(srcDir, tsconfigPath)
const graph = withSourceCache(
() => buildStaticImportGraph(srcDir, tsconfigPath),
sourceCache,
)
staticGraph = graph
singleRoute =
!watch &&
Expand Down Expand Up @@ -635,13 +643,18 @@ export function DevupUI(
const cwd = process.cwd()
const collectStartedAt =
prewarmStartedAt === undefined ? undefined : performance.now()
const prewarmFiles = collectProductionPrewarmFiles({
cwd,
graph: staticGraph,
expectedBaseFiles,
libPackage,
include,
})
const graph = staticGraph
const prewarmFiles = withSourceCache(
() =>
collectProductionPrewarmFiles({
cwd,
graph,
expectedBaseFiles,
libPackage,
include,
}),
sourceCache,
)
const collectDurationMs = elapsedMs(collectStartedAt)
for (const filename of prewarmFiles) {
const resourcePath = resolve(cwd, filename)
Expand All @@ -651,7 +664,10 @@ export function DevupUI(
).replaceAll('\\', '/')}`
const readStartedAt =
prewarmStartedAt === undefined ? undefined : performance.now()
const source = readFileSync(resourcePath, 'utf-8')
const source = withSourceCache(
() => readSource(resourcePath),
sourceCache,
)
if (readStartedAt !== undefined) {
prewarmReadMs += performance.now() - readStartedAt
prewarmSourceBytes += Buffer.byteLength(source)
Expand Down Expand Up @@ -905,6 +921,9 @@ 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 @@ -921,21 +940,23 @@ export function DevupUI(
const cwd = root
const srcDir = sourceDir(cwd)
const tsconfigPath = resolve(cwd, 'tsconfig.json')
const graph = buildStaticImportGraph(srcDir, tsconfigPath)
singleRoute =
options.singleCss === undefined && isSingleRouteApp(srcDir, graph)
productionPrewarmFiles = collectProductionPrewarmFiles({
cwd,
graph,
expectedBaseFiles: computeCompiledFiles({
productionPrewarmFiles = withSourceCache(() => {
const graph = buildStaticImportGraph(srcDir, tsconfigPath)
singleRoute =
options.singleCss === undefined && isSingleRouteApp(srcDir, graph)
return collectProductionPrewarmFiles({
cwd,
srcDir,
tsconfigPath,
graph,
}),
libPackage: options.package ?? '@devup-ui/react',
include: options.include ?? [],
})
expectedBaseFiles: computeCompiledFiles({
cwd,
srcDir,
tsconfigPath,
graph,
}),
libPackage: options.package ?? '@devup-ui/react',
include: options.include ?? [],
})
}, sourceCache)
}
config.plugins.push(
new DevupUIWebpackPlugin({
Expand All @@ -945,6 +966,7 @@ export function DevupUI(
productionPrewarmFiles: _options.dev
? undefined
: productionPrewarmFiles,
sourceCache: _options.dev ? undefined : sourceCache,
...(!_options.dev && singleRoute ? { singleCss: true } : {}),
}),
)
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,10 +1,11 @@
import { existsSync, readFileSync } from 'node:fs'
import { existsSync } 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 @@ -102,8 +103,7 @@ 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 : readFileSync(filename, 'utf-8')
const source = filename === module.path ? module.code : readSource(filename)
for (const imported of parseImports(filename, source)) {
queue.push({ importer: filename, specifier: imported.specifier })
}
Expand Down
103 changes: 103 additions & 0 deletions packages/plugin-utils/src/__tests__/source-cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'

import { afterEach, beforeEach, describe, expect, it } from 'bun:test'

import {
fileStamp,
readSource,
type SourceCache,
withSourceCache,
} from '../source-cache'

describe('readSource', () => {
let dir: string
let file: string

beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'devup-ui-source-cache-'))
file = join(dir, 'a.tsx')
writeFileSync(file, 'on disk')
})

afterEach(() => {
rmSync(dir, { recursive: true, force: true })
})

/** A cache already holding `code` for the file as it is now */
function holding(code: string): SourceCache {
return new Map([[resolve(file), { stamp: fileStamp(file) ?? '', code }]])
}

it('reads the disk outside a cache', () => {
expect(readSource(file)).toBe('on disk')
expect(globalThis.__devupSourceCache).toBeUndefined()
})

it('serves an unchanged file from the cache, under any spelling of its path', () => {
// Given
const cache = holding('cached')
// When
const read = withSourceCache(
() => [readSource(file), readSource(join(dir, '.', 'a.tsx'))],
cache,
)
// Then
expect(read).toEqual(['cached', 'cached'])
})

it('reads a changed file again and keeps it', () => {
// Given
const cache = holding('stale')
// When
writeFileSync(file, 'changed text')
// Then
expect(withSourceCache(() => readSource(file), cache)).toBe('changed text')
expect(cache.get(resolve(file))).toEqual({
stamp: fileStamp(file) ?? '',
code: 'changed text',
})
})

it('reads a file once into an empty cache', () => {
const cache: SourceCache = new Map()
expect(withSourceCache(() => readSource(file), cache)).toBe('on disk')
expect(cache.get(resolve(file))?.code).toBe('on disk')
})

it('reports a deleted file and forgets it', () => {
// Given
const cache = holding('cached')
// When
rmSync(file)
// Then
expect(() => withSourceCache(() => readSource(file), cache)).toThrow()
expect(cache.size).toBe(0)
})

it('keeps the outer cache in a nested run and restores none after', () => {
// Given
const outer: SourceCache = new Map()
const inner: SourceCache = new Map()
// When
withSourceCache(() => withSourceCache(() => readSource(file), inner), outer)
// Then
expect(outer.size).toBe(1)
expect(inner.size).toBe(0)
expect(globalThis.__devupSourceCache).toBeUndefined()
})

it('restores the outer cache when the run throws', () => {
expect(() =>
withSourceCache(() => {
throw new Error('boom')
}, new Map()),
).toThrow('boom')
expect(globalThis.__devupSourceCache).toBeUndefined()
})

it('stamps no missing file', () => {
expect(fileStamp(join(dir, 'missing.tsx'))).toBeUndefined()
})
})
Loading
Loading