5.0.1 • Published 8 months ago

babel-plugin-transform-reblend-function-to-class v5.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Babel Plugin For Reblendjs: Transform Functions to ES6 Classes

This Babel plugin transforms JavaScript functions containing JSX into ES6 classes, facilitating the migration from function-based components to class-based components. It handles various nuances such as function bindings and state management hooks.

Features

  • Transforms functions containing JSX into ES6 classes.
  • Binds functions and state hooks to the class instance.
  • Converts useState hooks into class properties with automatic state setters.
  • Adds a static ELEMENT_NAME property to each class, which holds the original function name.
  • Handles arrow functions and variable declarators.

Installation

To install the plugin, run:

npm install --save-dev babel-plugin-transform-functions-to-classes

Usage

Add the plugin to your Babel configuration:

{
  "plugins": ["transform-functions-to-classes"]
}

Example

Input

import { useState } from 'reblendjs';

const MyComponent = () => {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    setCount(count + 1);
  };

  return <button onClick={handleClick}>Count: {count}</button>;
};

Output

import { useState } from 'reblendjs';

class MyComponent extends Reblend {
  static ELEMENT_NAME = 'MyComponent';

  constructor() {
    super();
  }

  init() {
    let [count, setCount] = useState.bind(this)(0);
    this.count = count;
    this.setCount = this.setCount.bind(this);
    this.apply(this.setCount, 'count');
    const handleClick = () => {
      setCount(count + 1);
    };
    this.handleClick = handleClick;
    this.handleClick = this.handleClick.bind(this);
  }

  html() {
    return <button onClick={this.handleClick}>Count: {this.count}</button>;
  }
}
5.0.1

8 months ago

5.0.0

8 months ago

4.0.15

12 months ago

4.0.14

12 months ago

4.0.13

1 year ago

4.0.12

1 year ago

4.0.8

1 year ago

4.0.10

1 year ago

4.0.6

1 year ago

4.0.11

1 year ago

4.0.5

1 year ago

4.0.4

1 year ago

4.0.2

1 year ago

3.0.10

1 year ago

3.0.11

1 year ago

3.0.8

1 year ago

3.0.9

1 year ago

3.0.7

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.0

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago