feat(next): `next.config.ts` (#63051)
> Note: port of #57656
## What?
This PR initiated support of `next.config.ts`.
Follow ups:
- update docs
([x-ref](https://github.com/vercel/next.js/pull/63051/commits/2ea9b6e2c77b71ae9406032a33e1c20c6053e503))
- update cna ts templates
([x-ref](https://github.com/vercel/next.js/pull/63051/commits/80e674036edfc0307331d3f604b1399043bb65e6))
## Why?
> Next.js provides a TypeScript-first development experience for
building your React application.
This PR can resolve the two main needs:
1. Import TS files to the config.
2. Conduct a type check directly by importing `type` instead of JSDoc.
x-ref: #5318 #35969 #44632 #50121 #50126
## How?
### Goals
- [x] [do not
bundle](https://github.com/vercel/next.js/pull/57656#discussion_r1499983515)
- [x] [minimum fs
operations](https://github.com/vercel/next.js/pull/63051#discussion_r1550609199)
- [x] zero restrictions
Used `require.extensions` hooks to transpile the imported files on the
fly.
To reduce I/O operation on disk, used custom `module.exports` to require
directly from a transpiled code string.
## Expected
> Added tests for the cases below:
- [x] export default
- [x] export config `as default`
- [x] [config as an async
function](https://nextjs.org/docs/app/api-reference/next-config-js)
- [x] use Node.js modules (fs, path, etc.)
- [x] HMR on config change
- [x] throw when type error
- [x] turbopack
### Importing Files
- [x] nested imports (config -> A -> B)
- [x] import alias (tsconfig baseUrl and paths)
- [x] import from `node_modules`
- [x] import `JSON`
#### Extensions
- [x] `.ts`
- [x] `.cts`
- [x] `.mts`
- [x] `.cjs`
- [x] `.mjs`
- [x] `.js` (both ESM and CJS w/o extra change)
---------
Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
Co-authored-by: Sukka <isukkaw@gmail.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Sam Ko <sam@vercel.com>
Co-authored-by: Ahmed Abdelbaset <A7med3bdulBaset@gmail.com>
Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>