Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Feb 24, 2023
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
13 changes: 9 additions & 4 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> =
Expand Down Expand Up @@ -187,6 +186,7 @@ declare module 'next/link' {
}

declare module 'next' {
${originalNextModule.replaceAll('../dist', 'next/dist')}
export { Route }
}`
}
Expand Down Expand Up @@ -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)
})
Expand All @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions test/integration/app-types/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { Metadata } from 'next'

export const meta: Metadata = {
title: 'My App',
}

export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
Expand Down