2020-03-28 20:42:35 +01:00
|
|
|
import { NodePath, PluginObj } from 'next/dist/compiled/babel--core'
|
|
|
|
import { JSXOpeningElement } from 'next/dist/compiled/babel--types'
|
2019-02-28 23:04:29 +01:00
|
|
|
|
|
|
|
export default function AmpAttributePatcher(...args: any): PluginObj {
|
|
|
|
return {
|
|
|
|
visitor: {
|
|
|
|
JSXOpeningElement(path: NodePath<JSXOpeningElement>) {
|
|
|
|
const openingElement = path.node
|
|
|
|
|
|
|
|
const { name, attributes } = openingElement
|
|
|
|
if (!(name && name.type === 'JSXIdentifier')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name.name.startsWith('amp-')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const attribute of attributes) {
|
|
|
|
if (attribute.type !== 'JSXAttribute') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.name.name === 'className') {
|
|
|
|
attribute.name.name = 'class'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|