rsnext/packages/next/build/babel/plugins/amp-attributes.ts
Joe Haddad 330bedacc7 Add Babel plugin that fixes className for Amp elements (#6486)
This is a simple Babel plugin that overwrites certain attributes for `amp-` elements.
2019-02-28 23:04:29 +01:00

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'
}
}
},
},
}
}