Skip to content

Commit

Permalink
feat: show a warning about unrecognized export config
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 18, 2022
1 parent f35478e commit ed4eae8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/analysis/extract-const-value.ts
Expand Up @@ -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)) {
Expand Down
29 changes: 26 additions & 3 deletions 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'
Expand Down Expand Up @@ -32,13 +36,32 @@ export async function getPageStaticInfo(params: {
isDev?: boolean
page?: string
}): Promise<PageStaticInfo> {
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' &&
Expand Down

0 comments on commit ed4eae8

Please sign in to comment.