Skip to content

Commit 25da5f0

Browse files
authored
Move next-env.d.ts to dist dir (#86752)
1 parent aa8a243 commit 25da5f0

File tree

25 files changed

+251
-387
lines changed

25 files changed

+251
-387
lines changed

packages/next/src/build/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ import {
224224
createRouteTypesManifest,
225225
writeRouteTypesManifest,
226226
writeValidatorFile,
227+
writeDynamicTypesFile,
227228
} from '../server/lib/router-utils/route-types-utils'
228229
import { Lockfile } from './lockfile'
229230
import {
@@ -1484,6 +1485,12 @@ export default async function build(
14841485
config
14851486
)
14861487
await writeValidatorFile(routeTypesManifest, validatorFilePath)
1488+
await writeDynamicTypesFile({
1489+
distDir,
1490+
imageImportsEnabled: !config.images.disableStaticImages,
1491+
hasPagesDir: !!pagesDir,
1492+
hasAppDir: !!appDir,
1493+
})
14871494
})
14881495

14891496
// Turbopack already handles conflicting app and page routes.

packages/next/src/build/type-check.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function verifyTypeScriptSetup(
2222
distDir: string,
2323
typeCheckPreflight: boolean,
2424
tsconfigPath: string | undefined,
25-
disableStaticImages: boolean,
2625
cacheDir: string | undefined,
2726
enableWorkerThreads: boolean | undefined,
2827
hasAppDir: boolean,
@@ -52,7 +51,6 @@ function verifyTypeScriptSetup(
5251
distDir,
5352
typeCheckPreflight,
5453
tsconfigPath,
55-
disableStaticImages,
5654
cacheDir,
5755
hasAppDir,
5856
hasPagesDir,
@@ -119,7 +117,6 @@ export async function startTypeChecking({
119117
config.distDir,
120118
!ignoreTypeScriptErrors,
121119
config.typescript.tsconfigPath,
122-
config.images.disableStaticImages,
123120
cacheDir,
124121
config.experimental.workerThreads,
125122
!!appDir,

packages/next/src/cli/next-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ async function runPlaywright(
142142
distDir: nextConfig.distDir,
143143
typeCheckPreflight: false,
144144
tsconfigPath: nextConfig.typescript.tsconfigPath,
145-
disableStaticImages: nextConfig.images.disableStaticImages,
146145
hasAppDir: !!appDir,
147146
hasPagesDir: !!pagesDir,
148147
isolatedDevBuild: nextConfig.experimental.isolatedDevBuild,

packages/next/src/cli/next-typegen.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
createRouteTypesManifest,
3030
writeRouteTypesManifest,
3131
writeValidatorFile,
32+
writeDynamicTypesFile,
3233
} from '../server/lib/router-utils/route-types-utils'
3334
import { writeCacheLifeTypes } from '../server/lib/router-utils/cache-life-type-utils'
3435
import { createValidFileMatcher } from '../server/lib/find-page-file'
@@ -59,7 +60,6 @@ const nextTypegen = async (
5960
distDir: nextConfig.distDir,
6061
typeCheckPreflight: false,
6162
tsconfigPath: nextConfig.typescript.tsconfigPath,
62-
disableStaticImages: nextConfig.images.disableStaticImages,
6363
hasAppDir: !!appDir,
6464
hasPagesDir: !!pagesDir,
6565
isolatedDevBuild: nextConfig.experimental.isolatedDevBuild,
@@ -171,6 +171,13 @@ const nextTypegen = async (
171171

172172
await writeValidatorFile(routeTypesManifest, validatorFilePath)
173173

174+
await writeDynamicTypesFile({
175+
distDir,
176+
imageImportsEnabled: !nextConfig.images.disableStaticImages,
177+
hasPagesDir: !!pagesDir,
178+
hasAppDir: !!appDir,
179+
})
180+
174181
// Generate cache-life types if cacheLife config exists
175182
const cacheLifeFilePath = join(distDir, 'types', 'cache-life.d.ts')
176183
writeCacheLifeTypes(nextConfig.cacheLife, cacheLifeFilePath)

packages/next/src/lib/typescript/writeAppTypeDeclarations.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

packages/next/src/lib/typescript/writeConfigurationDefaults.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ describe('writeConfigurationDefaults()', () => {
8484
"node_modules",
8585
],
8686
"include": [
87-
"next-env.d.ts",
8887
".next/types/**/*.ts",
8988
".next/dev/types/**/*.ts",
9089
"**/*.mts",
@@ -107,7 +106,7 @@ describe('writeConfigurationDefaults()', () => {
107106
- strict was set to false
108107
- noEmit was set to true
109108
- incremental was set to true
110-
- include was set to ['next-env.d.ts', '.next/types/**/*.ts', '.next/dev/types/**/*.ts', '**/*.mts', '**/*.ts', '**/*.tsx']
109+
- include was set to ['.next/types/**/*.ts', '.next/dev/types/**/*.ts', '**/*.mts', '**/*.ts', '**/*.tsx']
111110
- plugins was updated to add { name: 'next' }
112111
- exclude was set to ['node_modules']
113112

packages/next/src/lib/typescript/writeConfigurationDefaults.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,16 @@ export async function writeConfigurationDefaults(
282282
)
283283

284284
if (!('include' in userTsConfig)) {
285-
userTsConfig.include = hasAppDir
286-
? ['next-env.d.ts', ...nextAppTypes, '**/*.mts', '**/*.ts', '**/*.tsx']
287-
: ['next-env.d.ts', '**/*.mts', '**/*.ts', '**/*.tsx']
285+
// Always include .next/types for dynamic types (image imports, route types, etc.)
286+
userTsConfig.include = [...nextAppTypes, '**/*.mts', '**/*.ts', '**/*.tsx']
288287
suggestedActions.push(
289288
cyan('include') +
290289
' was set to ' +
291290
bold(
292-
hasAppDir
293-
? `['next-env.d.ts', ${nextAppTypes.map((type) => `'${type}'`).join(', ')}, '**/*.mts', '**/*.ts', '**/*.tsx']`
294-
: `['next-env.d.ts', '**/*.mts', '**/*.ts', '**/*.tsx']`
291+
`[${nextAppTypes.map((type) => `'${type}'`).join(', ')}, '**/*.mts', '**/*.ts', '**/*.tsx']`
295292
)
296293
)
297-
} else if (hasAppDir) {
294+
} else {
298295
const missingFromResolved = []
299296
for (const type of nextAppTypes) {
300297
if (!userTsConfig.include.includes(type)) {

packages/next/src/lib/verify-typescript-setup.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as log from '../build/output/log'
99

1010
import { getTypeScriptIntent } from './typescript/getTypeScriptIntent'
1111
import type { TypeCheckResult } from './typescript/runTypeCheck'
12-
import { writeAppTypeDeclarations } from './typescript/writeAppTypeDeclarations'
1312
import { writeConfigurationDefaults } from './typescript/writeConfigurationDefaults'
1413
import { installDependencies } from './install-dependencies'
1514
import { isCI } from '../server/ci-info'
@@ -39,7 +38,6 @@ export async function verifyTypeScriptSetup({
3938
cacheDir,
4039
tsconfigPath,
4140
typeCheckPreflight,
42-
disableStaticImages,
4341
hasAppDir,
4442
hasPagesDir,
4543
isolatedDevBuild,
@@ -52,7 +50,6 @@ export async function verifyTypeScriptSetup({
5250
cacheDir?: string
5351
tsconfigPath: string | undefined
5452
typeCheckPreflight: boolean
55-
disableStaticImages: boolean
5653
hasAppDir: boolean
5754
hasPagesDir: boolean
5855
isolatedDevBuild: boolean | undefined
@@ -138,16 +135,6 @@ export async function verifyTypeScriptSetup({
138135
hasPagesDir,
139136
isolatedDevBuild
140137
)
141-
// Write out the necessary `next-env.d.ts` file to correctly register
142-
// Next.js' types:
143-
await writeAppTypeDeclarations({
144-
baseDir: dir,
145-
distDir,
146-
imageImportsEnabled: !disableStaticImages,
147-
hasPagesDir,
148-
hasAppDir,
149-
})
150-
151138
let result
152139
if (typeCheckPreflight) {
153140
const { runTypeCheck } =

packages/next/src/server/lib/router-utils/route-types-utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
generateRouteTypesFile,
1111
generateLinkTypesFile,
1212
generateValidatorFile,
13+
generateDynamicTypesFile,
1314
} from './typegen'
1415
import { tryToParsePath } from '../../../lib/try-to-parse-path'
1516
import {
@@ -382,3 +383,35 @@ export async function writeValidatorFile(
382383

383384
await fs.promises.writeFile(filePath, generateValidatorFile(manifest))
384385
}
386+
387+
/**
388+
* Writes the dynamic types file (.next/types/next-env.d.ts) that contains
389+
* config-dependent type references (image imports, navigation compat, etc.)
390+
*/
391+
export async function writeDynamicTypesFile({
392+
distDir,
393+
imageImportsEnabled,
394+
hasPagesDir,
395+
hasAppDir,
396+
}: {
397+
distDir: string
398+
imageImportsEnabled: boolean
399+
hasPagesDir: boolean
400+
hasAppDir: boolean
401+
}) {
402+
const typesDir = path.join(distDir, 'types')
403+
const filePath = path.join(typesDir, 'next-env.d.ts')
404+
405+
// Directory should already be created by writeRouteTypesManifest, but ensure it exists
406+
if (!fs.existsSync(typesDir)) {
407+
await fs.promises.mkdir(typesDir, { recursive: true })
408+
}
409+
410+
const content = generateDynamicTypesFile({
411+
imageImportsEnabled,
412+
hasPagesDir,
413+
hasAppDir,
414+
})
415+
416+
await fs.promises.writeFile(filePath, content)
417+
}

packages/next/src/server/lib/router-utils/setup-dev-bundler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import {
8585
createRouteTypesManifest,
8686
writeRouteTypesManifest,
8787
writeValidatorFile,
88+
writeDynamicTypesFile,
8889
} from './route-types-utils'
8990
import { writeCacheLifeTypes } from './cache-life-type-utils'
9091
import { isParallelRouteSegment } from '../../../shared/lib/segment'
@@ -145,7 +146,6 @@ async function verifyTypeScript(opts: SetupOpts) {
145146
distDir: opts.nextConfig.distDir,
146147
typeCheckPreflight: false,
147148
tsconfigPath: opts.nextConfig.typescript.tsconfigPath,
148-
disableStaticImages: opts.nextConfig.images.disableStaticImages,
149149
hasAppDir: !!opts.appDir,
150150
hasPagesDir: !!opts.pagesDir,
151151
isolatedDevBuild: opts.nextConfig.experimental.isolatedDevBuild,
@@ -265,6 +265,12 @@ async function startWatcher(
265265
path.join(distTypesDir, 'routes.d.ts'),
266266
opts.nextConfig
267267
)
268+
await writeDynamicTypesFile({
269+
distDir,
270+
imageImportsEnabled: !opts.nextConfig.images.disableStaticImages,
271+
hasPagesDir: !!pagesDir,
272+
hasAppDir: !!appDir,
273+
})
268274

269275
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
270276
const routesManifest: DevRoutesManifest = {

0 commit comments

Comments
 (0)