Fixed next-types-plugin
causing Typescript errors when "type": "module"
is set (#49027)
### What? This fixes `next-types-plugin` causing Typescript to complain about CommonJS files importing ESM ones when `"type": "module"` is set but `experimental.typedRoutes` is not enabled. ### How? Always create a `.next/types/package.json` with `"type": "module"` instead of only doing so when `experimental.typedRoutes` is enabled. Fixes #49004
This commit is contained in:
parent
2343610fdf
commit
2d800df099
1 changed files with 19 additions and 15 deletions
|
@ -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)
|
||||
|
@ -673,6 +675,17 @@ 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) => {
|
||||
|
@ -680,17 +693,8 @@ 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 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
|
||||
|
|
Loading…
Reference in a new issue