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-09-04 16:00:54 +02:00
|
|
|
import { PageConfig } from '../../../types'
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__'
|
2019-08-12 03:56:57 +02:00
|
|
|
export const sprStatus = { used: false }
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
const configKeys = new Set(['amp'])
|
2019-08-06 22:26:01 +02:00
|
|
|
const pageComponentVar = '__NEXT_COMP'
|
2019-08-12 03:56:57 +02:00
|
|
|
// this value can't be optimized by terser so the shorter the better
|
|
|
|
const prerenderId = '__NEXT_SPR'
|
2019-09-24 10:50:04 +02:00
|
|
|
const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps'
|
|
|
|
const EXPORT_NAME_GET_STATIC_PARAMS = 'unstable_getStaticParams'
|
2019-08-06 22:26:01 +02:00
|
|
|
|
|
|
|
// replace program path with just a variable with the drop identifier
|
2019-07-01 23:13:52 +02:00
|
|
|
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 {
|
2019-08-06 22:26:01 +02:00
|
|
|
isPrerender?: boolean
|
2019-07-01 23:13:52 +02:00
|
|
|
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-09-24 10:50:04 +02:00
|
|
|
if (state.bundleDropped || !path.node.declaration) {
|
2019-06-10 02:16:14 +02:00
|
|
|
return
|
2019-09-24 10:50:04 +02:00
|
|
|
}
|
|
|
|
const { declarations, id } = path.node.declaration as any
|
2019-06-10 02:16:14 +02:00
|
|
|
const config: PageConfig = {}
|
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
// drop SSR Exports for client bundles
|
|
|
|
if (
|
|
|
|
id &&
|
|
|
|
(id.name === EXPORT_NAME_GET_STATIC_PROPS ||
|
|
|
|
id.name === EXPORT_NAME_GET_STATIC_PARAMS)
|
|
|
|
) {
|
|
|
|
if (id.name === EXPORT_NAME_GET_STATIC_PROPS) {
|
|
|
|
state.isPrerender = true
|
|
|
|
sprStatus.used = true
|
|
|
|
}
|
|
|
|
path.remove()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!declarations) {
|
|
|
|
return
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
for (const declaration of declarations) {
|
2019-09-24 10:50:04 +02:00
|
|
|
if (declaration.id.name !== 'config') {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
|
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
|
|
|
|
}
|
2019-06-10 02:16:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
state
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
2019-08-06 22:26:01 +02:00
|
|
|
ExportDefaultDeclaration(path, state: ConfigState) {
|
|
|
|
if (!state.isPrerender) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const prev = t.cloneDeep(path.node.declaration)
|
|
|
|
|
|
|
|
// workaround to allow assigning a ClassDeclaration to a variable
|
|
|
|
// babel throws error without
|
|
|
|
if (prev.type.endsWith('Declaration')) {
|
|
|
|
prev.type = prev.type.replace(/Declaration$/, 'Expression') as any
|
|
|
|
}
|
|
|
|
|
|
|
|
path.insertBefore([
|
|
|
|
t.variableDeclaration('const', [
|
|
|
|
t.variableDeclarator(t.identifier(pageComponentVar), prev as any),
|
|
|
|
]),
|
|
|
|
t.assignmentExpression(
|
|
|
|
'=',
|
|
|
|
t.memberExpression(
|
|
|
|
t.identifier(pageComponentVar),
|
|
|
|
t.identifier(prerenderId)
|
|
|
|
),
|
|
|
|
t.booleanLiteral(true)
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
|
|
|
path.node.declaration = t.identifier(pageComponentVar)
|
|
|
|
},
|
2019-06-10 02:16:14 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|