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
8 changes: 8 additions & 0 deletions .changepacks/changepack_log_prewarm_warn_unreachable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"changes": {
"packages/next-plugin/package.json": "Patch",
"packages/webpack-plugin/package.json": "Patch"
},
"note": "A production prewarm no longer fails the build on a file it cannot extract. The Next.js prewarm still extracts every source file, but a file that does not parse (for example dead source no route imports) is now skipped with a warning naming the file and the error. If Next.js or webpack compiles that file, its loader still fails the build with the same error. Output for projects without such files is unchanged.",
"date": "2026-11-19T00:00:00.000Z"
}
2 changes: 2 additions & 0 deletions apps/landing/src/app/(detail)/docs/build-errors/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,8 @@ E.__emotion_user(); const broken;

**Fix:** Correct the syntax at the reported location, then fix any style diagnostic the build reports next.

A production `next build` first extracts every source file of the project, including files no route imports. A file it cannot extract there does not fail the build: the plugin prints a warning naming the file and the error, `[devup-ui] Turbopack prewarm of <file> failed: <message>. ...` (or `[devup-ui] webpack stylesheet warm-up of <file> failed: <message>. ...` with webpack), and skips it. If Next.js compiles that file, the build still fails with the error above.

{/* probe: parse-error after */}

```tsx
Expand Down
63 changes: 63 additions & 0 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,69 @@ export const box = style({ color: 'red' })`
}
})

it('skips an unreachable file it cannot extract with a warning, and goes on', () => {
process.env.TURBOPACK = '1'
// Given a dead file no route imports, which does not parse.
const [page, dead, late] = [
'src/app/page.tsx',
'src/dead.tsx',
'src/late.tsx',
].map((file) => resolve(file))
const files = [page, dead, late]
const graphSpy = spyOn(
importGraphModule,
'buildStaticImportGraph',
).mockReturnValue({
files,
fileSet: new Set(files),
staticImports: new Map(files.map((file) => [file, new Set<string>()])),
staticImporters: new Map(
files.map((file) => [file, new Set<string>()]),
),
dynamicTargets: new Set(),
dynamicImports: new Map(files.map((file) => [file, new Set<string>()])),
externalImports: new Map(
files.map((file) => [file, new Set<string>()]),
),
})
const compiledSpy = spyOn(
importGraphModule,
'computeCompiledFiles',
).mockReturnValue(['src/app/page.tsx'])
codeExtractWithoutSourceMapSpy.mockImplementation(
(filename: string, contents: string) => {
if (filename === 'src/dead.tsx')
throw new Error('Cannot parse source: Unexpected token')
return createCodeExtractResult(contents)
},
)
const warn = spyOn(console, 'warn').mockImplementation(() => {})
try {
// When the production prewarm runs, the build is not failed.
expect(() => DevupUI({})).not.toThrow()

// Then the dead file is named, left out, and the files after it prewarm.
expect(warn).toHaveBeenCalledWith(
'[devup-ui] Turbopack prewarm of src/dead.tsx failed: Cannot parse source: Unexpected token. The file is skipped; if Next.js compiles it, the build fails with this error.',
)
expect(codeExtractWithoutSourceMapSpy).toHaveBeenCalledTimes(3)
expect(startCoordinatorSpy).toHaveBeenCalledWith(
expect.objectContaining({
expectedBaseFiles: ['src/app/page.tsx'],
prewarmedFiles: ['src/app/page.tsx', 'src/late.tsx'],
}),
)
const coordinatorOptions = startCoordinatorSpy.mock.calls[0]?.[0]
expect([
...(coordinatorOptions?.prewarmedOutputs?.keys() ?? []),
]).toEqual(['src/app/page.tsx', 'src/late.tsx'])
} finally {
warn.mockRestore()
graphSpy.mockRestore()
compiledSpy.mockRestore()
}
})

