39761e5ce7
* Remove `withAmp` and do not expose `isInAmpMode`
This pull request removes the `withAmp` higher-order component in favor of the exported configuration object.
Retaining this backwards compatibility isn't worth the extra maintenance burden, given it hasn't been released for very long and the migration is ultra-simple.
I'm 👎 on retaining backwards compatibility just for the sake of it.
Migration example follows. I'm willing to write a codemod if reviewers feel it's necessary.
*Before*
```js
import { withAmp } from 'next/amp'
function Home() {
return <h1>My AMP Page</h1>
}
export default withAmp(Home)
// or
export default withAmp(Home, { hybrid: true })
```
*After*
```js
export default function Home() {
return <h1>My AMP Page</h1>
}
export const config = {
amp: true,
// or
amp: 'hybrid',
}
```
* Fix { amp: 'hybrid' }
Co-Authored-By: JJ Kasper <jj@jjsweb.site>
77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import { PluginObj } from '@babel/core'
|
|
import { NodePath } from '@babel/traverse'
|
|
import * as BabelTypes from '@babel/types'
|
|
|
|
interface PageConfig {
|
|
amp?: boolean | 'hybrid'
|
|
}
|
|
|
|
function replacePath(path: any, t: typeof BabelTypes) {
|
|
path.parentPath.replaceWith(
|
|
t.program(
|
|
[
|
|
t.variableDeclaration('const', [
|
|
t.variableDeclarator(
|
|
t.identifier('config'),
|
|
t.assignmentExpression(
|
|
'=',
|
|
t.identifier('no'), // this can't be empty but is required
|
|
t.stringLiteral(`__NEXT_DROP_CLIENT_FILE__ ${Date.now()}`)
|
|
)
|
|
),
|
|
]),
|
|
],
|
|
[]
|
|
)
|
|
)
|
|
}
|
|
|
|
export default function nextPageConfig({
|
|
types: t,
|
|
}: {
|
|
types: typeof BabelTypes
|
|
}): PluginObj<{
|
|
insertedDrop?: boolean
|
|
hasAmp?: boolean
|
|
}> {
|
|
return {
|
|
visitor: {
|
|
Program: {
|
|
enter(path, state: any) {
|
|
path.traverse(
|
|
{
|
|
ExportNamedDeclaration(
|
|
path: NodePath<BabelTypes.ExportNamedDeclaration>,
|
|
state: any
|
|
) {
|
|
if (
|
|
state.replaced ||
|
|
!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
|
|
|
|
for (const prop of declaration.init.properties) {
|
|
const { name } = prop.key
|
|
if (name === 'amp') config.amp = prop.value.value
|
|
}
|
|
}
|
|
|
|
if (config.amp === true) {
|
|
replacePath(path, t)
|
|
state.replaced = true
|
|
}
|
|
},
|
|
},
|
|
state
|
|
)
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|