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::{
...
@@ -15,7 +15,7 @@ use swc_core::{
PropName
,
PropOrSpread
,
Stmt
,
Str
,
Tpl
,
UnaryExpr
,
UnaryOp
,
PropName
,
PropOrSpread
,
Stmt
,
Str
,
Tpl
,
UnaryExpr
,
UnaryOp
,
},
},
utils
::{
private_ident
,
quote_ident
,
ExprFactory
},
utils
::{
private_ident
,
quote_ident
,
ExprFactory
},
visit
::{
fold_pass
,
Fold
,
FoldWith
,
VisitMut
,
VisitMutWith
},
visit
::{
visit_mut_pass
,
VisitMut
,
VisitMutWith
},
},
},
quote
,
quote
,
};
};
...
@@ -34,7 +34,7 @@ pub fn next_dynamic(
...
@@ -34,7 +34,7 @@ pub fn next_dynamic(
filename
:
Arc
<
FileName
>
,
filename
:
Arc
<
FileName
>
,
pages_or_app_dir
:
Option
<
PathBuf
>
,
pages_or_app_dir
:
Option
<
PathBuf
>
,
)
->
impl
Pass
{
)
->
impl
Pass
{
fold
_pass
(
NextDynamicPatcher
{
visit_mut
_pass
(
NextDynamicPatcher
{
is_development
,
is_development
,
is_server_compiler
,
is_server_compiler
,
is_react_server_layer
,
is_react_server_layer
,
...
@@ -117,33 +117,24 @@ enum TurbopackImport {
...
@@ -117,33 +117,24 @@ enum TurbopackImport {
},
},
}
}
impl
Fold
for
NextDynamicPatcher
{
impl
VisitMut
for
NextDynamicPatcher
{
fn
fold_module_items
(
&
mut
self
,
mut
items
:
Vec
<
ModuleItem
>
)
->
Vec
<
ModuleItem
>
{
fn
visit_mut_module_items
(
&
mut
self
,
items
:
&
mut
Vec
<
ModuleItem
>
)
{
items
=
items
.fold
_children_with
(
self
);
items
.visit_mut
_children_with
(
self
);
self
.maybe_add_dynamically_imported_specifier
(
&
mut
items
);
self
.maybe_add_dynamically_imported_specifier
(
items
);
items
}
}
fn
fold_import_decl
(
&
mut
self
,
decl
:
ImportDecl
)
->
ImportDecl
{
fn
visit_mut_import_decl
(
&
mut
self
,
decl
:
&
mut
ImportDecl
)
{
let
ImportDecl
{
if
&
decl
.src.value
==
"next/dynamic"
{
ref
src
,
for
specifier
in
&
decl
.specifiers
{
ref
specifiers
,
..
}
=
decl
;
if
&
src
.value
==
"next/dynamic"
{
for
specifier
in
specifiers
{
if
let
ImportSpecifier
::
Default
(
default_specifier
)
=
specifier
{
if
let
ImportSpecifier
::
Default
(
default_specifier
)
=
specifier
{
self
.dynamic_bindings
.push
(
default_specifier
.local
.to_id
());
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
self
.is_next_dynamic_first_arg
{
if
let
Callee
::
Import
(
..
)
=
&
expr
.callee
{
if
let
Callee
::
Import
(
..
)
=
&
expr
.callee
{
match
&*
expr
.args
[
0
]
.expr
{
match
&*
expr
.args
[
0
]
.expr
{
...
@@ -157,9 +148,12 @@ impl Fold for NextDynamicPatcher {
...
@@ -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
Callee
::
Expr
(
i
)
=
&
expr
.callee
{
if
let
Expr
::
Ident
(
identifier
)
=
&**
i
{
if
let
Expr
::
Ident
(
identifier
)
=
&**
i
{
if
self
.dynamic_bindings
.contains
(
&
identifier
.to_id
())
{
if
self
.dynamic_bindings
.contains
(
&
identifier
.to_id
())
{
...
@@ -172,7 +166,7 @@ impl Fold for NextDynamicPatcher {
...
@@ -172,7 +166,7 @@ impl Fold for NextDynamicPatcher {
)
)
.emit
()
.emit
()
});
});
return
expr
;
return
;
}
else
if
expr
.args
.len
()
>
2
{
}
else
if
expr
.args
.len
()
>
2
{
HANDLER
.with
(|
handler
|
{
HANDLER
.with
(|
handler
|
{
handler
handler
...
@@ -182,7 +176,7 @@ impl Fold for NextDynamicPatcher {
...
@@ -182,7 +176,7 @@ impl Fold for NextDynamicPatcher {
)
)
.emit
()
.emit
()
});
});
return
expr
;
return
;
}
}
if
expr
.args
.len
()
==
2
{
if
expr
.args
.len
()
==
2
{
match
&*
expr
.args
[
1
]
.expr
{
match
&*
expr
.args
[
1
]
.expr
{
...
@@ -196,19 +190,19 @@ impl Fold for NextDynamicPatcher {
...
@@ -196,19 +190,19 @@ impl Fold for NextDynamicPatcher {
)
)
.emit
();
.emit
();
});
});
return
expr
;
return
;
}
}
}
}
}
}
self
.is_next_dynamic_first_arg
=
true
;
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
;
self
.is_next_dynamic_first_arg
=
false
;
let
Some
((
dynamically_imported_specifier
,
dynamically_imported_specifier_span
))
=
let
Some
((
dynamically_imported_specifier
,
dynamically_imported_specifier_span
))
=
self
.dynamically_imported_specifier
.take
()
self
.dynamically_imported_specifier
.take
()
else
{
else
{
return
expr
;
return
;
};
};
let
project_dir
=
match
self
.pages_or_app_dir
.as_deref
()
{
let
project_dir
=
match
self
.pages_or_app_dir
.as_deref
()
{
...
@@ -419,7 +413,6 @@ impl Fold for NextDynamicPatcher {
...
@@ -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::{
...
@@ -5,7 +5,7 @@ use swc_core::{
common
::
DUMMY_SP
,
common
::
DUMMY_SP
,
ecma
::{
ecma
::{
ast
::
*
,
ast
::
*
,
visit
::{
fold_pass
,
Fold
},
visit
::{
visit_mut_pass
,
VisitMut
},
},
},
};
};
...
@@ -14,8 +14,8 @@ pub struct Config {
...
@@ -14,8 +14,8 @@ pub struct Config {
pub
packages
:
Vec
<
String
>
,
pub
packages
:
Vec
<
String
>
,
}
}
pub
fn
named_import_transform
(
config
:
Config
)
->
impl
Pass
{
pub
fn
named_import_transform
(
config
:
Config
)
->
impl
Pass
+
VisitMut
{
fold
_pass
(
NamedImportTransform
{
visit_mut
_pass
(
NamedImportTransform
{
packages
:
config
.packages
,
packages
:
config
.packages
,
})
})
}
}
...
@@ -25,9 +25,8 @@ struct NamedImportTransform {
...
@@ -25,9 +25,8 @@ struct NamedImportTransform {
packages
:
Vec
<
String
>
,
packages
:
Vec
<
String
>
,
}
}
/// TODO: Implement this as a [Pass] instead of a full visitor ([Fold])
impl
VisitMut
for
NamedImportTransform
{
impl
Fold
for
NamedImportTransform
{
fn
visit_mut_import_decl
(
&
mut
self
,
decl
:
&
mut
ImportDecl
)
{
fn
fold_import_decl
(
&
mut
self
,
decl
:
ImportDecl
)
->
ImportDecl
{
// Match named imports and check if it's included in the packages
// Match named imports and check if it's included in the packages
let
src_value
=
&
decl
.src.value
;
let
src_value
=
&
decl
.src.value
;
...
@@ -75,18 +74,12 @@ impl Fold for NamedImportTransform {
...
@@ -75,18 +74,12 @@ impl Fold for NamedImportTransform {
src_value
.to_string_lossy
()
src_value
.to_string_lossy
()
);
);
// Create a new import declaration, keep everything the same except the source
*
decl
.src
=
Str
{
let
mut
new_decl
=
decl
.clone
();
*
new_decl
.src
=
Str
{
span
:
DUMMY_SP
,
span
:
DUMMY_SP
,
value
:
new_src
.into
(),
value
:
new_src
.into
(),
raw
:
None
,
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