2019-06-10 02:16:14 +02:00
|
|
|
import { PluginObj } from '@babel/core'
|
|
|
|
import { NodePath } from '@babel/traverse'
|
|
|
|
import * as BabelTypes from '@babel/types'
|
2019-07-01 23:13:52 +02:00
|
|
|
import { PageConfig } from 'next-server/types'
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
const configKeys = new Set(['amp', 'experimentalPrerender'])
|
|
|
|
export const inlineGipIdentifier = '__NEXT_GIP_INLINE__'
|
|
|
|
export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__'
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
// replace progam path with just a variable with the drop identifier
|
|
|
|
function replaceBundle(path: any, t: typeof BabelTypes) {
|
2019-06-10 02:16:14 +02:00
|
|
|
path.parentPath.replaceWith(
|
|
|
|
t.program(
|
|
|
|
[
|
|
|
|
t.variableDeclaration('const', [
|
|
|
|
t.variableDeclarator(
|
|
|
|
t.identifier('config'),
|
|
|
|
t.assignmentExpression(
|
|
|
|
'=',
|
2019-07-01 23:13:52 +02:00
|
|
|
t.identifier(dropBundleIdentifier),
|
|
|
|
t.stringLiteral(`${dropBundleIdentifier} ${Date.now()}`)
|
2019-06-10 02:16:14 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
interface ConfigState {
|
|
|
|
setupInlining?: boolean
|
|
|
|
bundleDropped?: boolean
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
if (
|
2019-07-01 23:13:52 +02:00
|
|
|
state.bundleDropped ||
|
2019-06-10 02:16:14 +02:00
|
|
|
!path.node.declaration ||
|
|
|
|
!(path.node.declaration as any).declarations
|
|
|
|
)
|
|
|
|
return
|
|
|
|
const { declarations } = path.node.declaration as any
|
|
|
|
const config: PageConfig = {}
|
|
|
|
|
|
|
|
for (const declaration of declarations) {
|
|
|
|
if (declaration.id.name !== 'config') continue
|
|
|
|
|
2019-07-15 22:54:35 +02:00
|
|
|
if (declaration.init.type !== 'ObjectExpression') {
|
|
|
|
const pageName =
|
|
|
|
(state.filename || '').split(state.cwd || '').pop() ||
|
|
|
|
'unknown'
|
|
|
|
|
|
|
|
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`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:16:14 +02:00
|
|
|
for (const prop of declaration.init.properties) {
|
|
|
|
const { name } = prop.key
|
2019-07-01 23:13:52 +02:00
|
|
|
if (configKeys.has(name)) {
|
|
|
|
// @ts-ignore
|
|
|
|
config[name] = prop.value.value
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.amp === true) {
|
2019-07-01 23:13:52 +02:00
|
|
|
replaceBundle(path, t)
|
|
|
|
state.bundleDropped = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
config.experimentalPrerender === true ||
|
|
|
|
config.experimentalPrerender === 'inline'
|
|
|
|
) {
|
|
|
|
state.setupInlining = true
|
2019-06-10 02:16:14 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
state
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
2019-07-01 23:13:52 +02:00
|
|
|
// handles Page.getInitialProps = () => {}
|
|
|
|
AssignmentExpression(path, state: ConfigState) {
|
|
|
|
if (!state.setupInlining) return
|
|
|
|
const { property } = (path.node.left || {}) as any
|
|
|
|
const { name } = property
|
|
|
|
if (name !== 'getInitialProps') return
|
|
|
|
// replace the getInitialProps function with an identifier for replacing
|
|
|
|
path.node.right = t.functionExpression(
|
|
|
|
null,
|
|
|
|
[],
|
|
|
|
t.blockStatement([
|
|
|
|
t.returnStatement(t.stringLiteral(inlineGipIdentifier)),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
},
|
|
|
|
// handles class { static async getInitialProps() {} }
|
|
|
|
FunctionDeclaration(path, state: ConfigState) {
|
|
|
|
if (!state.setupInlining) return
|
|
|
|
if ((path.node.id && path.node.id.name) !== 'getInitialProps') return
|
|
|
|
path.node.body = t.blockStatement([
|
|
|
|
t.returnStatement(t.stringLiteral(inlineGipIdentifier)),
|
|
|
|
])
|
|
|
|
},
|
2019-07-25 04:16:32 +02:00
|
|
|
// handles class { static async getInitialProps() {} }
|
|
|
|
ClassMethod(path, state: ConfigState) {
|
|
|
|
if (!state.setupInlining) return
|
|
|
|
if (
|
|
|
|
(path.node.key && (path.node.key as BabelTypes.Identifier).name) !==
|
|
|
|
'getInitialProps'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
path.node.body = t.blockStatement([
|
|
|
|
t.returnStatement(t.stringLiteral(inlineGipIdentifier)),
|
|
|
|
])
|
|
|
|
},
|
2019-06-10 02:16:14 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|