330bedacc7
This is a simple Babel plugin that overwrites certain attributes for `amp-` elements.
32 lines
812 B
TypeScript
32 lines
812 B
TypeScript
import { PluginObj } from '@babel/core'
|
|
import { NodePath } from '@babel/traverse'
|
|
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'
|
|
}
|
|
}
|
|
},
|
|
},
|
|
}
|
|
}
|