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 next-types-plugin generated files for Node16/NodeNext #47571

Merged
merged 5 commits into from Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Expand Up @@ -44,11 +44,11 @@ function createTypeGuardFile(
}
) {
return `// File: ${fullPath}
import * as entry from '${relativePath}'
import * as entry from '${relativePath}.js'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to always import .js here even if the user could have e.g. .ts?

Copy link
Contributor Author

@karlhorky karlhorky Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, .js is the correct extension for importing a .ts / .tsx file in TypeScript Node.js ESM with fully-specified import paths:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

@karlhorky karlhorky Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also somewhat related (although maybe these type declaration files are not consumed by webpack anyway), this resolve.extensionAlias webpack configuration option was inspired by this module resolution behavior:

And it's being configured in webpack in Next.js too:

${
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