A Babel plugin to inject and wrap a function on an exported object
🏠 Homepage
- npm >=1.0.0
- node >=1.0.0
yarn add -D babel-plugin-function-injection
--OR--
npm i -D babel-plugin-function-injection
You need to pass 2 keys to the plugin options.
{
imports: IImport[],
target: RegEx // Files need to be injected
}
interface IImport {
name: string;
lib: string;
defaultImport: boolean;
}
// Plugin
module.exports = function (api) {
api.cache(true);
const plugins = [
[
"babel-plugin-function-injection",
{
imports: [
{
name: "connect",
lib: "react-redux",
defaultImport: false,
},
{
name: "mapStateToProps",
lib: "@utils/stateToProps",
defaultImport: true,
},
],
target: /(.*components.*index.[js|mjs|jsx|ts|tsx]*)/,
},
],
];
return {
plugins,
};
};
// Result
// File: components/index.js
// Input --> to plugin
export { default } from './Navbar';
----
export default Navbar;
// Output <-- from plugin
import { connect } from "react-redux";
import mapStateToProps from "@utils/stateToProps";
export const Something = connect(mapStateToProps)(Component);
----
export default connect(mapStateToProps)(Navbar);
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Give a ⭐️ if this project helped you!
Copyright © 2021 Abhishek Raj.
This project is MIT licensed.
This README was generated with ❤️ by readme-md-generator