Skip to content

Fixed next-types-plugin causing Typescript errors when "type": "module" is set #49027

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

Merged
Merged
34 changes: 19 additions & 15 deletions packages/next/src/build/webpack/plugins/next-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,13 @@ export class NextTypesPlugin {
appTypesBasePath,
relativePathToApp.replace(/\.(js|jsx|ts|tsx|mjs)$/, '.ts')
)
const relativeImportPath = path
.join(this.getRelativePathFromAppTypesDir(relativePathToApp))
.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
.replace(/\\/g, '/')
const assetPath = assetDirRelative + '/' + normalizePathSep(typePath)
const relativeImportPath = normalizePathSep(
path
.join(this.getRelativePathFromAppTypesDir(relativePathToApp))
.replace(/\.(js|jsx|ts|tsx|mjs)$/, '')
)

const assetPath = path.join(assetDirRelative, typePath)

if (IS_LAYOUT) {
const slots = await collectNamedSlots(mod.resource)
Expand Down Expand Up @@ -673,24 +675,26 @@ export class NextTypesPlugin {

await Promise.all(promises)

// Support `"moduleResolution": "Node16" | "NodeNext"` with `"type": "module"`

const packageJsonAssetPath = path.join(
assetDirRelative,
'types/package.json'
)

assets[packageJsonAssetPath] = new sources.RawSource(
'{"type": "module"}'
) as unknown as webpack.sources.RawSource

if (this.typedRoutes) {
if (this.dev && !this.isEdgeServer) {
devPageFiles.forEach((file) => {
this.collectPage(file)
})
}

// 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 linkAssetPath = path.join(assetDirRelative, 'types/link.d.ts')

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