From 6e7a095b1a0bdbe89d9c0af47c3eeccf683ee230 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Mon, 27 Mar 2023 17:18:41 +0200 Subject: [PATCH 1/3] Fix import extensions for Node16/NodeNext 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' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` Adding the extensions will make it work with these projects and still maintain backwards compatibility with other TypeScript / module configurations. --- .../next/src/build/webpack/plugins/next-types-plugin.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/next/src/build/webpack/plugins/next-types-plugin.ts b/packages/next/src/build/webpack/plugins/next-types-plugin.ts index ea1a366a06458..6077ddcec11f0 100644 --- a/packages/next/src/build/webpack/plugins/next-types-plugin.ts +++ b/packages/next/src/build/webpack/plugins/next-types-plugin.ts @@ -44,9 +44,9 @@ function createTypeGuardFile( } ) { return `// File: ${fullPath} -import * as entry from '${relativePath}' -import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface' -import type { NextRequest } from 'next/server' +import * as entry from '${relativePath}.js' +import type { ResolvingMetadata } from 'next/dist/lib/metadata/types/metadata-interface.js' +import type { NextRequest } from 'next/server.js' type TEntry = typeof entry From cec7e981cf327a867f3dd742c5311518baefdb2c Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Thu, 30 Mar 2023 11:17:03 +0200 Subject: [PATCH 2/3] Add package.json for next-types-plugin + Node16 --- .../src/build/webpack/plugins/next-types-plugin.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/next/src/build/webpack/plugins/next-types-plugin.ts b/packages/next/src/build/webpack/plugins/next-types-plugin.ts index ffb921f03dddd..c9e6aadb127e1 100644 --- a/packages/next/src/build/webpack/plugins/next-types-plugin.ts +++ b/packages/next/src/build/webpack/plugins/next-types-plugin.ts @@ -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 } From 92b344ee9f413813cfd529a094ea59b90f823aee Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Thu, 30 Mar 2023 12:29:42 +0200 Subject: [PATCH 3/3] Fix code style --- packages/next/src/build/webpack/plugins/next-types-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/build/webpack/plugins/next-types-plugin.ts b/packages/next/src/build/webpack/plugins/next-types-plugin.ts index c9e6aadb127e1..25a42e0e9bcc7 100644 --- a/packages/next/src/build/webpack/plugins/next-types-plugin.ts +++ b/packages/next/src/build/webpack/plugins/next-types-plugin.ts @@ -642,7 +642,7 @@ export class NextTypesPlugin { const packageJsonAssetPath = assetDirRelative + '/' + normalizePathSep(packageJsonTypePath) assets[packageJsonAssetPath] = new sources.RawSource( - '{ "type": "module" }' + '{"type": "module"}' ) as unknown as webpack.sources.RawSource const linkTypePath = path.join('types', 'link.d.ts')