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
74d7e358
Unverified
Commit
74d7e358
authored
May 20, 2020
by
Joe Haddad
Committed by
GitHub
May 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Eliminate array destructuring assignment (#13151)
While we're at it, we should fix this too. x-ref: #13144
parent
b325f15e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
0 deletions
+75
-0
next-ssg-transform.ts
packages/next/build/babel/plugins/next-ssg-transform.ts
+53
-0
babel-plugin-next-ssg-transform.test.js
test/unit/babel-plugin-next-ssg-transform.test.js
+22
-0
No files found.
packages/next/build/babel/plugins/next-ssg-transform.ts
View file @
74d7e358
...
@@ -212,6 +212,28 @@ export default function nextTransformSsg({
...
@@ -212,6 +212,28 @@ export default function nextTransformSsg({
throw
new
Error
(
'invariant'
)
throw
new
Error
(
'invariant'
)
})()
})()
)
as
NodePath
<
BabelTypes
.
Identifier
>
)
as
NodePath
<
BabelTypes
.
Identifier
>
if
(
isIdentifierReferenced
(
local
))
{
state
.
refs
.
add
(
local
)
}
})
}
else
if
(
path
.
node
.
id
.
type
===
'ArrayPattern'
)
{
const
pattern
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
ArrayPattern
>
const
elements
=
pattern
.
get
(
'elements'
)
elements
.
forEach
((
e
)
=>
{
let
local
:
NodePath
<
BabelTypes
.
Identifier
>
if
(
e
.
node
?.
type
===
'Identifier'
)
{
local
=
e
as
NodePath
<
BabelTypes
.
Identifier
>
}
else
if
(
e
.
node
?.
type
===
'RestElement'
)
{
local
=
e
.
get
(
'argument'
)
as
NodePath
<
BabelTypes
.
Identifier
>
}
else
{
return
}
if
(
isIdentifierReferenced
(
local
))
{
if
(
isIdentifierReferenced
(
local
))
{
state
.
refs
.
add
(
local
)
state
.
refs
.
add
(
local
)
}
}
...
@@ -377,6 +399,37 @@ export default function nextTransformSsg({
...
@@ -377,6 +399,37 @@ export default function nextTransformSsg({
)
{
)
{
path
.
remove
()
path
.
remove
()
}
}
}
else
if
(
path
.
node
.
id
.
type
===
'ArrayPattern'
)
{
const
pattern
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
ArrayPattern
>
const
beforeCount
=
count
const
elements
=
pattern
.
get
(
'elements'
)
elements
.
forEach
((
e
)
=>
{
let
local
:
NodePath
<
BabelTypes
.
Identifier
>
if
(
e
.
node
?.
type
===
'Identifier'
)
{
local
=
e
as
NodePath
<
BabelTypes
.
Identifier
>
}
else
if
(
e
.
node
?.
type
===
'RestElement'
)
{
local
=
e
.
get
(
'argument'
)
as
NodePath
<
BabelTypes
.
Identifier
>
}
else
{
return
}
if
(
refs
.
has
(
local
)
&&
!
isIdentifierReferenced
(
local
))
{
++
count
e
.
remove
()
}
})
if
(
beforeCount
!==
count
&&
pattern
.
get
(
'elements'
).
length
<
1
)
{
path
.
remove
()
}
}
}
},
},
FunctionDeclaration
:
sweepFunction
,
FunctionDeclaration
:
sweepFunction
,
...
...
test/unit/babel-plugin-next-ssg-transform.test.js
View file @
74d7e358
...
@@ -476,5 +476,27 @@ describe('babel plugin (next-ssg-transform)', () => {
...
@@ -476,5 +476,27 @@ describe('babel plugin (next-ssg-transform)', () => {
`"import other from'other';const{a,cat:bar}=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
`"import other from'other';const{a,cat:bar}=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
)
})
})
it
(
'destructuring assignment (array)'
,
()
=>
{
const
output
=
babel
(
trim
`
import fs from 'fs';
import other from 'other';
const [a, b, ...rest]= fs.promises;
const [foo, bar] = other;
export async function getStaticProps() {
a;
b;
rest;
bar;
}
export default function Home() { return <div />; }
`
)
expect
(
output
).
toMatchInlineSnapshot
(
`"import other from'other';const[foo]=other;export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
})
})
})
})
})
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