Skip to content

Commit

Permalink
Merge branch 'hotfix/export--as-default' of github.com:Timer/next.js …
Browse files Browse the repository at this point in the history
…into hotfix/export--as-default
  • Loading branch information
Timer committed Feb 13, 2020
2 parents 676940d + 33edf60 commit 504989b
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 157 deletions.
54 changes: 39 additions & 15 deletions packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NodePath, PluginObj } from '@babel/core'
import * as BabelTypes from '@babel/types'
import { PageConfig } from 'next/types'

const configKeys = new Set(['amp'])
const STRING_LITERAL_DROP_BUNDLE = '__NEXT_DROP_CLIENT_FILE__'

// replace program path with just a variable with the drop identifier
Expand All @@ -26,6 +25,12 @@ function replaceBundle(path: any, t: typeof BabelTypes) {
)
}

function errorMessage(state: any, details: string) {
const pageName =
(state.filename || '').split(state.cwd || '').pop() || 'unknown'
return `Invalid page config export found. ${details} in file ${pageName}. See: https://err.sh/zeit/next.js/invalid-page-config`
}

interface ConfigState {
bundleDropped?: boolean
}
Expand All @@ -50,32 +55,51 @@ export default function nextPageConfig({
return
}

const { declarations } = path.node.declaration as any
const config: PageConfig = {}

if (!declarations) {
if (!BabelTypes.isVariableDeclaration(path.node.declaration)) {
return
}

const { declarations } = path.node.declaration
const config: PageConfig = {}

for (const declaration of declarations) {
if (declaration.id.name !== 'config') {
if (
!BabelTypes.isIdentifier(declaration.id, { name: 'config' })
) {
continue
}

if (declaration.init.type !== 'ObjectExpression') {
const pageName =
(state.filename || '').split(state.cwd || '').pop() ||
'unknown'

if (!BabelTypes.isObjectExpression(declaration.init)) {
const got = declaration.init
? declaration.init.type
: 'undefined'
throw new Error(
`Invalid page config export found. Expected object but got ${declaration.init.type} in file ${pageName}. See: https://err.sh/zeit/next.js/invalid-page-config`
errorMessage(state, `Expected object but got ${got}`)
)
}

for (const prop of declaration.init.properties) {
if (BabelTypes.isSpreadElement(prop)) {
throw new Error(
errorMessage(state, `Property spread is not allowed`)
)
}
const { name } = prop.key
if (configKeys.has(name)) {
// @ts-ignore
config[name] = prop.value.value
if (BabelTypes.isIdentifier(prop.key, { name: 'amp' })) {
if (!BabelTypes.isObjectProperty(prop)) {
throw new Error(
errorMessage(state, `Invalid property "${name}"`)
)
}
if (
!BabelTypes.isBooleanLiteral(prop.value) &&
!BabelTypes.isStringLiteral(prop.value)
) {
throw new Error(
errorMessage(state, `Invalid value for "${name}"`)
)
}
config.amp = prop.value.value as PageConfig['amp']
}
}
}
Expand Down
Loading

0 comments on commit 504989b

Please sign in to comment.