it('prewarms the same complete file set in singleCss mode', () => {
process.env.TURBOPACK = '1'
const compiledSpy = spyOn(
Expand Down
129 changes: 129 additions & 0 deletions packages/next-plugin/src/__tests__/prewarm-error.wasm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { existsSync, mkdtempSync, rmSync, watch } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, relative } from 'node:path'

import * as wasm from '@devup-ui/wasm'
import { describe, expect, it } from 'bun:test'

import {
flushCoordinatorWrites,
resetCoordinator,
startCoordinator,
} from '../coordinator'
import loader, { resetInit } from '../loader'
import { withModuleResolver } from '../wasm'

const page = `import { Box } from '@devup-ui/react';
export const box = <Box color="red" />;`
const dead = `import { Box } from '@devup-ui/react';
export const box = <Box color="blue" />; const broken;`

function captureState() {
const sheet = wasm.exportSheet()
const classes = wasm.exportClassMap()
const files = wasm.exportFileMap()
const canonical = wasm.exportCanonicalMap()
return () => {
wasm.importSheet(JSON.parse(sheet))
wasm.importClassMap(JSON.parse(classes))
wasm.importFileMap(JSON.parse(files))
wasm.importCanonicalMap(JSON.parse(canonical))
}
}

describe('real WASM prewarm of a file that does not parse', () => {
it('leaves the sheet as it was, and fails the loader that compiles the file', async () => {
const restore = captureState()
const root = mkdtempSync(join(tmpdir(), 'devup-ui-prewarm-error-'))
const coordinatorPortFile = join(root, 'port')
const watcher = watch(root)
let readinessTimeout: ReturnType<typeof setTimeout> | undefined
let coordinator: ReturnType<typeof startCoordinator> | undefined
try {
wasm.importCanonicalMap({})
wasm.setModuleResolver(undefined)
const key = (file: string) =>
relative(process.cwd(), join(root, file)).replaceAll('\\', '/')
const args = ['@devup-ui/react', './', true, false, true, {}] as const
// Given the prewarm extracts a valid page, then a dead file.
wasm.codeExtractWithoutSourceMap(key('page.tsx'), page, ...args).free()
const css = wasm.getCss(null, false)
// When the dead file does not parse, the prewarm sees the real error.
expect(() =>
wasm.codeExtractWithoutSourceMap(key('dead.tsx'), dead, ...args),
).toThrow('Cannot parse source')
// Then skipping it leaves the sheet byte-identical.
expect(wasm.getCss(null, false)).toBe(css)

const ready = new Promise<void>((resolve, reject) => {
watcher.on('error', reject)
readinessTimeout = setTimeout(
() => reject(new Error('Coordinator port file was not ready')),
3000,
)
watcher.on('change', () => {
if (existsSync(coordinatorPortFile)) resolve()
})
})
const options = {
package: '@devup-ui/react',
cssDir: root,
sheetFile: join(root, 'sheet.json'),
classMapFile: join(root, 'classes.json'),
fileMapFile: join(root, 'files.json'),
singleCss: true,
coordinatorPortFile,
}
coordinator = startCoordinator({
...options,
distDir: root,
wasm,
importAliases: {},
canonicalMap: {},
prewarmedFiles: [key('page.tsx')],
expectedBaseFiles: [key('page.tsx')],
})
await ready
// When a route imports the dead file and Next.js compiles it, its
// loader fails the build with the same error.
const error = await new Promise<Error | null | undefined>((resolve) => {
const context = {
resourcePath: join(root, 'dead.tsx'),
getOptions: () => ({
...options,
themeFile: join(root, 'theme.json'),
watch: false,
defaultSheet: {},
defaultClassMap: {},
defaultFileMap: {},
}),
addDependency: (_path: string) => {},
async: () => {
const callback: ReturnType<
ThisParameterType<typeof loader>['async']
> = (error) => resolve(error)
return callback
},
} satisfies Pick<
ThisParameterType<typeof loader>,
'resourcePath' | 'getOptions' | 'addDependency' | 'async'
>
Reflect.apply(loader, context, [Buffer.from(dead)])
})
expect(error?.message).toContain('Cannot parse source')
} finally {
clearTimeout(readinessTimeout)
watcher.close()
coordinator?.close()
try {
await flushCoordinatorWrites()
} finally {
resetCoordinator()
resetInit()
restore()
withModuleResolver(wasm)
rmSync(root, { recursive: true, force: true })
}
}
})
})
43 changes: 30 additions & 13 deletions packages/next-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ export function DevupUI(
// entries synchronously first. The wider set covers template imports, MDX
// dependencies and package-level globalCss (notably reset-css) that the
// route graph cannot represent. Loader-time extraction uses the same
// keys/options and is idempotent.
// keys/options and is idempotent. A file that fails to extract is skipped
// with a warning; the loader reports its error if Next.js compiles it.
const prewarmedFiles: string[] = []
const prewarmedOutputs = new Map<string, PrewarmedOutput>()
if (!watch && staticGraph) {
Expand Down Expand Up @@ -561,19 +562,35 @@ export function DevupUI(
}
const extractStartedAt =
prewarmStartedAt === undefined ? undefined : performance.now()
const output = takeExtractOutput(
extract(
filename,
source,
libPackage,
relCssDir,
singleCss,
let output: ReturnType<typeof takeExtractOutput>
try {
output = takeExtractOutput(
extract(
filename,
source,
libPackage,
relCssDir,
singleCss,
false,
true,
importAliases as unknown as Record<string, string | null>,
),
false,
true,
importAliases as unknown as Record<string, string | null>,
),
false,
)
)
} catch (error) {
// The prewarm covers every source file, including ones no route
// imports, so a file it cannot extract must not fail the build. It
// stays out of the prewarm: when Next.js compiles it, its loader
// extracts it again and fails the build with the same error.
console.warn(
setupWarning(
`prewarm of ${filename}`,
error,
'The file is skipped; if Next.js compiles it, the build fails with this error.',
),
)
continue
}
if (extractStartedAt !== undefined) {
prewarmExtractMs += performance.now() - extractStartedAt
}
Expand Down
75 changes: 68 additions & 7 deletions packages/webpack-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ describe('devupUIWebpackPlugin', () => {
},
)

it.each(['canonical', 'numbering', 'read', 'extract'])(
it.each(['canonical', 'numbering', 'read'])(
'propagates genuine %s errors for an explicit complete set',
(stage) => {
// Given a failing correctness-prewarm dependency.
Expand All @@ -1550,7 +1550,6 @@ describe('devupUIWebpackPlugin', () => {
if (stage === 'canonical') buildCanonicalMapSpy.mockImplementation(fail)
if (stage === 'numbering') numbering.mockImplementation(fail)
if (stage === 'read') readFileSyncSpy.mockImplementation(fail)
if (stage === 'extract') codeExtractSpy.mockImplementation(fail)
const plugin = new DevupUIWebpackPlugin({
engine: 'wasm',
debug: stage === 'numbering',
Expand All @@ -1577,6 +1576,38 @@ describe('devupUIWebpackPlugin', () => {
},
)

it('skips a file of an explicit complete set it cannot extract, with a warning', () => {
// Given a complete set holding a dead file that does not parse.
readFileSyncSpy.mockImplementation((path: string) => String(path))
codeExtractSpy.mockImplementation(
(filename: string, contents: string) => {
if (filename === 'src/dead.tsx')
throw new Error('Cannot parse source: Unexpected token')
return createCodeExtractResult(contents)
},
)
const plugin = new DevupUIWebpackPlugin({
engine: 'wasm',
productionPrewarmFiles: ['src/a.tsx', 'src/dead.tsx', 'src/z.tsx'],
})
const compiler = createCompiler()
const warn = spyOn(console, 'warn').mockImplementation(() => {})
try {
plugin.apply(asCompiler(compiler))
// When the warm-up runs, the build goes on past the dead file.
expect(() => compiler.hooks.run.tap.mock.calls[0][1]()).not.toThrow()
// Then every file is still tried, and the dead one is named.
expect(codeExtractSpy.mock.calls.map(([filename]) => filename)).toEqual(
['src/a.tsx', 'src/dead.tsx', 'src/z.tsx'],
)
expect(warn).toHaveBeenCalledWith(
'[devup-ui] webpack stylesheet warm-up of src/dead.tsx failed: Cannot parse source: Unexpected token. The file is skipped; if webpack compiles it, the build fails with this error.',
)
} finally {
warn.mockRestore()
}
})

it('pre-warms without collapse, from no entries when they are a function', () => {
buildCanonicalMapSpy.mockReturnValue({})
const compiler = createCompiler()
Expand Down Expand Up @@ -1613,11 +1644,41 @@ describe('devupUIWebpackPlugin', () => {
throw new Error('extract boom')
})
const plugin = new DevupUIWebpackPlugin({ engine: 'wasm' })
// apply must still complete without throwing
const compiler = createCompiler()
plugin.apply(asCompiler(compiler))
compiler.hooks.run.tap.mock.calls[0][1]()
expect(codeExtractSpy).toHaveBeenCalled()
const warn = spyOn(console, 'warn').mockImplementation(() => {})
try {
// apply must still complete without throwing
const compiler = createCompiler()
plugin.apply(asCompiler(compiler))
compiler.hooks.run.tap.mock.calls[0][1]()
expect(codeExtractSpy).toHaveBeenCalled()
expect(warn).toHaveBeenCalledWith(
'[devup-ui] webpack stylesheet warm-up of src/parent.tsx failed: extract boom. The file is skipped; if webpack compiles it, the build fails with this error.',
)
} finally {
warn.mockRestore()
}
})

it('stops a generic pre-warm it cannot read, with a warning', () => {
const parent = resolve(process.cwd(), 'src', 'parent.tsx')
computeReachableFilesSpy.mockReturnValue([parent])
readFileSyncSpy.mockImplementation((path: string) => {
if (path === parent) throw new Error('read boom')
return '{}'
})
const plugin = new DevupUIWebpackPlugin({ engine: 'wasm' })
const warn = spyOn(console, 'warn').mockImplementation(() => {})
try {
const compiler = createCompiler()
plugin.apply(asCompiler(compiler))
compiler.hooks.run.tap.mock.calls[0][1]()
expect(codeExtractSpy).not.toHaveBeenCalled()
expect(warn).toHaveBeenCalledWith(
'[devup-ui] webpack stylesheet warm-up failed: read boom. A stylesheet webpack builds before every file it holds is extracted can miss their styles.',
)
} finally {
warn.mockRestore()
}
})

it('composes collapse + hoist and folds reach onto the canonical bucket', () => {
Expand Down
Loading
Loading