2019-11-21 03:53:12 +01:00
|
|
|
import { NodePath, PluginObj } from '@babel/core'
|
2019-06-10 02:16:14 +02:00
|
|
|
import * as BabelTypes from '@babel/types'
|
2020-01-25 05:25:11 +01:00
|
|
|
import { PageConfig } from 'next/types'
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
const configKeys = new Set(['amp'])
|
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
|
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-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
|
|
|
)
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:43:11 +01:00
|
|
|
const { declarations } = path.node.declaration as any
|
2019-10-07 19:22:47 +02:00
|
|
|
const config: PageConfig = {}
|
2019-06-10 02:16:14 +02:00
|
|
|
|
2019-09-24 10:50:04 +02:00
|
|
|
if (!declarations) {
|
|
|
|
return
|
|
|
|
}
|
2019-10-07 19:22:47 +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(
|
2019-11-11 04:24:53 +01:00
|
|
|
`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-07-15 22:54:35 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|