Unverified Commit e76fc186 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Remove Scope Bindings for SSG (#9642)

parent 7a51b888
...@@ -55,16 +55,27 @@ export default function nextTransformSsg({ ...@@ -55,16 +55,27 @@ export default function nextTransformSsg({
name === EXPORT_NAME_GET_STATIC_PROPS || name === EXPORT_NAME_GET_STATIC_PROPS ||
name === EXPORT_NAME_GET_STATIC_PATHS name === EXPORT_NAME_GET_STATIC_PATHS
) { ) {
path.remove()
state.isPrerender = true state.isPrerender = true
if (path.parent.type !== 'ExportNamedDeclaration') { const parent = path.parent
if (parent.type !== 'ExportNamedDeclaration') {
throw new Error( throw new Error(
`invariant: ${path.type} has unknown parent: ${path.parent.type}` `invariant: ${path.type} has unknown parent: ${parent.type}`
) )
} }
if (path.parent.specifiers.length === 0) { if (!parent.source) {
const localName = path.node.local.name
const binding = path.scope.getBinding(localName)
if (binding) {
binding.path.remove()
}
}
path.remove()
if (parent.specifiers.length === 0) {
path.parentPath.remove() path.parentPath.remove()
} }
} }
......
...@@ -187,5 +187,41 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -187,5 +187,41 @@ describe('babel plugin (next-ssg-transform)', () => {
`"export const foo=2;const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"export const foo=2;const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
it('should remove re-exported variable declarations', () => {
const output = babel(trim`
const unstable_getStaticPaths = () => {
return []
}
export { unstable_getStaticPaths }
export default function Test() {
return <div />
}
`)
expect(output).toMatchInlineSnapshot(
`"const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
)
})
it('should remove re-exported function declarations', () => {
const output = babel(trim`
function unstable_getStaticPaths() {
return []
}
export { unstable_getStaticPaths }
export default function Test() {
return <div />
}
`)
expect(output).toMatchInlineSnapshot(
`"const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
)
})
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment