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
d93f4c80
Unverified
Commit
d93f4c80
authored
Nov 21, 2025
by
Niklas Mischkulnig
Committed by
GitHub
Nov 21, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate to VisitMut (#86347)
This is generally better for performance
parent
40dd4ce4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
192 deletions
+121
-192
dynamic.rs
crates/next-custom-transforms/src/transforms/dynamic.rs
+20
-27
named_import_transform.rs
...ustom-transforms/src/transforms/named_import_transform.rs
+6
-13
next_ssg.rs
crates/next-custom-transforms/src/transforms/next_ssg.rs
+95
-152
No files found.
crates/next-custom-transforms/src/transforms/dynamic.rs
View file @
d93f4c80
...
...
@@ -15,7 +15,7 @@ use swc_core::{
PropName
,
PropOrSpread
,
Stmt
,
Str
,
Tpl
,
UnaryExpr
,
UnaryOp
,
},
utils
::{
private_ident
,
quote_ident
,
ExprFactory
},
visit
::{
fold_pass
,
Fold
,
FoldWith
,
VisitMut
,
VisitMutWith
},
visit
::{
visit_mut_pass
,
VisitMut
,
VisitMutWith
},
},
quote
,
};
...
...
@@ -34,7 +34,7 @@ pub fn next_dynamic(
filename
:
Arc
<
FileName
>
,
pages_or_app_dir
:
Option
<
PathBuf
>
,
)
->
impl
Pass
{
fold
_pass
(
NextDynamicPatcher
{
visit_mut
_pass
(
NextDynamicPatcher
{
is_development
,
is_server_compiler
,
is_react_server_layer
,
...
...
@@ -117,33 +117,24 @@ enum TurbopackImport {
},
}
impl
Fold
for
NextDynamicPatcher
{
fn
fold_module_items
(
&
mut
self
,
mut
items
:
Vec
<
ModuleItem
>
)
->
Vec
<
ModuleItem
>
{
items
=
items
.fold
_children_with
(
self
);
impl
VisitMut
for
NextDynamicPatcher
{
fn
visit_mut_module_items
(
&
mut
self
,
items
:
&
mut
Vec
<
ModuleItem
>
)
{
items
.visit_mut
_children_with
(
self
);
self
.maybe_add_dynamically_imported_specifier
(
&
mut
items
);
items
self
.maybe_add_dynamically_imported_specifier
(
items
);
}
fn
fold_import_decl
(
&
mut
self
,
decl
:
ImportDecl
)
->
ImportDecl
{
let
ImportDecl
{
ref
src
,
ref
specifiers
,
..
}
=
decl
;
if
&
src
.value
==
"next/dynamic"
{
for
specifier
in
specifiers
{
fn
visit_mut_import_decl
(
&
mut
self
,
decl
:
&
mut
ImportDecl
)
{
if
&
decl
.src.value
==
"next/dynamic"
{
for
specifier
in
&
decl
.specifiers
{
if
let
ImportSpecifier
::
Default
(
default_specifier
)
=
specifier
{
self
.dynamic_bindings
.push
(
default_specifier
.local
.to_id
());
}
}
}
decl
}
fn
fold_call_expr
(
&
mut
self
,
expr
:
CallExpr
)
->
CallExpr
{
fn
visit_mut_call_expr
(
&
mut
self
,
expr
:
&
mut
CallExpr
)
{
if
self
.is_next_dynamic_first_arg
{
if
let
Callee
::
Import
(
..
)
=
&
expr
.callee
{
match
&*
expr
.args
[
0
]
.expr
{
...
...
@@ -157,9 +148,12 @@ impl Fold for NextDynamicPatcher {
_
=>
{}
}
}
return
expr
.fold_children_with
(
self
);
expr
.visit_mut_children_with
(
self
);
return
;
}
let
mut
expr
=
expr
.fold_children_with
(
self
);
expr
.visit_mut_children_with
(
self
);
if
let
Callee
::
Expr
(
i
)
=
&
expr
.callee
{
if
let
Expr
::
Ident
(
identifier
)
=
&**
i
{
if
self
.dynamic_bindings
.contains
(
&
identifier
.to_id
())
{
...
...
@@ -172,7 +166,7 @@ impl Fold for NextDynamicPatcher {
)
.emit
()
});
return
expr
;
return
;
}
else
if
expr
.args
.len
()
>
2
{
HANDLER
.with
(|
handler
|
{
handler
...
...
@@ -182,7 +176,7 @@ impl Fold for NextDynamicPatcher {
)
.emit
()
});
return
expr
;
return
;
}
if
expr
.args
.len
()
==
2
{
match
&*
expr
.args
[
1
]
.expr
{
...
...
@@ -196,19 +190,19 @@ impl Fold for NextDynamicPatcher {
)
.emit
();
});
return
expr
;
return
;
}
}
}
self
.is_next_dynamic_first_arg
=
true
;
expr
.args
[
0
]
.expr
=
expr
.args
[
0
]
.expr
.clone
()
.fold
_with
(
self
);
expr
.args
[
0
]
.expr
.visit_mut
_with
(
self
);
self
.is_next_dynamic_first_arg
=
false
;
let
Some
((
dynamically_imported_specifier
,
dynamically_imported_specifier_span
))
=
self
.dynamically_imported_specifier
.take
()
else
{
return
expr
;
return
;
};
let
project_dir
=
match
self
.pages_or_app_dir
.as_deref
()
{
...
...
@@ -419,7 +413,6 @@ impl Fold for NextDynamicPatcher {
}
}
}
expr
}
}
...
...
crates/next-custom-transforms/src/transforms/named_import_transform.rs
View file @
d93f4c80
...
...
@@ -5,7 +5,7 @@ use swc_core::{
common
::
DUMMY_SP
,
ecma
::{
ast
::
*
,
visit
::{
fold_pass
,
Fold
},
visit
::{
visit_mut_pass
,
VisitMut
},
},
};
...
...
@@ -14,8 +14,8 @@ pub struct Config {
pub
packages
:
Vec
<
String
>
,
}
pub
fn
named_import_transform
(
config
:
Config
)
->
impl
Pass
{
fold
_pass
(
NamedImportTransform
{
pub
fn
named_import_transform
(
config
:
Config
)
->
impl
Pass
+
VisitMut
{
visit_mut
_pass
(
NamedImportTransform
{
packages
:
config
.packages
,
})
}
...
...
@@ -25,9 +25,8 @@ struct NamedImportTransform {
packages
:
Vec
<
String
>
,
}
/// TODO: Implement this as a [Pass] instead of a full visitor ([Fold])
impl
Fold
for
NamedImportTransform
{
fn
fold_import_decl
(
&
mut
self
,
decl
:
ImportDecl
)
->
ImportDecl
{
impl
VisitMut
for
NamedImportTransform
{
fn
visit_mut_import_decl
(
&
mut
self
,
decl
:
&
mut
ImportDecl
)
{
// Match named imports and check if it's included in the packages
let
src_value
=
&
decl
.src.value
;
...
...
@@ -75,18 +74,12 @@ impl Fold for NamedImportTransform {
src_value
.to_string_lossy
()
);
// Create a new import declaration, keep everything the same except the source
let
mut
new_decl
=
decl
.clone
();
*
new_decl
.src
=
Str
{
*
decl
.src
=
Str
{
span
:
DUMMY_SP
,
value
:
new_src
.into
(),
raw
:
None
,
};
return
new_decl
;
}
}
decl
}
}
crates/next-custom-transforms/src/transforms/next_ssg.rs
View file @
d93f4c80
This diff is collapsed.
Click to expand it.
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