Skip to content

Commit

Permalink
Fix next-types-plugin generated files for Node16/NodeNext (#47571)
Browse files Browse the repository at this point in the history
### What / Why / How?

In a project with `"type": "module"` in `package.json` and a `compilerOptions.moduleResolution` of `Node16` or `NodeNext` in `tsconfig.json`, running `tsc` will yield errors like the following:

```
.next/types/app/[...not_found]/page.ts:1:40 - error TS2307: Cannot find module 'next/dist/lib/metadata/types/metadata-interface' or its corresponding type declarations.

1 import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface';
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.next/types/app/layout.ts:2:24 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../../../app/layout.jsx'?

2 import * as entry from '../../../app/layout'
                         ~~~~~~~~~~~~~~~~~~~~~

.next/types/app/layout.ts:3:40 - error TS2307: Cannot find module 'next/dist/lib/metadata/types/metadata-interface' or its corresponding type declarations.

3 import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface'
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

These files are checked because of the following `tsconfig.json` line which Next.js adds:

```diff
{
  "include": [
+    ".next/types/**/*.ts",
  ]
}
```

Adding the extensions will make it work with these projects and still maintain backwards compatibility with other TypeScript / module configurations.
  • Loading branch information
karlhorky authored Mar 30, 2023
1 parent 99cdb36 commit e7371bc
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ function createTypeGuardFile(
}
) {
return `// File: ${fullPath}
import * as entry from '${relativePath}'
import * as entry from '${relativePath}.js'
${
options.type === 'route'
? `import type { NextRequest } from 'next/server'`
: `import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface'`
? `import type { NextRequest } from 'next/server.js'`
: `import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface.js'`
}
type TEntry = typeof entry
Expand Down Expand Up @@ -637,10 +637,18 @@ export class NextTypesPlugin {
})
}

// Support tsconfig values for "moduleResolution": "Node16" or "NodeNext"
const packageJsonTypePath = path.join('types', 'package.json')
const packageJsonAssetPath =
assetDirRelative + '/' + normalizePathSep(packageJsonTypePath)
assets[packageJsonAssetPath] = new sources.RawSource(
'{"type": "module"}'
) as unknown as webpack.sources.RawSource

const linkTypePath = path.join('types', 'link.d.ts')
const assetPath =
const linkAssetPath =
assetDirRelative + '/' + normalizePathSep(linkTypePath)
assets[assetPath] = new sources.RawSource(
assets[linkAssetPath] = new sources.RawSource(
createRouteDefinitions()
) as unknown as webpack.sources.RawSource
}
Expand Down

0 comments on commit e7371bc

Please sign in to comment.