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
04b2f0eb
Unverified
Commit
04b2f0eb
authored
Dec 07, 2019
by
Joe Haddad
Committed by
GitHub
Dec 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use Real Identifier for SSG Transform (#9656)
parent
cbf8e0b2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
11 deletions
+33
-11
next-ssg-transform.ts
packages/next/build/babel/plugins/next-ssg-transform.ts
+12
-11
babel-plugin-next-ssg-transform.test.js
test/unit/babel-plugin-next-ssg-transform.test.js
+21
-0
No files found.
packages/next/build/babel/plugins/next-ssg-transform.ts
View file @
04b2f0eb
...
@@ -12,7 +12,11 @@ const ssgExports = new Set([
...
@@ -12,7 +12,11 @@ const ssgExports = new Set([
EXPORT_NAME_GET_STATIC_PATHS
,
EXPORT_NAME_GET_STATIC_PATHS
,
])
])
type
PluginState
=
{
refs
:
Set
<
any
>
;
isPrerender
:
boolean
;
done
:
boolean
}
type
PluginState
=
{
refs
:
Set
<
NodePath
<
BabelTypes
.
Identifier
>>
isPrerender
:
boolean
done
:
boolean
}
function
decorateSsgExport
(
function
decorateSsgExport
(
t
:
typeof
BabelTypes
,
t
:
typeof
BabelTypes
,
...
@@ -106,7 +110,7 @@ export default function nextTransformSsg({
...
@@ -106,7 +110,7 @@ export default function nextTransformSsg({
)
{
)
{
const
ident
=
getIdentifier
(
path
)
const
ident
=
getIdentifier
(
path
)
if
(
ident
&&
ident
.
node
&&
isIdentifierReferenced
(
ident
))
{
if
(
ident
&&
ident
.
node
&&
isIdentifierReferenced
(
ident
))
{
state
.
refs
.
add
(
ident
.
node
.
name
)
state
.
refs
.
add
(
ident
)
}
}
}
}
...
@@ -120,7 +124,7 @@ export default function nextTransformSsg({
...
@@ -120,7 +124,7 @@ export default function nextTransformSsg({
)
{
)
{
const
local
=
path
.
get
(
'local'
)
const
local
=
path
.
get
(
'local'
)
if
(
isIdentifierReferenced
(
local
))
{
if
(
isIdentifierReferenced
(
local
))
{
state
.
refs
.
add
(
local
.
node
.
name
)
state
.
refs
.
add
(
local
)
}
}
}
}
...
@@ -128,7 +132,7 @@ export default function nextTransformSsg({
...
@@ -128,7 +132,7 @@ export default function nextTransformSsg({
visitor
:
{
visitor
:
{
Program
:
{
Program
:
{
enter
(
_
,
state
)
{
enter
(
_
,
state
)
{
state
.
refs
=
new
Set
<
string
>
()
state
.
refs
=
new
Set
<
NodePath
<
BabelTypes
.
Identifier
>
>
()
state
.
isPrerender
=
false
state
.
isPrerender
=
false
state
.
done
=
false
state
.
done
=
false
},
},
...
@@ -147,7 +151,7 @@ export default function nextTransformSsg({
...
@@ -147,7 +151,7 @@ export default function nextTransformSsg({
if
(
if
(
ident
&&
ident
&&
ident
.
node
&&
ident
.
node
&&
refs
.
has
(
ident
.
node
.
name
)
&&
refs
.
has
(
ident
)
&&
!
isIdentifierReferenced
(
ident
)
!
isIdentifierReferenced
(
ident
)
)
{
)
{
++
count
++
count
...
@@ -171,7 +175,7 @@ export default function nextTransformSsg({
...
@@ -171,7 +175,7 @@ export default function nextTransformSsg({
>
>
)
{
)
{
const
local
=
path
.
get
(
'local'
)
const
local
=
path
.
get
(
'local'
)
if
(
refs
.
has
(
local
.
node
.
name
)
&&
!
isIdentifierReferenced
(
local
))
{
if
(
refs
.
has
(
local
)
&&
!
isIdentifierReferenced
(
local
))
{
++
count
++
count
path
.
remove
()
path
.
remove
()
if
(
if
(
...
@@ -195,10 +199,7 @@ export default function nextTransformSsg({
...
@@ -195,10 +199,7 @@ export default function nextTransformSsg({
}
}
const
local
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
Identifier
>
const
local
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
Identifier
>
if
(
if
(
refs
.
has
(
local
)
&&
!
isIdentifierReferenced
(
local
))
{
refs
.
has
(
local
.
node
.
name
)
&&
!
isIdentifierReferenced
(
local
)
)
{
++
count
++
count
path
.
remove
()
path
.
remove
()
}
}
...
@@ -224,7 +225,7 @@ export default function nextTransformSsg({
...
@@ -224,7 +225,7 @@ export default function nextTransformSsg({
const
local
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
Identifier
>
const
local
=
path
.
get
(
'id'
)
as
NodePath
<
BabelTypes
.
Identifier
>
if
(
isIdentifierReferenced
(
local
))
{
if
(
isIdentifierReferenced
(
local
))
{
state
.
refs
.
add
(
path
.
node
.
id
.
name
)
state
.
refs
.
add
(
local
)
}
}
},
},
FunctionDeclaration
:
markFunction
,
FunctionDeclaration
:
markFunction
,
...
...
test/unit/babel-plugin-next-ssg-transform.test.js
View file @
04b2f0eb
...
@@ -308,5 +308,26 @@ describe('babel plugin (next-ssg-transform)', () => {
...
@@ -308,5 +308,26 @@ describe('babel plugin (next-ssg-transform)', () => {
`"import keep_me from'hello';import{keep_me2}from'hello2';import*as keep_me3 from'hello3';import{but_not_me}from'bar';var leave_me_alone=1;function dont_bug_me_either(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
`"import keep_me from'hello';import{keep_me2}from'hello2';import*as keep_me3 from'hello3';import{but_not_me}from'bar';var leave_me_alone=1;function dont_bug_me_either(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
)
)
})
})
it
(
'should not mix up bindings'
,
()
=>
{
const
output
=
babel
(
trim
`
function Function1() {
return {
a: function bug(a) {
return 2;
}
};
}
function Function2() {
var bug = 1;
return { bug };
}
`
)
expect
(
output
).
toMatchInlineSnapshot
(
`"function Function1(){return{a:function bug(a){return 2;}};}function Function2(){var bug=1;return{bug};}"`
)
})
})
})
})
})
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