From 690092fdb8ec86d7daf7969e63cae99b0b867f76 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 15 Mar 2022 10:40:32 +0100 Subject: [PATCH 1/3] enhance: cover re-exported gsp cases for runtime fallback --- packages/next/build/entries.ts | 22 ++++++++++++++----- .../page-runtime/fallback-re-export-gsp.js | 5 +++++ .../{fallback.js => fallback-with-gsp.js} | 0 test/unit/parse-page-runtime.test.ts | 14 ++++++++++-- 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 test/unit/fixtures/page-runtime/fallback-re-export-gsp.js rename test/unit/fixtures/page-runtime/{fallback.js => fallback-with-gsp.js} (100%) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 1ac79e357ed3..52bc33e03ee6 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -144,7 +144,7 @@ export async function getPageRuntime( for (const node of body) { const { type, declaration } = node if (type === 'ExportDeclaration') { - // `export const config` + // Match `export const config` const valueNode = declaration?.declarations?.[0] if (valueNode?.id?.value === 'config') { const props = valueNode.init.properties @@ -155,15 +155,27 @@ export async function getPageRuntime( pageRuntime = runtime === 'edge' || runtime === 'nodejs' ? runtime : pageRuntime } else if (declaration?.type === 'FunctionDeclaration') { - // `export function getStaticProps` and - // `export function getServerSideProps` + // Match `export function getStaticProps | getServerSideProps` + const identifier = declaration.identifier?.value if ( - declaration.identifier?.value === 'getStaticProps' || - declaration.identifier?.value === 'getServerSideProps' + identifier === 'getStaticProps' || + identifier === 'getServerSideProps' ) { isRuntimeRequired = true } } + } else if (type === 'ExportNamedDeclaration') { + // Match `export { getStaticProps | getServerSideProps } ` + const { specifiers } = node + specifiers.some((specifier: any) => { + const { orig } = specifier + const hasDataFetchingExports = + specifier.type === 'ExportSpecifier' && + orig?.type === 'Identifier' && + orig?.value === 'getStaticProps' && + orig?.value === 'getServerSideProps' + if (hasDataFetchingExports) isRuntimeRequired = true + }) } } } catch (err) {} diff --git a/test/unit/fixtures/page-runtime/fallback-re-export-gsp.js b/test/unit/fixtures/page-runtime/fallback-re-export-gsp.js new file mode 100644 index 000000000000..4cb8568fa50a --- /dev/null +++ b/test/unit/fixtures/page-runtime/fallback-re-export-gsp.js @@ -0,0 +1,5 @@ +export { getStaticProps } from '../lib/utils' + +export default function ImportGsp({ gsp }) { + return `import-${gsp}` +} diff --git a/test/unit/fixtures/page-runtime/fallback.js b/test/unit/fixtures/page-runtime/fallback-with-gsp.js similarity index 100% rename from test/unit/fixtures/page-runtime/fallback.js rename to test/unit/fixtures/page-runtime/fallback-with-gsp.js diff --git a/test/unit/parse-page-runtime.test.ts b/test/unit/parse-page-runtime.test.ts index d567e5e70bf3..11b4946ec1a2 100644 --- a/test/unit/parse-page-runtime.test.ts +++ b/test/unit/parse-page-runtime.test.ts @@ -24,10 +24,20 @@ describe('parse page runtime config', () => { ) 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( - join(fixtureDir, 'page-runtime/fallback.js'), + join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'), 'edge' ) expect(runtime).toBe('edge') From 5b9c908e381153660b3ead878d871122dd8ecbe4 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 15 Mar 2022 12:39:50 +0100 Subject: [PATCH 2/3] fix cond --- packages/next/build/entries.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 52bc33e03ee6..7183b506506b 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -172,9 +172,13 @@ export async function getPageRuntime( const hasDataFetchingExports = specifier.type === 'ExportSpecifier' && orig?.type === 'Identifier' && - orig?.value === 'getStaticProps' && - orig?.value === 'getServerSideProps' - if (hasDataFetchingExports) isRuntimeRequired = true + (orig?.value === 'getStaticProps' || + orig?.value === 'getServerSideProps') + if (hasDataFetchingExports) { + isRuntimeRequired = true + return true + } + return false }) } } From 6b1fdebda56a859521e6e73f6c437e0390d5248b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 15 Mar 2022 12:58:42 +0100 Subject: [PATCH 3/3] fix lint --- packages/next/build/entries.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 7183b506506b..304b7047b149 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -167,7 +167,7 @@ export async function getPageRuntime( } else if (type === 'ExportNamedDeclaration') { // Match `export { getStaticProps | getServerSideProps } ` const { specifiers } = node - specifiers.some((specifier: any) => { + for (const specifier of specifiers) { const { orig } = specifier const hasDataFetchingExports = specifier.type === 'ExportSpecifier' && @@ -176,10 +176,9 @@ export async function getPageRuntime( orig?.value === 'getServerSideProps') if (hasDataFetchingExports) { isRuntimeRequired = true - return true + break } - return false - }) + } } } } catch (err) {}