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
b141f3b2
Unverified
Commit
b141f3b2
authored
Dec 08, 2023
by
Shu Ding
Committed by
GitHub
Dec 08, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix cases for the `optimize_server_react` transform (#59390)
Closes #59310, see attached test cases. Closes NEXT-1830
parent
d5291fad
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
147 additions
and
61 deletions
+147
-61
optimize_server_react.rs
packages/next-swc/crates/core/src/optimize_server_react.rs
+46
-55
output.js
...ates/core/tests/fixture/optimize_server_react/2/output.js
+8
-2
output.js
...ates/core/tests/fixture/optimize_server_react/3/output.js
+10
-4
input.js
...rates/core/tests/fixture/optimize_server_react/6/input.js
+12
-0
output.js
...ates/core/tests/fixture/optimize_server_react/6/output.js
+14
-0
input.js
...rates/core/tests/fixture/optimize_server_react/7/input.js
+31
-0
output.js
...ates/core/tests/fixture/optimize_server_react/7/output.js
+26
-0
No files found.
packages/next-swc/crates/core/src/optimize_server_react.rs
View file @
b141f3b2
...
@@ -129,76 +129,67 @@ impl Fold for OptimizeServerReact {
...
@@ -129,76 +129,67 @@ impl Fold for OptimizeServerReact {
}
}
}
}
expr
expr
.fold_children_with
(
self
)
}
}
// const [state, setState] = useState(x);
// const [state, setState] = useState(x);
// const [state, setState] = React.useState(x);
// const [state, setState] = React.useState(x);
fn
fold_var_declarator
s
(
&
mut
self
,
d
:
Vec
<
VarDeclarator
>
)
->
Vec
<
VarDeclarator
>
{
fn
fold_var_declarator
(
&
mut
self
,
decl
:
VarDeclarator
)
->
VarDeclarator
{
if
!
self
.optimize_use_state
{
if
!
self
.optimize_use_state
{
return
d
;
return
d
ecl
;
}
}
let
mut
new_d
=
vec!
[];
if
let
Pat
::
Array
(
array_pat
)
=
&
decl
.name
{
for
decl
in
d
{
if
array_pat
.elems
.len
()
==
2
{
if
let
Pat
::
Array
(
array_pat
)
=
&
decl
.name
{
if
let
Some
(
box
Expr
::
Call
(
call
))
=
&
decl
.init
{
if
array_pat
.elems
.len
()
==
2
{
if
let
Callee
::
Expr
(
box
Expr
::
Ident
(
f
))
=
&
call
.callee
{
if
let
(
Some
(
array_pat_1
),
Some
(
array_pat_2
))
=
if
let
Some
(
use_state_ident
)
=
&
self
.use_state_ident
{
(
&
array_pat
.elems
[
0
],
&
array_pat
.elems
[
1
])
if
&
f
.to_id
()
==
use_state_ident
&&
call
.args
.len
()
==
1
{
{
// We do the optimization only if the arg is a literal or a
if
let
Some
(
box
Expr
::
Call
(
call
))
=
&
decl
.init
{
// type that we can
if
let
Callee
::
Expr
(
box
Expr
::
Ident
(
f
))
=
&
call
.callee
{
// be sure is not a function (e.g. {} or [] lit).
if
let
Some
(
use_state_ident
)
=
&
self
.use_state_ident
{
// This is because useState allows a function as the
if
&
f
.to_id
()
==
use_state_ident
&&
call
.args
.len
()
==
1
{
// initialiser.
// We do the optimization only if the arg is a literal or a
match
&
call
.args
[
0
]
.expr
{
// type that we can
box
Expr
::
Lit
(
_
)
|
box
Expr
::
Object
(
_
)
|
box
Expr
::
Array
(
_
)
=>
{
// be sure is not a function (e.g. {} or [] lit).
// const [state, setState] = [x, () => {}];
// This is because useState allows a function as the
return
VarDeclarator
{
// initialiser.
definite
:
false
,
match
&
call
.args
[
0
]
.expr
{
name
:
decl
.name
.clone
(),
box
Expr
::
Lit
(
_
)
init
:
Some
(
Box
::
new
(
Expr
::
Array
(
ArrayLit
{
|
box
Expr
::
Object
(
_
)
elems
:
vec!
[
|
box
Expr
::
Array
(
_
)
=>
{
Some
(
call
.args
[
0
]
.expr
.clone
()
.into
()),
// const state = x, setState = () => {};
Some
(
new_d
.push
(
VarDeclarator
{
Expr
::
Arrow
(
ArrowExpr
{
definite
:
false
,
span
:
DUMMY_SP
,
name
:
array_pat_1
.clone
(),
body
:
Box
::
new
(
BlockStmtOrExpr
::
Expr
(
init
:
Some
(
call
.args
[
0
]
.expr
.clone
()),
Box
::
new
(
Expr
::
Lit
(
Lit
::
Null
(
span
:
DUMMY_SP
,
Null
{
span
:
DUMMY_SP
},
});
))),
new_d
.push
(
VarDeclarator
{
)),
definite
:
false
,
is_async
:
false
,
name
:
array_pat_2
.clone
(),
is_generator
:
false
,
init
:
Some
(
Box
::
new
(
Expr
::
Arrow
(
ArrowExpr
{
params
:
vec!
[],
body
:
Box
::
new
(
BlockStmtOrExpr
::
Expr
(
return_type
:
None
,
Box
::
new
(
Expr
::
Lit
(
Lit
::
Null
(
Null
{
type_params
:
None
,
span
:
DUMMY_SP
,
})
}))),
.into
(),
)),
),
params
:
vec!
[],
],
is_async
:
false
,
span
:
DUMMY_SP
,
is_generator
:
false
,
}))),
span
:
DUMMY_SP
,
span
:
DUMMY_SP
,
type_params
:
None
,
};
return_type
:
None
,
}))),
span
:
DUMMY_SP
,
});
continue
;
}
_
=>
{}
}
}
}
_
=>
{}
}
}
}
}
}
}
}
}
}
}
}
}
new_d
.push
(
decl
.fold_with
(
self
));
}
}
new_d
decl
.fold_children_with
(
self
)
}
}
}
}
packages/next-swc/crates/core/tests/fixture/optimize_server_react/2/output.js
View file @
b141f3b2
...
@@ -7,8 +7,14 @@ import { FilterItem } from './item';
...
@@ -7,8 +7,14 @@ import { FilterItem } from './item';
export
default
function
FilterItemDropdown
({
list
})
{
export
default
function
FilterItemDropdown
({
list
})
{
const
pathname
=
usePathname
();
const
pathname
=
usePathname
();
const
searchParams
=
useSearchParams
();
const
searchParams
=
useSearchParams
();
const
active
=
''
,
setActive
=
()
=>
null
;
const
[
active
,
setActive
]
=
[
const
openSelect
=
false
,
setOpenSelect
=
()
=>
null
;
''
,
()
=>
null
];
const
[
openSelect
,
setOpenSelect
]
=
[
false
,
()
=>
null
];
const
ref
=
useRef
(
null
);
const
ref
=
useRef
(
null
);
null
;
null
;
null
;
null
;
...
...
packages/next-swc/crates/core/tests/fixture/optimize_server_react/3/output.js
View file @
b141f3b2
import
{
useState
}
from
'react'
;
import
{
useState
}
from
'react'
;
export
default
function
App
({
x
})
{
export
default
function
App
({
x
})
{
const
state
=
0
,
setState
=
()
=>
null
;
const
[
state
,
setState
]
=
[
0
,
()
=>
null
];
const
[
state2
,
setState2
]
=
useState
(()
=>
0
);
const
[
state2
,
setState2
]
=
useState
(()
=>
0
);
const
[
state3
,
setState3
]
=
useState
(
x
);
const
[
state3
,
setState3
]
=
useState
(
x
);
const
s
=
useState
(
0
);
const
s
=
useState
(
0
);
const
[
state4
]
=
useState
(
0
);
const
[
state4
]
=
useState
(
0
);
const
{
a
}
=
{
const
[{
a
},
setState5
]
=
[
a
:
0
{
},
setState5
=
()
=>
null
;
a
:
0
},
()
=>
null
];
return
<
div
>
return
<
div
>
<
h1
>
Hello
World
<
/h1
>
<
h1
>
Hello
World
<
/h1
>
...
...
packages/next-swc/crates/core/tests/fixture/optimize_server_react/6/input.js
0 → 100644
View file @
b141f3b2
import
{
useState
,
useEffect
,
useLayoutEffect
}
from
'react'
import
React
from
'react'
const
Component
=
({
children
,
fallback
})
=>
{
const
[
mounted
,
setMounted
]
=
useState
(
false
)
useEffect
(()
=>
setMounted
(
true
),
[])
if
(
!
mounted
)
{
return
fallback
??
/* @__PURE__ */
jsx
(
Fragment
,
{})
}
return
children
}
export
{
Component
}
packages/next-swc/crates/core/tests/fixture/optimize_server_react/6/output.js
0 → 100644
View file @
b141f3b2
import
{
useState
,
useEffect
,
useLayoutEffect
}
from
'react'
;
import
React
from
'react'
;
const
Component
=
({
children
,
fallback
})
=>
{
const
[
mounted
,
setMounted
]
=
[
false
,
()
=>
null
];
null
;
if
(
!
mounted
)
{
return
fallback
??
/* @__PURE__ */
jsx
(
Fragment
,
{});
}
return
children
;
};
export
{
Component
};
packages/next-swc/crates/core/tests/fixture/optimize_server_react/7/input.js
0 → 100644
View file @
b141f3b2
import
{
useEffect
,
useLayoutEffect
,
useMemo
}
from
'react'
import
*
as
React
from
'react'
export
default
function
App
()
{
useEffect
(()
=>
{
console
.
log
(
'Hello World'
)
},
[])
useLayoutEffect
(()
=>
{
function
foo
()
{}
return
()
=>
{}
},
[
1
,
2
,
App
])
useLayoutEffect
(()
=>
{},
[
runSideEffect
()])
useEffect
(()
=>
{},
[
1
,
runSideEffect
(),
2
])
useEffect
(()
=>
{},
getArray
())
const
a
=
useMemo
(()
=>
{
return
1
},
[])
React
.
useEffect
(()
=>
{
console
.
log
(
'Hello World'
)
})
return
(
<
div
>
<
h1
>
Hello
World
<
/h1
>
<
/div
>
)
}
packages/next-swc/crates/core/tests/fixture/optimize_server_react/7/output.js
0 → 100644
View file @
b141f3b2
import
{
useEffect
,
useLayoutEffect
,
useMemo
}
from
'react'
;
import
*
as
React
from
'react'
;
export
default
function
App
()
{
null
;
null
;
useLayoutEffect
(()
=>
{},
[
runSideEffect
()
]);
useEffect
(()
=>
{},
[
1
,
runSideEffect
(),
2
]);
useEffect
(()
=>
{},
getArray
());
const
a
=
useMemo
(()
=>
{
return
1
;
},
[]);
React
.
useEffect
(()
=>
{
console
.
log
(
'Hello World'
);
});
return
<
div
>
<
h1
>
Hello
World
<
/h1
>
<
/div>
;
}
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