Unverified Commit 9df7df13 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

fix(ts): correctly augment `next` when `typedRoutes: true` (#46332)

parent 8d83d85e
......@@ -123,12 +123,11 @@ const edgeRouteTypes: string[] = []
export const pageFiles = new Set<string>()
function createRouteDefinitions() {
function createRouteDefinitions(originalNextModule: string) {
const fallback =
!edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : ''
return `
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
return `type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type Suffix = '' | SearchOrHash
type SafeSlug<S extends string> =
......@@ -187,6 +186,7 @@ declare module 'next/link' {
}
declare module 'next' {
${originalNextModule.replaceAll('../dist', 'next/dist')}
export { Route }
}`
}
......@@ -383,6 +383,11 @@ export class NextTypesPlugin {
await Promise.all(promises)
if (this.typedRoutes) {
const originalNextModule = await fs.readFile(
path.join(__dirname, '../../../../types/index.d.ts'),
'utf-8'
)
pageFiles.forEach((file) => {
this.collectPage(file)
})
......@@ -391,7 +396,7 @@ export class NextTypesPlugin {
const assetPath =
assetDirRelative + '/' + linkTypePath.replace(/\\/g, '/')
assets[assetPath] = new sources.RawSource(
createRouteDefinitions()
createRouteDefinitions(originalNextModule)
) as unknown as webpack.sources.RawSource
}
......
import { Metadata } from 'next'
export const meta: Metadata = {
title: 'My App',
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
......
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