2020-11-06 03:33:14 +01:00
|
|
|
import { NodePath, PluginObj, types } from 'next/dist/compiled/babel/core'
|
2019-02-28 23:04:29 +01:00
|
|
|
|
2020-05-21 14:57:04 +02:00
|
|
|
export default function AmpAttributePatcher(): PluginObj {
|
2019-02-28 23:04:29 +01:00
|
|
|
return {
|
|
|
|
visitor: {
|
2020-04-22 07:25:43 +02:00
|
|
|
JSXOpeningElement(path: NodePath<types.JSXOpeningElement>) {
|
2019-02-28 23:04:29 +01:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|