Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
2a73ab0f
Commit
2a73ab0f
authored
Aug 29, 2025
by
devjiwonchoi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
resolve swc option
- no need with assertion handling - strict pass module type, no isModule unknown
parent
d76502aa
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
42 deletions
+26
-42
loader.ts
packages/next/src/build/next-config-ts/loader.ts
+7
-29
transpile-config.ts
packages/next/src/build/next-config-ts/transpile-config.ts
+19
-13
No files found.
packages/next/src/build/next-config-ts/loader.ts
View file @
2a73ab0f
...
...
@@ -4,13 +4,13 @@ import type {
ModuleFormat
,
ResolveHook
,
}
from
'module'
import
type
{
Options
as
SWCOptions
}
from
'@swc/core'
import
type
{
CompilerOptions
}
from
'typescript'
import
path
from
'path'
import
{
existsSync
}
from
'fs'
import
{
readFile
}
from
'fs/promises'
import
{
fileURLToPath
}
from
'url'
import
{
resolveSWCOptions
}
from
'./transpile-config.js'
const
tsExts
=
new
Set
([
'.ts'
,
'.mts'
,
'.cts'
])
const
localContext
=
new
Map
<
string
,
any
>
()
...
...
@@ -127,34 +127,12 @@ export const load: LoadHook = async (url, context, nextLoad) => {
const
rawSource
=
await
readFile
(
fileURLToPath
(
url
),
'utf-8'
)
// Lazy load swc to reduce the initial loader registration time.
const
{
transform
}
=
await
import
(
'../swc/index.js'
)
const
{
code
}
=
await
transform
(
rawSource
,
{
jsc
:
{
parser
:
{
syntax
:
'typescript'
,
},
...(
compilerOptions
.
paths
?
{
paths
:
compilerOptions
.
paths
}
:
{}),
...(
compilerOptions
.
baseUrl
?
// Needs to be an absolute path.
{
baseUrl
:
path
.
resolve
(
cwd
,
compilerOptions
.
baseUrl
)
}
:
compilerOptions
.
paths
?
// If paths is given, baseUrl is required.
{
baseUrl
:
cwd
}
:
{}),
experimental
:
{
keepImportAttributes
:
true
,
// Without this option, `assert` assertion also transpiles to `with` attribute,
// which will throw if in Node.js version that does not support `with`.
// Switch from Import Assertions to Import Attributes held at v21.0.0, v20.10.0, v18.20.0.
emitAssertForImportAttributes
:
true
,
},
},
env
:
{
targets
:
{
// Setting the Node.js version can reduce unnecessary code generation.
node
:
process
?.
versions
?.
node
??
'20.19.0'
,
},
},
}
satisfies
SWCOptions
)
const
swcOptions
=
resolveSWCOptions
({
cwd
,
compilerOptions
,
type
:
'es6'
,
})
const
{
code
}
=
await
transform
(
rawSource
,
swcOptions
)
return
{
format
:
'module'
,
...
...
packages/next/src/build/next-config-ts/transpile-config.ts
View file @
2a73ab0f
import
type
{
Options
as
SWCOptions
}
from
'@swc/core'
import
type
{
CompilerOptions
}
from
'typescript'
import
path
from
'path'
import
{
join
,
resolve
}
from
'path'
import
{
readFile
}
from
'fs/promises'
import
{
register
}
from
'module'
import
{
pathToFileURL
}
from
'url'
...
...
@@ -10,10 +10,15 @@ import { deregisterHook, registerHook, requireFromString } from './require-hook'
import
{
warn
}
from
'../output/log'
import
{
installDependencies
}
from
'../../lib/install-dependencies'
function
resolveSWCOptions
(
cwd
:
string
,
export
function
resolveSWCOptions
({
cwd
,
compilerOptions
,
type
,
}:
{
cwd
:
string
compilerOptions
:
CompilerOptions
):
SWCOptions
{
type
:
'commonjs'
|
'es6'
}):
SWCOptions
{
return
{
jsc
:
{
parser
:
{
...
...
@@ -22,16 +27,15 @@ function resolveSWCOptions(
...(
compilerOptions
.
paths
?
{
paths
:
compilerOptions
.
paths
}
:
{}),
...(
compilerOptions
.
baseUrl
?
// Needs to be an absolute path.
{
baseUrl
:
path
.
resolve
(
cwd
,
compilerOptions
.
baseUrl
)
}
{
baseUrl
:
resolve
(
cwd
,
compilerOptions
.
baseUrl
)
}
:
compilerOptions
.
paths
?
// If paths is given, baseUrl is required.
{
baseUrl
:
cwd
}
:
{}),
},
module
:
{
type
:
'commonjs'
,
type
,
},
isModule
:
'unknown'
,
env
:
{
targets
:
{
// Setting the Node.js version can reduce unnecessary code generation.
...
...
@@ -124,9 +128,7 @@ export async function transpileConfig({
let
pkgJson
:
Record
<
string
,
string
>
=
{}
try
{
pkgJson
=
JSON
.
parse
(
await
readFile
(
path
.
join
(
cwd
,
'package.json'
),
'utf8'
)
)
pkgJson
=
JSON
.
parse
(
await
readFile
(
join
(
cwd
,
'package.json'
),
'utf8'
))
}
catch
{}
if
(
configFileName
.
endsWith
(
'.mts'
)
||
pkgJson
.
type
===
'module'
)
{
...
...
@@ -150,7 +152,11 @@ async function handleCJS({
nextConfigPath
:
string
compilerOptions
:
CompilerOptions
})
{
const
swcOptions
=
resolveSWCOptions
(
cwd
,
compilerOptions
)
const
swcOptions
=
resolveSWCOptions
({
cwd
,
compilerOptions
,
type
:
'commonjs'
,
})
let
hasRequire
=
false
try
{
const
nextConfigString
=
await
readFile
(
nextConfigPath
,
'utf8'
)
...
...
@@ -166,7 +172,7 @@ async function handleCJS({
}
// filename & extension don't matter here
return
requireFromString
(
code
,
path
.
resolve
(
cwd
,
'next.config.compiled.js'
))
return
requireFromString
(
code
,
resolve
(
cwd
,
'next.config.compiled.js'
))
}
catch
(
error
)
{
throw
error
}
finally
{
...
...
@@ -186,7 +192,7 @@ async function handleESM(workerData: {
})
{
try
{
if
(
!
hasRegistered
)
{
register
(
pathToFileURL
(
path
.
join
(
__dirname
,
'loader.js'
)).
href
,
{
register
(
pathToFileURL
(
join
(
__dirname
,
'loader.js'
)).
href
,
{
parentURL
:
pathToFileURL
(
workerData
.
cwd
).
href
,
data
:
{
cwd
:
workerData
.
cwd
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment