2019-11-21 03:53:12 +01:00
|
|
|
import { NodePath, PluginObj } from '@babel/core'
|
2019-02-28 23:04:29 +01:00
|
|
|
import { JSXOpeningElement } from '@babel/types'
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|