2.1.0 • Published 3 years ago

babel-plugin-function-injection v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

🏠 Homepage

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