-
Notifications
You must be signed in to change notification settings - Fork 28.3k
feat(next): next.config.ts
#57656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(next): next.config.ts
#57656
Conversation
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
Notifying the following users due to files changed in this PR based on this repo's notify modifiers: @timneutkens, @ijjk, @shuding, @huozhi:
|
|
I'll remove the caching part. Thank you! |
ref: #57656 (comment) Co-authored-by: Donny/강동윤 <[email protected]>
next.config.ts
I opened a PR devjiwonchoi#1 to make create-next-app create |
@@ -319,6 +319,7 @@ When you need to declare custom types, you might be tempted to modify `next-env. | |||
|
|||
| Version | Changes | | |||
| --------- | ------------------------------------------------------------------------------------------------------------------------------------ | | |||
| `v14.2.0` | `next.config.ts` support added for TypeScript projects. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily set version as v14.2.0
This reverts commit 8e4c38e.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ijjk These were the final attempts I tried before moving back to webpack.
const swc = transformSync(code, swcOptions) | ||
return originalCompile.call(this, swc.code, filename) | ||
if (isESM) { | ||
// TODO: Implement importing ESM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to modify require.extensions['.js']
behavior as:
if (isESM) {
delete (require as any).extensions['.js']
transformableExtensions.push('.js')
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Received error:
> Build error occurred
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/node_modules/esm/foo.js from /tmp/next.config.cjs not supported.
Instead change the require of foo.js in /tmp/next.config.cjs to a dynamic import() which is available in all CommonJS modules.
at require.extensions.<computed> [as .js]
@@ -101,7 +106,6 @@ export async function transpileConfig({ | |||
throw error | |||
} finally { | |||
await unlink(tempConfigPath).catch(() => {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert the .js
hook.
transformableExtensions.forEach((ext) => delete require.extensions[ext])
require.extensions['.js'] = originalJsHandler
await unlink(tempConfigPath).catch(() => {})
Why this was closed? |
I accidentally deleted the head, finding a way to restore it 😅 |
Phew, I was really looking forward to this feature, and it would be a shame if this was closed intentionally. Please keep up the good work, a large number of users will benefit from this feature! |
@joulev Thank you for your support! I'll do my best to ship it as efficiently as possible. 😄 |
Ported to #63051
What?
This PR initiated support of
next.config.ts
usingSWC
andwebpack
.next.config.ts
.create-next-app
for TS to createnext.config.ts
by default.Why?
This PR can resolve the two main needs:
type
not JSDoc.As mentioned in #35969 (comment), it became possible to transpile the config fast enough to have less pain on DX.
The current method impacts the transpilation time less than a
0.1s
increase in HMR.x-ref: #5318 #35969 #44632 #50121 #50126
How?
Used
webpack
as mentioned in #50126 (review), only on cases where the config imports other modules.SWC
if the config has no valid imports(types or unused), which is the case for most users.webpack
to resolve imports with various conditions and extensions.next.config.mts
can handle it.Restrictions
.next
directory, regardless of thedistDir
option in the config.__filename
will returnnext.config.ts
instead of the default behavior returning the absolute file path.Expected & Edge Cases
{ x as default }
node_modules
next.config.js
without changeNative ESM (package.json
type: module
).cts
.cjs
.js
(ESM)CJS
next.config.mts
).mts
.mjs
.js
(CJS)Transpilation Speed Profile (format:
second
)Open Stats
Test Settings
Transpile-only (no import)
Bundle-webpack (has import)
next dev
Transpile-only (no import)
Bundle-webpack (has import)
next build
Transpile-only (no import)
Bundle-webpack (has import)
next start
Transpile-only (no import)
Bundle-webpack (has import)
Spec
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030 Available memory (MB): 18432 Available CPU cores: 11 Binaries: Node: 18.17.1 npm: 9.6.7 Yarn: N/A pnpm: 8.15.1 Relevant Packages: next: 14.1.2-canary.2 // Latest available version is detected (14.1.2-canary.2). eslint-config-next: 14.1.2-canary.2 react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A