Skip to content

Commit ec56020

Browse files
authored
Put native TS resolver for next config under --experimental-next-config-strip-types flag (#84675)
Using Node.js native TS resolution is great. However, enabling by default can be frustrating for the users as it has restrictions, such as requiring a file extension for imports, and does not read tsconfig.json (no import alias). It also emits a warning when using `next.config.ts` on a CommonJS project, which is the majority of the apps: ``` (node:68710) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///.../next.config.ts is not specified and it doesn't parse as CommonJS. Reparsing as ES module because module syntax was detected. This incurs a performance overhead. To eliminate this warning, add "type": "module" to /.../package.json. (Use `node --trace-warnings ...` to show where the warning was created) ``` Therefore, put this under an `--experimental-next-config-strip-types` flag for now.
1 parent 321c6af commit ec56020

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

.github/workflows/build_and_test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ jobs:
562562
with:
563563
nodeVersion: ${{ matrix.node }}
564564
afterBuild: |
565+
export __NEXT_NODE_NATIVE_TS_LOADER_ENABLED=true
565566
NEXT_TEST_MODE=dev NODE_OPTIONS=--experimental-transform-types node run-tests.js test/e2e/app-dir/next-config-ts-native-ts/**/*.test.ts test/e2e/app-dir/next-config-ts-native-mts/**/*.test.ts
566567
stepName: 'test-next-config-ts-native-ts-dev-${{ matrix.node }}'
567568

@@ -582,6 +583,7 @@ jobs:
582583
with:
583584
nodeVersion: ${{ matrix.node }}
584585
afterBuild: |
586+
export __NEXT_NODE_NATIVE_TS_LOADER_ENABLED=true
585587
NEXT_TEST_MODE=start NODE_OPTIONS=--experimental-transform-types node run-tests.js test/e2e/app-dir/next-config-ts-native-ts/**/*.test.ts test/e2e/app-dir/next-config-ts-native-mts/**/*.test.ts
586588
stepName: 'test-next-config-ts-native-ts-prod-${{ matrix.node }}'
587589

packages/next/src/bin/next.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,20 @@ program
150150
'--experimental-upload-trace, <traceUrl>',
151151
'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.'
152152
)
153-
.action((directory: string, options: NextBuildOptions) =>
153+
.option(
154+
'--experimental-next-config-strip-types',
155+
'Use Node.js native TypeScript resolution for next.config.(ts|mts)'
156+
)
157+
.action((directory: string, options: NextBuildOptions) => {
158+
if (options.experimentalNextConfigStripTypes) {
159+
process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'true'
160+
}
154161
// ensure process exits after build completes so open handles/connections
155162
// don't cause process to hang
156-
import('../cli/next-build.js').then((mod) =>
163+
return import('../cli/next-build.js').then((mod) =>
157164
mod.nextBuild(options, directory).then(() => process.exit(0))
158165
)
159-
)
166+
})
160167
.usage('[directory] [options]')
161168

162169
program
@@ -208,8 +215,15 @@ program
208215
'--experimental-upload-trace, <traceUrl>',
209216
'Reports a subset of the debugging trace to a remote HTTP URL. Includes sensitive data.'
210217
)
218+
.option(
219+
'--experimental-next-config-strip-types',
220+
'Use Node.js native TypeScript resolution for next.config.(ts|mts)'
221+
)
211222
.action(
212223
(directory: string, options: NextDevOptions, { _optionValueSources }) => {
224+
if (options.experimentalNextConfigStripTypes) {
225+
process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'true'
226+
}
213227
const portSource = _optionValueSources.port
214228
import('../cli/next-dev.js').then((mod) =>
215229
mod.nextDev(options, portSource, directory)
@@ -267,11 +281,18 @@ program
267281
'Specify the maximum amount of milliseconds to wait before closing inactive connections.'
268282
).argParser(parseValidPositiveInteger)
269283
)
270-
.action((directory: string, options: NextStartOptions) =>
271-
import('../cli/next-start.js').then((mod) =>
284+
.option(
285+
'--experimental-next-config-strip-types',
286+
'Use Node.js native TypeScript resolution for next.config.(ts|mts)'
287+
)
288+
.action((directory: string, options: NextStartOptions) => {
289+
if (options.experimentalNextConfigStripTypes) {
290+
process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'true'
291+
}
292+
return import('../cli/next-start.js').then((mod) =>
272293
mod.nextStart(options, directory)
273294
)
274-
)
295+
})
275296
.usage('[directory] [options]')
276297

277298
program

packages/next/src/build/next-config-ts/transpile-config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export async function transpileConfig({
116116
}) {
117117
try {
118118
// envs are passed to the workers and preserve the flag
119-
if (process.env.__NEXT_NODE_NATIVE_TS_LOADER_FAILED !== '1') {
119+
if (process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED === 'true') {
120120
try {
121121
// Node.js v22.10.0+
122122
// Value is 'strip' or 'transform' based on how the feature is enabled.
@@ -139,7 +139,7 @@ export async function transpileConfig({
139139
}
140140

141141
// Feature is not enabled, fallback to legacy resolution for current session.
142-
process.env.__NEXT_NODE_NATIVE_TS_LOADER_FAILED = '1'
142+
process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'false'
143143
} catch (cause) {
144144
warnOnce(
145145
`Failed to import "${configFileName}" using Node.js native TypeScript resolution.` +
@@ -148,7 +148,7 @@ export async function transpileConfig({
148148
{ cause }
149149
)
150150
// Once failed, fallback to legacy resolution for current session.
151-
process.env.__NEXT_NODE_NATIVE_TS_LOADER_FAILED = '1'
151+
process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'false'
152152
}
153153
}
154154

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type NextBuildOptions = {
2525
experimentalTurbo?: boolean
2626
experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env'
2727
experimentalUploadTrace?: string
28+
experimentalNextConfigStripTypes?: boolean
2829
}
2930

3031
const nextBuild = (options: NextBuildOptions, directory?: string) => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type NextDevOptions = {
5656
experimentalHttpsCert?: string
5757
experimentalHttpsCa?: string
5858
experimentalUploadTrace?: string
59+
experimentalNextConfigStripTypes?: boolean
5960
}
6061

6162
type PortSource = 'cli' | 'default' | 'env'

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type NextStartOptions = {
1313
port: number
1414
hostname?: string
1515
keepAliveTimeout?: number
16+
experimentalNextConfigStripTypes?: boolean
1617
}
1718

1819
/**

0 commit comments

Comments
 (0)