Unverified Commit 8a92f28f authored by Ben Gubler's avatar Ben Gubler Committed by GitHub

[backport] fix: avoid importing types that will be unused (#82856) (#83026)

parent 19617abe
...@@ -579,7 +579,23 @@ export function generateValidatorFile( ...@@ -579,7 +579,23 @@ export function generateValidatorFile(
} }
// Build import statement based on what's actually needed // Build import statement based on what's actually needed
const routeImports = ['AppRoutes', 'LayoutRoutes', 'ParamMap'] const routeImports = []
// Only import AppRoutes if there are app pages
if (appPageValidations) {
routeImports.push('AppRoutes')
}
// Only import LayoutRoutes if there are layouts
if (layoutValidations) {
routeImports.push('LayoutRoutes')
}
// Only import ParamMap if there are routes that use it
if (appPageValidations || layoutValidations || appRouteHandlerValidations) {
routeImports.push('ParamMap')
}
if (hasAppRouteHandlers) { if (hasAppRouteHandlers) {
routeImports.push('AppRouteHandlerRoutes') routeImports.push('AppRouteHandlerRoutes')
} }
...@@ -593,13 +609,18 @@ export function generateValidatorFile( ...@@ -593,13 +609,18 @@ export function generateValidatorFile(
? "import type { NextRequest } from 'next/server.js'\n" ? "import type { NextRequest } from 'next/server.js'\n"
: '' : ''
// Only import metadata types if there are App Router pages or layouts that might use them
const metadataImport =
appPageValidations || layoutValidations
? 'import type { ResolvingMetadata, ResolvingViewport } from "next/dist/lib/metadata/types/metadata-interface.js"\n'
: ''
return `// This file is generated automatically by Next.js return `// This file is generated automatically by Next.js
// Do not edit this file manually // Do not edit this file manually
// This file validates that all pages and layouts export the correct types // This file validates that all pages and layouts export the correct types
${routeImportStatement} ${routeImportStatement}
import type { ResolvingMetadata, ResolvingViewport } from "next/dist/lib/metadata/types/metadata-interface.js" ${metadataImport}${nextRequestImport}
${nextRequestImport}
${typeDefinitions} ${typeDefinitions}
${appPageValidations} ${appPageValidations}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment