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 3 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
27 changes: 27 additions & 0 deletions packages/next/build/babel/plugins/next-ssg-transform.ts
Expand Up @@ -57,6 +57,33 @@ function decorateSsgExport(
])
path.scope.registerDeclaration(pageCompPath)
},
ExportNamedDeclaration(path) {
if (state.done) {
return
}
// we need to handle the default export being a named export
// for `export default class Page extends Component {}` syntax
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
37 changes: 22 additions & 15 deletions test/integration/prerender/pages/blog/[post]/[comment].js
Expand Up @@ -23,20 +23,27 @@ export async function unstable_getStaticProps({ params }) {
}
}

export default ({ post, comment, time }) => {
// we're in a loading state
if (!post) {
return <p>loading...</p>
}
// Do not change this, it is making sure our SSG transform handles
// export default in this way with a class component
class Page extends React.Component {
render() {
const { post, comment, time } = this.props
// we're in a loading state
if (!post) {
return <p>loading...</p>
}

return (
<>
<p>Post: {post}</p>
<p>Comment: {comment}</p>
<span>time: {time}</span>
<Link href="/">
<a id="home">to home</a>
</Link>
</>
)
return (
<>
<p>Post: {post}</p>
<p>Comment: {comment}</p>
<span>time: {time}</span>
<Link href="/">
<a id="home">to home</a>
</Link>
</>
)
}
}

export default Page
33 changes: 21 additions & 12 deletions test/integration/prerender/pages/blog/[post]/index.js
Expand Up @@ -38,16 +38,25 @@ export async function unstable_getStaticProps({ params }) {
}
}

export default ({ post, time, params }) => {
return (
<>
<p>Post: {post}</p>
<span>time: {time}</span>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>
</Link>
</>
)
function Query() {
return <div id="query">{JSON.stringify(useRouter().query)}</div>
}

// Do not change this, it is making sure our SSG transform handles
// export default in this way with a class component
export default class Page extends React.Component {
render() {
const { post, time, params } = this.props
return (
<>
<p>Post: {post}</p>
<span>time: {time}</span>
<div id="params">{JSON.stringify(params)}</div>
<Query />
<Link href="/">
<a id="home">to home</a>
</Link>
</>
)
}
}