Commit 7a51b888 authored by Joe Haddad's avatar Joe Haddad Committed by JJ Kasper

Add SSG Support for VariableDeclarations (#9641)

parent a718df5f
...@@ -69,6 +69,30 @@ export default function nextTransformSsg({ ...@@ -69,6 +69,30 @@ export default function nextTransformSsg({
} }
} }
}, },
// export const unstable_getStaticPaths = () => {}
VariableDeclaration(path) {
if (path.parent.type !== 'ExportNamedDeclaration') {
return
}
path.node.declarations = path.node.declarations.filter(d => {
const name = d.id.type === 'Identifier' && d.id.name
const isPrerender =
name === EXPORT_NAME_GET_STATIC_PROPS ||
name === EXPORT_NAME_GET_STATIC_PATHS
if (isPrerender) {
state.isPrerender = true
}
return !isPrerender
})
if (path.node.declarations.length === 0) {
path.parentPath.remove()
}
},
}) })
}, },
exit(path, state) { exit(path, state) {
......
...@@ -127,5 +127,65 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -127,5 +127,65 @@ describe('babel plugin (next-ssg-transform)', () => {
`"export function Noop(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"export function Noop(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
it('should remove named export variable declarations', () => {
const output = babel(trim`
export const unstable_getStaticPaths = () => {
return []
}
export const unstable_getStaticProps = function() {
return { props: {} }
}
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 named export variable declarations (async)', () => {
const output = babel(trim`
export const unstable_getStaticPaths = async () => {
return []
}
export const unstable_getStaticProps = async function() {
return { props: {} }
}
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 not remove extra named export variable declarations', () => {
const output = babel(trim`
export const unstable_getStaticPaths = () => {
return []
}, foo = 2
export const unstable_getStaticProps = function() {
return { props: {} }
}
export default function Test() {
return <div />
}
`)
expect(output).toMatchInlineSnapshot(
`"export const foo=2;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