From dcbfbde89484393c4c006fbe7a9030e0ca6e35c3 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 | 24 ++++++++++++++++--- 2 files changed, 23 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..7d2c4d73dbd7 100644 --- a/packages/next/build/analysis/get-page-static-info.ts +++ b/packages/next/build/analysis/get-page-static-info.ts @@ -1,6 +1,9 @@ import { isServerRuntime, ServerRuntime } from '../../server/config-shared' import type { NextConfig } from '../../server/config-shared' -import { tryToExtractExportedConstValue } from './extract-const-value' +import { + extractExportedConstValue, + 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 +35,28 @@ 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 / failsafe 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 its value + Log.warn( + `You have exported a \`config\` field in "${ + page || pageFilePath + }" that Next.js can't recognize, so it will be ignored` + ) + } + // `export config` doesn't exist, or other unknown error throw by swc, silence them + } if ( typeof config.runtime !== 'string' &&