Skip to content

Commit

Permalink
Add handling for default as named export in SSG transform (#10486)
Browse files Browse the repository at this point in the history
* Add failing SSG syntax

* Add handling for default as named export in SSG transform

* Revert [comment].js

* Revert index.js

* adjust

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
ijjk and Timer authored Feb 13, 2020
1 parent 0b12e2e commit 4617950
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/next/build/babel/plugins/next-ssg-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
])
},
})
}

Expand Down
98 changes: 98 additions & 0 deletions test/unit/babel-plugin-next-ssg-transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};"`
)
})
})
})

0 comments on commit 4617950

Please sign in to comment.