Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/next/build/babel/plugins/next-ssg-transform.ts
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([
Timer marked this conversation as resolved.
Show resolved Hide resolved
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
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};"`
)
})
})
})