Unverified Commit 99aea513 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

enhance: cover re-exported gsp cases for runtime fallback (#35326)

Cover re-exported gsp cases from #35011
parent 2e16d02f
...@@ -144,7 +144,7 @@ export async function getPageRuntime( ...@@ -144,7 +144,7 @@ export async function getPageRuntime(
for (const node of body) { for (const node of body) {
const { type, declaration } = node const { type, declaration } = node
if (type === 'ExportDeclaration') { if (type === 'ExportDeclaration') {
// `export const config` // Match `export const config`
const valueNode = declaration?.declarations?.[0] const valueNode = declaration?.declarations?.[0]
if (valueNode?.id?.value === 'config') { if (valueNode?.id?.value === 'config') {
const props = valueNode.init.properties const props = valueNode.init.properties
...@@ -155,15 +155,30 @@ export async function getPageRuntime( ...@@ -155,15 +155,30 @@ export async function getPageRuntime(
pageRuntime = pageRuntime =
runtime === 'edge' || runtime === 'nodejs' ? runtime : pageRuntime runtime === 'edge' || runtime === 'nodejs' ? runtime : pageRuntime
} else if (declaration?.type === 'FunctionDeclaration') { } else if (declaration?.type === 'FunctionDeclaration') {
// `export function getStaticProps` and // Match `export function getStaticProps | getServerSideProps`
// `export function getServerSideProps` const identifier = declaration.identifier?.value
if ( if (
declaration.identifier?.value === 'getStaticProps' || identifier === 'getStaticProps' ||
declaration.identifier?.value === 'getServerSideProps' identifier === 'getServerSideProps'
) { ) {
isRuntimeRequired = true isRuntimeRequired = true
} }
} }
} else if (type === 'ExportNamedDeclaration') {
// Match `export { getStaticProps | getServerSideProps } <from '../..'>`
const { specifiers } = node
for (const specifier of specifiers) {
const { orig } = specifier
const hasDataFetchingExports =
specifier.type === 'ExportSpecifier' &&
orig?.type === 'Identifier' &&
(orig?.value === 'getStaticProps' ||
orig?.value === 'getServerSideProps')
if (hasDataFetchingExports) {
isRuntimeRequired = true
break
}
}
} }
} }
} catch (err) {} } catch (err) {}
......
export { getStaticProps } from '../lib/utils'
export default function ImportGsp({ gsp }) {
return `import-${gsp}`
}
...@@ -24,10 +24,20 @@ describe('parse page runtime config', () => { ...@@ -24,10 +24,20 @@ describe('parse page runtime config', () => {
) )
expect(runtime).toBe(undefined) expect(runtime).toBe(undefined)
}) })
})
describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
'edge'
)
expect(runtime).toBe('edge')
})
it('should fallback to the global runtime configuration if a runtime is needed', async () => { it('should fallback when gSP is re-exported from other module', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback.js'), join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
'edge' 'edge'
) )
expect(runtime).toBe('edge') expect(runtime).toBe('edge')
......
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