babel-plugin-react-element-info
Version:
113 lines (89 loc) • 2.45 kB
Markdown
# babel-plugin-react-element-info
> Babel plugin for exposing React element name and filename in DOM nodes
[](https://www.npmjs.com/package/babel-plugin-react-element-info)
[](https://www.npmjs.com/package/babel-plugin-react-element-info)
[](https://travis-ci.org/suprraz/babel-plugin-react-element-info)
[](https://david-dm.org/suprraz/babel-plugin-react-element-info)
[](https://david-dm.org/suprraz/babel-plugin-react-element-info#info=devDependencies&view=list)
## Installation
```sh
npm install --save-dev babel-plugin-react-element-info
```
## The problem solved
This is useful for auto-generating selectors to run selenium tests.
## Example
**In**
```myInputFile.js```
```js
class Foo extends React.Component {
render() {
return (
<MyReactComponent>
My component contents
</MyReactComponent>
);
}
}
```
**Out**
```js
class Foo extends React.Component {
render() {
return (
<MyReactComponent data-qa-node="MyReactComponent" data-qa-file="myInputFile">
My component contents
</MyReactComponent>
);
}
}
```
## Usage
#### Via `.babelrc` (Recommended)
**.babelrc**
without options:
```json
{
"env": {
"development": {
"plugins": [
"react-element-info"
]
}
}
}
```
with options. Prefix is the attribute prefix, defaulting to `qa` (`data-qa-*`). To get `data-test-prefix-*` attributes, set prefix to `test-prefix`:
```json
{
"env": {
"development": {
"plugins": [
["react-element-info", {"prefix": "test-prefix"}]
]
}
}
}
```
#### Via CLI
```sh
babel --plugins react-element-info script.js
```
#### Via Node API
without options:
```js
require('babel-core').transform('code', {
plugins: [
'react-element-info',
],
});
```
with options:
```js
require('babel-core').transform('code', {
plugins: [
['react-element-info', {prefix: 'text-prefix'}],
],
});
```
## License
MIT