Unverified Commit d165670e authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

feat: use Node.js native TS resolver for `next.config.ts` (#83240)

### Why?

Since `next.config.ts` was resolved via require-hook, it had a
restriction with Native ESM syntax, like top-level await, dynamic
import, and Node.js ESM APIs like `import.meta.url`. There were previous
attempts to resolve this via the ESM loader, but those approaches
(https://github.com/vercel/next.js/pull/68365,
https://github.com/vercel/next.js/pull/83120) ended up adding ~39ms
compared to the require-hook due to the loader registration time.

However, Node.js natively supports TS since v22.7.0 under the
experimental flag, and was enabled by default since v23.6.0. Therefore,
this PR feature detects the Node.js version and uses its feature to
resolve `next.config.ts` and allows native ESM syntax.

As a comparison, the usage of ESM loader vs Native TS on a "Large App":

- Duration: ~65% faster (186 ms → 66 ms)
- RSS: ~41% lower (80 MB → 47 MB)
- Heap: ~78% lower (18 MB → 4 MB)

<details><summary>Benchmark Details</summary>
<p>

| Type | Transpile Duration (ms) | Resident Set Size (MB) | Heap Used
(MB) |

|---------|--------------------------|------------------------|----------------|
| ESM | 186.04 | 79.94 | 17.68 |
| Native | 66.09 | 47.32 | 3.81 |
| **Δ (Abs)** | -119.95 | -32.62 | -13.87 |
| **Δ (%)** | -64.5% | -40.8% | -78.4% |

</p>
</details> 

### How?

Used Node.js Native TS support (since v22.7.0). Feature detected with
`process.features.typescript` (since v22.10.0), and fallback to the
legacy resolution for the current session if it throws. This feature
follows the restriction of the Native TS support. The notable
restrictions are:

- Requires extensions for file imports
- Doesn't read tsconfig.json (no import alias)
- Requires `with { type: 'json' }` when importing JSON

For details, see [Node.js Type
stripping](https://nodejs.org/api/typescript.html#type-stripping).

See the changes below to get the idea of what is restricted or newly
allowed.

- Changes to fixtures due to restrictions:
https://github.com/vercel/next.js/pull/83240/commits/de1bec89f1976aa6f0cba03aa38cae4c1977abec
- Additions to fixtures due to ESM support:
https://github.com/vercel/next.js/pull/83240/commits/622a2a828693422aa511793af56734f40569d9f5

Added `next-config-ts-native-ts` tests on CI to run on Node.js v22 and
v24.

## Benchmark

#### Hello World App

- Duration: ~68% faster (102 ms → 32 ms)
- RSS: ~65% lower (72 MB → 25 MB)
- Heap: ~87% lower (31 MB → 4 MB)

<details><summary>Benchmark Details</summary>
<p>

| Attempt | Transpile Duration (ms) | Resident Set Size (MB) | Heap Used
(MB) |

|-----------|--------------------------|------------------------|----------------|
| CJS 1 | 104.03 | 69.16 | 30.32 |
| CJS 2 | 100.35 | 59.95 | 30.14 |
| CJS 3 | 100.77 | 75.83 | 32.18 |
| CJS 4 | 103.43 | 77.33 | 30.13 |
| CJS 5 | 100.82 | 76.30 | 30.22 |
| Native 1 | 33.75 | 25.19 | 3.84 |
| Native 2 | 28.71 | 21.72 | 3.85 |
| Native 3 | 33.49 | 28.95 | 3.85 |
| Native 4 | 32.78 | 24.97 | 3.85 |
| Native 5 | 33.43 | 24.89 | 3.85 |
| **CJS Avg** | 101.88 | 71.71 | 30.60 |
| **Native Avg**| 32.43 | 25.14 | 3.85 |
| **Δ (Abs)** | -69.45 | -46.57 | -26.75 |
| **Δ (%)** | -68.2% | -64.9% | -87.4% |

</p>
</details> 

#### Large App

> Benchmarked from a large repository that imports packages like
`@next/bundle-analyzer`.

- Duration: ~56% faster (152 ms → 66 ms)
- RSS: ~30% lower (68 MB → 47 MB)
- Heap: ~78% lower (17 MB → 4 MB)

<details><summary>Benchmark Details</summary>
<p>

| Attempt | Transpile Duration (ms) | Resident Set Size (MB) | Heap Used
(MB) |

|-----------|--------------------------|------------------------|----------------|
| CJS 1 | 154.35 | 67.73 | 17.16 |
| CJS 2 | 148.02 | 61.33 | 17.32 |
| CJS 3 | 152.44 | 66.06 | 17.14 |
| CJS 4 | 149.89 | 74.33 | 17.17 |
| CJS 5 | 156.27 | 68.20 | 17.25 |
| Native 1 | 68.01 | 34.66 | 5.51 |
| Native 2 | 66.41 | 53.83 | 5.48 |
| Native 3 | 62.39 | 45.80 | 1.48 |
| Native 4 | 65.20 | 52.02 | 1.35 |
| Native 5 | 68.44 | 50.31 | 5.25 |
| **CJS Avg** | 152.19 | 67.53 | 17.21 |
| **Native Avg**| 66.09 | 47.32 | 3.81 |
| **Δ (Abs)** | -86.10 | -20.21 | -13.39 |
| **Δ (%)** | -56.6% | -29.9% | -77.8% |

</p>
</details>

Fixes https://github.com/vercel/next.js/issues/67765
Fixes https://github.com/vercel/next.js/issues/71705
Closes [NDX-494](https://linear.app/vercel/issue/NDX-494)

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: 's avatargraphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: 's avatarSebastian "Sebbie" Silbermann <silbermann.sebastian@gmail.com>
parent 85a2a029
...@@ -521,6 +521,50 @@ jobs: ...@@ -521,6 +521,50 @@ jobs:
secrets: inherit secrets: inherit
# TODO: Remove this once we bump minimum Node.js version to v22
test-next-config-ts-native-ts-dev:
name: test next-config-ts-native-ts dev
needs: ['changes', 'build-next', 'build-native']
if: ${{ needs.changes.outputs.docs-only == 'false' }}
strategy:
fail-fast: false
matrix:
node: [22, 24]
uses: ./.github/workflows/build_reusable.yml
with:
nodeVersion: ${{ matrix.node }}
afterBuild: |
export NEXT_TEST_MODE=dev
export NODE_OPTIONS=--experimental-transform-types
node run-tests.js test/e2e/app-dir/next-config-ts-native-ts/**/*.test.ts
stepName: 'test-next-config-ts-native-ts-dev-${{ matrix.node }}'
secrets: inherit
# TODO: Remove this once we bump minimum Node.js version to v22
test-next-config-ts-native-ts-prod:
name: test next-config-ts-native-ts prod
needs: ['changes', 'build-next', 'build-native']
if: ${{ needs.changes.outputs.docs-only == 'false' }}
strategy:
fail-fast: false
matrix:
node: [22, 24]
uses: ./.github/workflows/build_reusable.yml
with:
nodeVersion: ${{ matrix.node }}
afterBuild: |
export NEXT_TEST_MODE=start
export NODE_OPTIONS=--experimental-transform-types
node run-tests.js test/e2e/app-dir/next-config-ts-native-ts/**/*.test.ts
stepName: 'test-next-config-ts-native-ts-prod-${{ matrix.node }}'
secrets: inherit
test-unit-windows: test-unit-windows:
name: test unit windows name: test unit windows
needs: ['changes', 'build-native', 'build-native-windows'] needs: ['changes', 'build-native', 'build-native-windows']
...@@ -956,6 +1000,8 @@ jobs: ...@@ -956,6 +1000,8 @@ jobs:
'validate-docs-links', 'validate-docs-links',
'check-types-precompiled', 'check-types-precompiled',
'test-unit', 'test-unit',
'test-next-config-ts-native-ts-dev',
'test-next-config-ts-native-ts-prod',
'test-dev', 'test-dev',
'test-prod', 'test-prod',
'test-integration', 'test-integration',
......
...@@ -3,9 +3,11 @@ import type { CompilerOptions } from 'typescript' ...@@ -3,9 +3,11 @@ import type { CompilerOptions } from 'typescript'
import { resolve } from 'node:path' import { resolve } from 'node:path'
import { readFile } from 'node:fs/promises' import { readFile } from 'node:fs/promises'
import { pathToFileURL } from 'node:url'
import { deregisterHook, registerHook, requireFromString } from './require-hook' import { deregisterHook, registerHook, requireFromString } from './require-hook'
import { warn } from '../output/log' import { warn, warnOnce } from '../output/log'
import { installDependencies } from '../../lib/install-dependencies' import { installDependencies } from '../../lib/install-dependencies'
import { getNodeOptionsArgs } from '../../server/lib/utils'
function resolveSWCOptions( function resolveSWCOptions(
cwd: string, cwd: string,
...@@ -103,6 +105,8 @@ async function getTsConfig(cwd: string): Promise<CompilerOptions> { ...@@ -103,6 +105,8 @@ async function getTsConfig(cwd: string): Promise<CompilerOptions> {
return parsedCommandLine.options return parsedCommandLine.options
} }
let useNodeNativeTSLoader = true
export async function transpileConfig({ export async function transpileConfig({
nextConfigPath, nextConfigPath,
configFileName, configFileName,
...@@ -113,6 +117,39 @@ export async function transpileConfig({ ...@@ -113,6 +117,39 @@ export async function transpileConfig({
cwd: string cwd: string
}) { }) {
try { try {
if (useNodeNativeTSLoader) {
try {
// Node.js v22.10.0+
// Value is 'strip' or 'transform' based on how the feature is enabled.
// https://nodejs.org/api/process.html#processfeaturestypescript
if ((process.features as any).typescript) {
return import(pathToFileURL(nextConfigPath).href)
}
if (
getNodeOptionsArgs().includes('--no-experimental-strip-types') ||
process.execArgv.includes('--no-experimental-strip-types')
) {
// TODO: Add Next.js docs link.
warnOnce(
`Skipped resolving "${configFileName}" using Node.js native TypeScript resolution because it was disabled by the "--no-experimental-strip-types" flag.` +
' Falling back to legacy resolution.'
)
}
// Feature is not enabled, fallback to legacy resolution for current session.
useNodeNativeTSLoader = false
} catch (cause) {
warnOnce(
`Failed to import "${configFileName}" using Node.js native TypeScript resolution.` +
' Falling back to legacy resolution.',
{ cause }
)
// Once failed, fallback to legacy resolution for current session.
useNodeNativeTSLoader = false
}
}
// Ensure TypeScript is installed to use the API. // Ensure TypeScript is installed to use the API.
await verifyTypeScriptSetup(cwd, configFileName) await verifyTypeScriptSetup(cwd, configFileName)
const compilerOptions = await getTsConfig(cwd) const compilerOptions = await getTsConfig(cwd)
......
...@@ -118,7 +118,7 @@ export const tokenizeArgs = (input: string): string[] => { ...@@ -118,7 +118,7 @@ export const tokenizeArgs = (input: string): string[] => {
* *
* @returns An array of strings with the node options. * @returns An array of strings with the node options.
*/ */
const getNodeOptionsArgs = () => { export const getNodeOptionsArgs = () => {
if (!process.env.NODE_OPTIONS) return [] if (!process.env.NODE_OPTIONS) return []
return tokenizeArgs(process.env.NODE_OPTIONS) return tokenizeArgs(process.env.NODE_OPTIONS)
......
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-async-function-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should support config as async function (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-async-function-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support config as async function (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfigAsyncFunction = async (phase, { defaultConfig }) => {
const nextConfig: NextConfig = {
...defaultConfig,
env: {
foo: phase ? 'foo' : 'bar',
},
}
return nextConfig
}
export default nextConfigAsyncFunction
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-dynamic-import-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support dynamic import (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig = async () => {
const { foo } = await import('./foo.ts')
const nextConfig: NextConfig = {
env: {
foo,
},
}
return nextConfig
}
export default nextConfig
{
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-export-as-default-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should support export as default (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-export-as-default-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support export as default (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig: NextConfig = {
env: {
foo: 'foo',
},
}
export { nextConfig as default }
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-export-default-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should support export default (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-export-default-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support export default (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
export default {
env: {
foo: 'foo',
},
} satisfies NextConfig
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<p>
{JSON.stringify([
process.env.cjs,
process.env.mjs,
process.env.jsCJS,
process.env.jsESM,
])}
</p>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-from-node-modules-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should import from node_modules (CJS)', async () => {
const $ = await next.render$('/')
const text = $('p').text()
expect(text).toContain('cjs')
expect(text).toContain('mjs')
expect(text).toContain('jsCJS')
expect(text).toContain('jsESM')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-from-node-modules-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should import from node_modules (ESM)', async () => {
const $ = await next.render$('/')
const text = $('p').text()
expect(text).toContain('cjs')
expect(text).toContain('mjs')
expect(text).toContain('jsCJS')
expect(text).toContain('jsESM')
})
})
import type { NextConfig } from 'next'
import cjs from 'cjs'
import mjs from 'mjs'
import jsCJS from 'js-cjs'
import jsESM from 'js-esm'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig: NextConfig = {
env: {
cjs,
mjs,
jsCJS,
jsESM,
},
}
export default nextConfig
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<p>
{JSON.stringify([
process.env.js,
process.env.cjs,
process.env.mjs,
process.env.cts,
process.env.mts,
process.env.ts,
])}
</p>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-js-extensions-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should import js extensions (CJS)', async () => {
const $ = await next.render$('/')
const text = $('p').text()
expect(text).toContain('jsCJS')
expect(text).toContain('cjs')
expect(text).toContain('mjs')
expect(text).toContain('cts')
expect(text).toContain('mts')
expect(text).toContain('ts')
})
})
import type { NextConfig } from 'next'
import cjs from './fixtures/cjs.cjs'
import mjs from './fixtures/mjs.mjs'
import * as cts from './fixtures/cts.cts'
import mts from './fixtures/mts.mts'
import ts from './fixtures/ts.ts'
import js from './fixtures/js-cjs.js'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig: NextConfig = {
env: {
cjs,
mjs,
// Node.js native TS can't handle `export =` syntax.
cts: (cts as any).default,
mts,
ts,
js,
},
}
export default nextConfig
{
"compilerOptions": {
"allowImportingTsExtensions": true,
"esModuleInterop": true,
"allowJs": true
}
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<p>
{JSON.stringify([
process.env.js,
process.env.cjs,
process.env.mjs,
process.env.cts,
process.env.mts,
process.env.ts,
])}
</p>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-js-extensions-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should import js extensions (ESM)', async () => {
const $ = await next.render$('/')
const text = $('p').text()
expect(text).toContain('jsESM')
expect(text).toContain('cjs')
expect(text).toContain('mjs')
expect(text).toContain('cts')
expect(text).toContain('mts')
expect(text).toContain('ts')
})
})
import type { NextConfig } from 'next'
import cjs from './fixtures/cjs.cjs'
import mjs from './fixtures/mjs.mjs'
import * as cts from './fixtures/cts.cts'
import mts from './fixtures/mts.mts'
import ts from './fixtures/ts.ts'
import js from './fixtures/js-esm.js'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig: NextConfig = {
env: {
cjs,
mjs,
// Node.js native TS can't handle `export =` syntax.
cts: (cts as any).default,
mts,
ts,
js,
},
}
export default nextConfig
{
"compilerOptions": {
"allowImportingTsExtensions": true,
"esModuleInterop": true,
"allowJs": true
}
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-json-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should support import json (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-import-json-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support import json (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
import fooJson from './foo.json' with { type: 'json' }
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
export default {
env: {
foo: fooJson.foo,
},
} satisfies NextConfig
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foobarbaz}</p>
}
import { baz } from './baz.ts'
export const barbaz = 'bar' + baz
import { barbaz } from './bar.ts'
export const foobarbaz = 'foo' + barbaz
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-nested-imports-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should handle nested imports (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foobarbaz')
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-nested-imports-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should handle nested imports (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foobarbaz')
})
})
import type { NextConfig } from 'next'
import { foobarbaz } from './foo.ts'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const nextConfig: NextConfig = {
env: {
foobarbaz,
},
}
export default nextConfig
{
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-node-api-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
})
it('should be able to use Node.js API (CJS)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
import fs from 'node:fs'
import { join } from 'node:path'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
const foo = fs.readFileSync(join('.', 'foo.txt'), 'utf8')
const nextConfig: NextConfig = {
env: {
foo,
},
}
export default nextConfig
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-node-api-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should be able to use Node.js API (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
import fs from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
// import.meta.url only works in ESM
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const foo = fs.readFileSync(join(__dirname, 'foo.txt'), 'utf8')
const nextConfig: NextConfig = {
env: {
foo,
},
}
export default nextConfig
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-top-level-await-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next } = nextTestSetup({
files: __dirname,
packageJson: {
type: 'module',
},
})
it('should support top-level await (ESM)', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
})
})
import type { NextConfig } from 'next'
import { getFoo } from './get-foo.ts'
const foo = await getFoo()
const nextConfig: NextConfig = {
env: {
foo,
},
}
export default nextConfig
{
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>{process.env.foo}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-type-error-cjs', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
})
if (skipped) {
return
}
it('should throw with type error on build (CJS)', async () => {
if (isNextDev) {
await next.start()
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
} else {
const { cliOutput } = await next.build()
await expect(cliOutput).toContain(
`Type 'string' is not assignable to type 'number'.`
)
}
})
})
import { nextTestSetup } from 'e2e-utils'
describe('next-config-ts-type-error-esm', () => {
// TODO: Remove this once we bump minimum Node.js version to v22
if (!(process.features as any).typescript) {
it.skip('requires `process.features.typescript` to feature detect Node.js native TS', () => {})
return
}
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
packageJson: {
type: 'module',
},
})
if (skipped) {
return
}
it('should throw with type error on build (ESM)', async () => {
if (isNextDev) {
await next.start()
const $ = await next.render$('/')
expect($('p').text()).toBe('foo')
} else {
const { cliOutput } = await next.build()
await expect(cliOutput).toContain(
`Type 'string' is not assignable to type 'number'.`
)
}
})
})
import type { NextConfig } from 'next'
// top-level await will only work in Native TS mode.
// This is to ensure that the test is running in Native TS mode.
await Promise.resolve()
// type error
let foo: number = 'foo'
const nextConfig: NextConfig = {
env: {
foo,
},
}
export default nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment