Unverified Commit 46179504 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add handling for default as named export in SSG transform (#10486)

* Add failing SSG syntax

* Add handling for default as named export in SSG transform

* Revert [comment].js

* Revert index.js

* adjust
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent 0b12e2e8
......@@ -57,6 +57,32 @@ function decorateSsgExport(
])
path.scope.registerDeclaration(pageCompPath)
},
ExportNamedDeclaration(path) {
if (state.done) {
return
}
// Look for a `export { _ as default }` specifier
const defaultSpecifier = path.node.specifiers.find(s => {
return s.exported.name === 'default'
})
if (!defaultSpecifier) {
return
}
state.done = true
path.replaceWithMultiple([
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier((defaultSpecifier as any).local.name),
t.identifier(state.isPrerender ? prerenderId : serverPropsId)
),
t.booleanLiteral(true)
),
path.node,
])
},
})
}
......
......@@ -331,5 +331,103 @@ describe('babel plugin (next-ssg-transform)', () => {
`"function Function1(){return{a:function bug(a){return 2;}};}function Function2(){var bug=1;return{bug};}"`
)
})
it('should support class exports', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
}
export default class Test extends React.Component {
render() {
return <div />
}
}
`)
expect(output).toMatchInlineSnapshot(
`"const __NEXT_COMP=class Test extends React.Component{render(){return __jsx(\\"div\\",null);}};__NEXT_COMP.__N_SSG=true export default __NEXT_COMP;"`
)
})
it('should support class exports 2', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
}
class Test extends React.Component {
render() {
return <div />
}
}
export default Test;
`)
expect(output).toMatchInlineSnapshot(
`"class Test extends React.Component{render(){return __jsx(\\"div\\",null);}}const __NEXT_COMP=Test;__NEXT_COMP.__N_SSG=true export default __NEXT_COMP;"`
)
})
it('should support export { _ as default }', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
}
function El() {
return <div />
}
export { El as default }
`)
expect(output).toMatchInlineSnapshot(
`"function El(){return __jsx(\\"div\\",null);}El.__N_SSG=true export{El as default};"`
)
})
it('should support export { _ as default } with other specifiers', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
}
function El() {
return <div />
}
const a = 5
export { El as default, a }
`)
expect(output).toMatchInlineSnapshot(
`"function El(){return __jsx(\\"div\\",null);}const a=5;El.__N_SSG=true export{El as default,a};"`
)
})
it('should support export { _ as default } with a class', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
}
class El extends React.Component {
render() {
return <div />
}
}
const a = 5
export { El as default, a }
`)
expect(output).toMatchInlineSnapshot(
`"class El extends React.Component{render(){return __jsx(\\"div\\",null);}}const a=5;El.__N_SSG=true export{El as default,a};"`
)
})
})
})
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