Unverified Commit 202ea6fb authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Support eliminating memoized functions (#12953)

parent 7edc815a
import { NodePath, PluginObj, types as BabelTypes } from '@babel/core' import { NodePath, PluginObj, types as BabelTypes } from '@babel/core'
import { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants' import { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants'
import { import {
STATIC_PROPS_ID,
SERVER_PROPS_ID, SERVER_PROPS_ID,
STATIC_PROPS_ID,
} from '../../../next-server/lib/constants' } from '../../../next-server/lib/constants'
export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps' export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps'
...@@ -134,7 +134,19 @@ export default function nextTransformSsg({ ...@@ -134,7 +134,19 @@ export default function nextTransformSsg({
ident: NodePath<BabelTypes.Identifier> ident: NodePath<BabelTypes.Identifier>
): boolean { ): boolean {
const b = ident.scope.getBinding(ident.node.name) const b = ident.scope.getBinding(ident.node.name)
return b != null && b.referenced if (b?.referenced) {
// Functions can reference themselves, so we need to check if there's a
// binding outside the function scope or not.
if (b.path.type === 'FunctionDeclaration') {
return !b.constantViolations
.concat(b.referencePaths)
// Check that every reference is contained within the function:
.every(ref => ref.findParent(p => p === b.path))
}
return true
}
return false
} }
function markFunction( function markFunction(
......
...@@ -439,5 +439,22 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -439,5 +439,22 @@ describe('babel plugin (next-ssg-transform)', () => {
`"export var __N_SSG=true;export{default}from'a';"` `"export var __N_SSG=true;export{default}from'a';"`
) )
}) })
it('should support babel-style memoized function', () => {
const output = babel(trim`
function fn() {
fn = function () {};
return fn.apply(this, arguments);
}
export function getStaticProps() {
fn;
}
export default function Home() { return <div />; }
`)
expect(output).toMatchInlineSnapshot(
`"export var __N_SSG=true;export default function Home(){return __jsx(\\"div\\",null);}"`
)
})
}) })
}) })
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