diff --git a/.changepacks/changepack_log_next_route_entries.json b/.changepacks/changepack_log_next_route_entries.json new file mode 100644 index 000000000..ce1422851 --- /dev/null +++ b/.changepacks/changepack_log_next_route_entries.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch","packages/plugin-utils/package.json":"Patch"},"note":"Count Pages Router modules and every App Router route file as Next route entries, so Pages Router projects get a deterministic CSS completion set","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/plugin-utils/src/import-graph.test.ts b/packages/plugin-utils/src/import-graph.test.ts index 7f0069c24..d1143afd8 100644 --- a/packages/plugin-utils/src/import-graph.test.ts +++ b/packages/plugin-utils/src/import-graph.test.ts @@ -853,6 +853,55 @@ describe('computeCompiledFiles', () => { expect(compiled).not.toContain('src/orphan.tsx') }) + it('treats every Pages Router module as a route entry', () => { + writeFixture('src/pages/_app.tsx', "import '../shell'\n") + writeFixture('src/pages/_document.tsx', 'export default 1\n') + writeFixture('src/pages/index.tsx', 'export default 1\n') + writeFixture('src/pages/blog/[slug].tsx', "import '../../post'\n") + writeFixture('src/pages/api/hello.ts', 'export default 1\n') + writeFixture('src/shell.tsx', 'export const S = 1\n') + writeFixture('src/post.tsx', 'export const P = 1\n') + writeFixture('src/orphan.tsx', 'export const O = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir })).toEqual([ + 'src/pages/_app.tsx', + 'src/pages/_document.tsx', + 'src/pages/api/hello.ts', + 'src/pages/blog/[slug].tsx', + 'src/pages/index.tsx', + 'src/post.tsx', + 'src/shell.tsx', + ]) + }) + + it('finds a Pages Router at the project root', () => { + writeFixture('pages/about.jsx', "import './about-body'\n") + writeFixture('pages/about-body.jsx', 'export const B = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir: cwd })).toEqual([ + 'pages/about-body.jsx', + 'pages/about.jsx', + ]) + }) + + it('includes App Router files no page sits below', () => { + writeFixture('src/app/api/route.ts', "import '../../handler'\n") + writeFixture('src/app/@modal/default.tsx', "import '../../modal'\n") + writeFixture('src/app/not-found.tsx', 'export default 1\n') + writeFixture('src/app/global-error.tsx', 'export default 1\n') + writeFixture('src/handler.ts', 'export const H = 1\n') + writeFixture('src/modal.tsx', 'export const M = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir })).toEqual([ + 'src/app/@modal/default.tsx', + 'src/app/api/route.ts', + 'src/app/global-error.tsx', + 'src/app/not-found.tsx', + 'src/handler.ts', + 'src/modal.tsx', + ]) + }) + it('returns an empty list when no leaf route exists', () => { writeFixture('src/index.tsx', "import './a'\n") writeFixture('src/a.tsx', 'export const A = 1\n') diff --git a/packages/plugin-utils/src/import-graph.ts b/packages/plugin-utils/src/import-graph.ts index 9bf15167e..fb147bdea 100644 --- a/packages/plugin-utils/src/import-graph.ts +++ b/packages/plugin-utils/src/import-graph.ts @@ -78,6 +78,19 @@ const testFileRegex = /\.(?:test|spec)\.[mc]?[jt]sx?$/ const routeFileRegex = /(^|\/)(page|layout|template|default|loading|error|not-found|global-error)\.(tsx|ts|jsx|js)$/ const leafRouteFileRegex = /(^|\/)page\.(tsx|ts|jsx|js)$/ +// Every App Router file Next compiles as part of a route tree: pages, route +// handlers, and each segment file, including the ones (parallel-route +// `default`, root `not-found`) no page sits below. +const appRouterFileRegex = + /(^|\/)(page|route|layout|template|default|loading|error|not-found|forbidden|unauthorized|global-error|global-not-found)\.(tsx|ts|jsx|js)$/ +// Every module in the Pages Router directory is a route, `_app`, `_document` +// and `_error` included. +const pagesRouterFileRegex = /^pages\/.+\.(tsx|ts|jsx|js)$/ + +/** Whether Next compiles this `srcDir`-relative file as a route entry. */ +function isCompiledRouteEntry(relPath: string): boolean { + return appRouterFileRegex.test(relPath) || pagesRouterFileRegex.test(relPath) +} let cachedOxcParser: false | OxcParser | undefined @@ -292,8 +305,10 @@ export interface ComputeCompiledFilesOptions { /** * Every source file the bundler will compile for the app's routes: the closure - * of all leaf routes (plus their ancestor route shells) over BOTH static and - * dynamic `import()` edges. + * of every route entry over BOTH static and dynamic `import()` edges. Route + * entries are all App Router route files (pages, route handlers, layouts and + * the other segment files) and every Pages Router module (`pages/**`, + * `_app`/`_document` included). * * This is deliberately WIDER than `computeFileRoutes`, whose closure stops at * static edges because a lazily-loaded file belongs to its own chunk for @@ -305,7 +320,7 @@ export interface ComputeCompiledFilesOptions { * * Keys are POSIX paths relative to `cwd`, matching the extraction filename the * loader posts. Returns a sorted array (deterministic across runs). Empty when - * no leaf route is detected, which keeps callers on their idle fallback rather + * no route entry is detected, which keeps callers on their idle fallback rather * than blocking on a set that can never complete. */ export function computeCompiledFiles( @@ -323,16 +338,10 @@ export function computeCompiledFiles( allImports.set(file, edges) } - const routeShellFilesByDir = getRouteShellFilesByDir(files, srcDir) const compiled = new Set() for (const file of files) { - if (!leafRouteFileRegex.test(toPosixRelative(srcDir, file))) continue - for (const reached of getLeafRouteClosure( - file, - srcDir, - allImports, - routeShellFilesByDir, - )) { + if (!isCompiledRouteEntry(toPosixRelative(srcDir, file))) continue + for (const reached of getStaticClosure(file, allImports)) { compiled.add(reached) } }