2020-04-22 07:25:43 +02:00
|
|
|
import { NodePath, PluginObj, types as BabelTypes } from '@babel/core'
|
2020-01-25 05:25:11 +01:00
|
|
|
import { PageConfig } from 'next/types'
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-11-25 20:32:25 +01:00
|
|
|
const STRING_LITERAL_DROP_BUNDLE = '__NEXT_DROP_CLIENT_FILE__'
|
2019-08-06 22:26:01 +02:00
|
|
|
|
|
|
|
// replace program path with just a variable with the drop identifier
|
2020-05-21 14:57:04 +02:00
|
|
|
function replaceBundle(path: any, t: typeof BabelTypes): void {
|
2019-06-10 02:16:14 +02:00
|
|
|
path.parentPath.replaceWith(
|
|
|
|
t.program(
|
|
|
|
[
|
|
|
|
t.variableDeclaration('const', [
|
|
|
|
t.variableDeclarator(
|
|
|
|
t.identifier('config'),
|
|
|
|
t.assignmentExpression(
|
|
|
|
'=',
|
2019-11-25 20:32:25 +01:00
|
|
|
t.identifier(STRING_LITERAL_DROP_BUNDLE),
|
|
|
|
t.stringLiteral(`${STRING_LITERAL_DROP_BUNDLE} ${Date.now()}`)
|
2019-06-10 02:16:14 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-21 14:57:04 +02:00
|
|
|
function errorMessage(state: any, details: string): string {
|
2020-02-13 05:39:51 +01:00
|
|
|
const pageName =
|
|
|
|
(state.filename || '').split(state.cwd || '').pop() || 'unknown'
|
2020-05-27 23:51:11 +02:00
|
|
|
return `Invalid page config export found. ${details} in file ${pageName}. See: https://err.sh/vercel/next.js/invalid-page-config`
|
2020-02-13 05:39:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
interface ConfigState {
|
|
|
|
bundleDropped?: boolean
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
// config to parsing pageConfig for client bundles
|
2019-06-10 02:16:14 +02:00
|
|
|
export default function nextPageConfig({
|
|
|
|
types: t,
|
|
|
|
}: {
|
|
|
|
types: typeof BabelTypes
|
2019-07-01 23:13:52 +02:00
|
|
|
}): PluginObj {
|
2019-06-10 02:16:14 +02:00
|
|
|
return {
|
|
|
|
visitor: {
|
|
|
|
Program: {
|
2019-07-01 23:13:52 +02:00
|
|
|
enter(path, state: ConfigState) {
|
2019-06-10 02:16:14 +02:00
|
|
|
path.traverse(
|
|
|
|
{
|
|
|
|
ExportNamedDeclaration(
|
|
|
|
path: NodePath<BabelTypes.ExportNamedDeclaration>,
|
|
|
|
state: any
|
|
|
|
) {
|
2019-12-05 22:43:11 +01:00
|
|
|
if (state.bundleDropped || !path.node.declaration) {
|
2019-11-25 17:44:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-13 05:39:51 +01:00
|
|
|
if (!BabelTypes.isVariableDeclaration(path.node.declaration)) {
|
2019-09-24 10:50:04 +02:00
|
|
|
return
|
|
|
|
}
|
2020-02-13 05:39:51 +01:00
|
|
|
|
|
|
|
const { declarations } = path.node.declaration
|
|
|
|
const config: PageConfig = {}
|
|
|
|
|
2019-10-07 19:22:47 +02:00
|
|
|
for (const declaration of declarations) {
|
2020-02-13 05:39:51 +01:00
|
|
|
if (
|
|
|
|
!BabelTypes.isIdentifier(declaration.id, { name: 'config' })
|
|
|
|
) {
|
2019-09-24 10:50:04 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2020-02-13 05:39:51 +01:00
|
|
|
if (!BabelTypes.isObjectExpression(declaration.init)) {
|
|
|
|
const got = declaration.init
|
|
|
|
? declaration.init.type
|
|
|
|
: 'undefined'
|
2019-07-15 22:54:35 +02:00
|
|
|
throw new Error(
|
2020-02-13 05:39:51 +01:00
|
|
|
errorMessage(state, `Expected object but got ${got}`)
|
2019-07-15 22:54:35 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:16:14 +02:00
|
|
|
for (const prop of declaration.init.properties) {
|
2020-02-13 05:39:51 +01:00
|
|
|
if (BabelTypes.isSpreadElement(prop)) {
|
|
|
|
throw new Error(
|
|
|
|
errorMessage(state, `Property spread is not allowed`)
|
|
|
|
)
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
const { name } = prop.key
|
2020-02-13 05:39:51 +01:00
|
|
|
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']
|
2019-07-01 23:13:52 +02:00
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.amp === true) {
|
2020-01-08 17:30:53 +01:00
|
|
|
if (!state.file?.opts?.caller.isDev) {
|
2019-12-16 18:45:48 +01:00
|
|
|
// don't replace bundle in development so HMR can track
|
|
|
|
// dependencies and trigger reload when they are changed
|
|
|
|
replaceBundle(path, t)
|
|
|
|
}
|
2019-07-01 23:13:52 +02:00
|
|
|
state.bundleDropped = true
|
|
|
|
return
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
state
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|