shawnxusy / babel-plugin-react-generate-property Goto Github PK
View Code? Open in Web Editor NEWA plugin to automatically generate properties (for example data attributes) for all JSX open tags, using user specified convention
A plugin to automatically generate properties (for example data attributes) for all JSX open tags, using user specified convention
Trying to use in webpack but not seeing any effect - does this look corrrect?
"use strict";
/* eslint-disable no-unused-vars */
module.exports = (config, webpack) => {
config.module.rules.push({
test: /\.(?:js|jsx|tsx|cjs)$/,
include: [
/node_modules\/@strapi\/admin\/admin\/src\/components/,
/node_modules\/@strapi\/admin\/admin\/src\/content-manager\/components/,
/node_modules\/@strapi\/design-system\/dist/
],
use: {
loader: require.resolve("babel-loader"),
options: {
cacheDirectory: false,
cacheCompression: false,
compact: false,
presets: [
require.resolve("@babel/preset-env"),
require.resolve("@babel/preset-react")
],
plugins: [
[
"babel-plugin-react-generate-property",
{
"customProperty": "data-dev",
"dirLevel": 4,
"slashChar": "\\",
"addModuleClassNames": true,
"prefix": "myPrefix",
"firstChildOnly": false
}
]
]
}
}
});
return config;
};
Great plugin, thanks!
A nice improvement would be an option for specifying the separator character 🙏. I think it might be common for others to not want to use the hardcoded _
.
Hey @shawnxusy i tried to utilise this project and encountered some issue we are using design system pattern and we reuse our react components that written with styled-components which means that we have one Button component in our bank with different properties so the problem is when we have couple of Buttons on same view the they have same generated attribute so it is hard for automation tests to select button that we need.
To illustrate the issue
we have Button component that comes from some library and we have couple different variant of buttons on same page
so we will the plugin will add same attribute based on the absolute path of this button so all the buttons will have something like data-id="src/componentsLibrary/differentComponents/button"
even through inside the view all the buttons will have different callbacks and strings the will have same data-id so in addition to the issue i will open pull-request with fix for this along with some other minor changes
Hi, thanks for the plugin.
I checked the source code and I think it is useful that the plugin checks if a JSX property exists. If so, plugin doesn't add the property with generated value. That however doesn't work with spreaded props.
<div {...props} />
becomes
<div {...props} xyzProp="XYZ"/>
and that effectively overwrites xyzProp
contained in props
My current patched solution is to change .push
to .unshift
in https://github.com/shawnxusy/babel-plugin-react-generate-property/blob/master/src/index.js#L123
Hence the example above becomes
<div xyzProp="XYZ" {...props} />
I'm fine with creating a PR. Let me know if you want me to create one with some specifics like if there should be a new option etc.
Hey @shawnxusy thanks for this useful library!
Our library has various depths of paths, for example you have:
src/App.tsx
or
src/libs/mylib/components/component1/Component1.tsx
Problem with the dirLevel
path is that it strips the directory, so you get:
src_app
for src/App.tsx
and
component1_component1
for src/libs/mylib/components/component1/Component1.tsx
When you nest components deeper in your file hierarchy you start missing some information.
Would it be an idea to add an option something like stripDirs
which takes off the first x amount of directories?
Hi.
Thanks for what you doing.
Please, update npm version to the latest.
I wish to use new configs like ‘addModuleClassnames’ and ‘ignoreNodeNames’
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.