Unverified Commit a718df5f authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Rewrite SSG Transform (#9640)

* Rewrite SSG Transform

* Add Named Declaration Support

* more adjustments

* Re-crawl scope
parent b4d0740c
import { NodePath, PluginObj } from '@babel/core'
import { PluginObj } from '@babel/core'
import * as BabelTypes from '@babel/types'
const pageComponentVar = '__NEXT_COMP'
const prerenderId = '__NEXT_SPR'
export const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps'
const EXPORT_NAME_GET_STATIC_PATHS = 'unstable_getStaticPaths'
interface ConfigState {
isPrerender?: boolean
defaultExportUpdated?: boolean
}
export const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps'
export const EXPORT_NAME_GET_STATIC_PATHS = 'unstable_getStaticPaths'
export default function nextSsgTransform({
export default function nextTransformSsg({
types: t,
traverse,
}: {
types: typeof BabelTypes
traverse: any
}): PluginObj {
}): PluginObj<{
isPrerender: boolean
done: boolean
}> {
return {
visitor: {
Program: {
enter(path, state: ConfigState) {
path.traverse(
{
ExportNamedDeclaration(
path: NodePath<BabelTypes.ExportNamedDeclaration>,
state: any
) {
if (
state.bundleDropped ||
!(path.node.declaration || path.node.specifiers.length)
) {
return
}
enter(path, state) {
path.traverse({
// export function unstable_getStaticPaths() {}
ExportNamedDeclaration(path) {
const declaration = path.node.declaration
if (!declaration) {
return
}
if (path.node.specifiers.length) {
const { specifiers } = path.node
specifiers.forEach(specifier => {
if (specifier.type !== 'ExportSpecifier') {
return
}
if (declaration.type === 'VariableDeclaration') {
return
}
if (
specifier.exported.name ===
EXPORT_NAME_GET_STATIC_PROPS ||
specifier.exported.name === EXPORT_NAME_GET_STATIC_PATHS
) {
if (
specifier.exported.name === EXPORT_NAME_GET_STATIC_PROPS
) {
state.isPrerender = true
}
path.node.specifiers = path.node.specifiers.filter(
s => s !== specifier
)
return
}
})
if (path.node.specifiers.length === 0) {
path.remove()
}
return
const name =
declaration.type === 'FunctionDeclaration'
? declaration.id && declaration.id.name
: null
if (name == null) {
throw new Error(`invariant: null function declaration`)
}
if (
name === EXPORT_NAME_GET_STATIC_PROPS ||
name === EXPORT_NAME_GET_STATIC_PATHS
) {
path.remove()
state.isPrerender = true
}
},
// export { unstable_getStaticPaths } from '.'
ExportSpecifier(path) {
const name = path.node.exported.name
if (
name === EXPORT_NAME_GET_STATIC_PROPS ||
name === EXPORT_NAME_GET_STATIC_PATHS
) {
path.remove()
state.isPrerender = true
if (path.parent.type !== 'ExportNamedDeclaration') {
throw new Error(
`invariant: ${path.type} has unknown parent: ${path.parent.type}`
)
}
const { id } = path.node.declaration as any
if (
id &&
(id.name === EXPORT_NAME_GET_STATIC_PROPS ||
id.name === EXPORT_NAME_GET_STATIC_PATHS)
) {
if (id.name === EXPORT_NAME_GET_STATIC_PROPS) {
state.isPrerender = true
}
path.remove()
return
if (path.parent.specifiers.length === 0) {
path.parentPath.remove()
}
},
}
},
state
)
})
},
exit(path, state) {
if (state.isPrerender) {
// After we delete a bunch of code, we need to re-compute the scope.
// This is necessary for later code elimination.
traverse.cache.clear()
;(path.scope as any).crawl()
}
},
},
ExportDefaultDeclaration(path, state: ConfigState) {
if (!state.isPrerender || state.defaultExportUpdated) {
return
}
const prev = t.cloneDeep(path.node.declaration)
// workaround to allow assigning a ClassDeclaration to a variable
// babel throws error without
if (prev.type.endsWith('Declaration')) {
prev.type = prev.type.replace(/Declaration$/, 'Expression') as any
}
path.traverse({
ExportDefaultDeclaration(path) {
if (!state.isPrerender || state.done) {
return
}
// @ts-ignore invalid return type
const [pageCompPath] = path.replaceWithMultiple([
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(pageComponentVar), prev as any),
]),
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier(pageComponentVar),
t.identifier(prerenderId)
),
t.booleanLiteral(true)
),
t.exportDefaultDeclaration(t.identifier(pageComponentVar)),
])
path.scope.registerDeclaration(pageCompPath)
state.defaultExportUpdated = true
state.done = true
const prev = path.node.declaration
if (prev.type.endsWith('Declaration')) {
prev.type = prev.type.replace(
/Declaration$/,
'Expression'
) as any
}
// @ts-ignore invalid return type
const [pageCompPath] = path.replaceWithMultiple([
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(pageComponentVar),
prev as any
),
]),
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier(pageComponentVar),
t.identifier(prerenderId)
),
t.booleanLiteral(true)
),
t.exportDefaultDeclaration(t.identifier(pageComponentVar)),
])
path.scope.registerDeclaration(pageCompPath)
},
})
},
},
},
}
......
......@@ -70,7 +70,7 @@ describe('babel plugin (next-ssg-transform)', () => {
)
})
it('should remove named export declarations', () => {
it('should remove named export function declarations', () => {
const output = babel(trim`
export function unstable_getStaticPaths() {
return []
......@@ -90,7 +90,27 @@ describe('babel plugin (next-ssg-transform)', () => {
)
})
it('should not remove extra named export declarations', () => {
it('should remove named export function declarations (async)', () => {
const output = babel(trim`
export async function unstable_getStaticPaths() {
return []
}
export async function unstable_getStaticProps() {
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 function declarations', () => {
const output = babel(trim`
export function unstable_getStaticProps() {
return { props: {} }
......
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