Skip to content

abskrj/babel-plugin-function-injection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-function-injection 👋

Version Documentation Maintenance License: MIT Twitter: abhishekraj272

A Babel plugin to inject and wrap a function on an exported object

Prerequisites

  • npm >=1.0.0
  • node >=1.0.0

Install

yarn add -D babel-plugin-function-injection

--OR--

npm i -D babel-plugin-function-injection

Usage

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;
}

Example

// 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);

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Abhishek Raj.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator