Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/abskrj/babel-plugin-function-injection


https://github.com/abskrj/babel-plugin-function-injection

babel hacktoberfest hacktoberfest-accepted hacktoberfest2021 js plugin

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

babel-plugin-function-injection 👋




Version




Documentation


Maintenance


License: MIT


Twitter: abhishekraj272

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

### 🏠 [Homepage](https://github.com/abhishekraj272/babel-plugin-function-injection#readme)

## Prerequisites

- npm >=1.0.0
- node >=1.0.0

## Install

```sh
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.

```js
{
imports: IImport[],
target: RegEx // Files need to be injected
}

interface IImport {
name: string;
lib: string;
defaultImport: boolean;
}
```

## Example

```js
// 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](https://github.com/abhishekraj272/babel-plugin-function-injection/issues). 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](https://github.com/abhishekraj272).

This project is [MIT](https://github.com/abhishekraj272/babel-plugin-function-injection/blob/master/LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_