From ed4eae8e9554c1d8646496d2a0b4fb55c6fd264e Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 18 Jul 2022 23:18:05 +0800 Subject: [PATCH] feat: show a warning about unrecognized `export config` --- .../build/analysis/extract-const-value.ts | 4 +-- .../build/analysis/get-page-static-info.ts | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/next/build/analysis/extract-const-value.ts b/packages/next/build/analysis/extract-const-value.ts index edfb89ea5e29..0cca5dfaba89 100644 --- a/packages/next/build/analysis/extract-const-value.ts +++ b/packages/next/build/analysis/extract-const-value.ts @@ -129,8 +129,8 @@ function isTemplateLiteral(node: Node): node is TemplateLiteral { return node.type === 'TemplateLiteral' } -class UnsupportedValueError extends Error {} -class NoSuchDeclarationError extends Error {} +export class UnsupportedValueError extends Error {} +export class NoSuchDeclarationError extends Error {} function extractValue(node: Node): any { if (isNullLiteral(node)) { diff --git a/packages/next/build/analysis/get-page-static-info.ts b/packages/next/build/analysis/get-page-static-info.ts index 60d94aa800e3..1a65f630c02b 100644 --- a/packages/next/build/analysis/get-page-static-info.ts +++ b/packages/next/build/analysis/get-page-static-info.ts @@ -1,6 +1,10 @@ import { isServerRuntime, ServerRuntime } from '../../server/config-shared' import type { NextConfig } from '../../server/config-shared' -import { tryToExtractExportedConstValue } from './extract-const-value' +import { + extractExportedConstValue, + NoSuchDeclarationError, + UnsupportedValueError, +} from './extract-const-value' import { escapeStringRegexp } from '../../shared/lib/escape-regexp' import { parseModule } from './parse-module' import { promises as fs } from 'fs' @@ -32,13 +36,32 @@ export async function getPageStaticInfo(params: { isDev?: boolean page?: string }): Promise { - const { isDev, pageFilePath, nextConfig } = params + const { isDev, pageFilePath, nextConfig, page } = params const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || '' if (/runtime|getStaticProps|getServerSideProps|matcher/.test(fileContent)) { const swcAST = await parseModule(pageFilePath, fileContent) const { ssg, ssr } = checkExports(swcAST) - const config = tryToExtractExportedConstValue(swcAST, 'config') || {} + + // default value for config + let config: any = {} + try { + config = extractExportedConstValue(swcAST, 'config') + } catch (e) { + if (e instanceof UnsupportedValueError) { + // `export config` is found, but can't extract the value of it + Log.warn( + `You have exported a \`config\` field in "${ + page || pageFilePath + }" that Next.js can't recognize, so it will be ignored` + ) + } else if (e instanceof NoSuchDeclarationError) { + // Can not find `export config` in the file, just ignore it + } else { + // Unknown error, throw it + throw e + } + } if ( typeof config.runtime !== 'string' &&