Giter Site home page Giter Site logo

probablyup / babel-plugin-react-displayname Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zendesk/babel-plugin-react-displayname

1.0 1.0 1.0 221 KB

Babel plugin to generate React components' displayName while preserving tree-shaking

License: Apache License 2.0

JavaScript 100.00%

babel-plugin-react-displayname's Introduction

@probablyup/babel-plugin-react-displayname

Jest test CI

Automatically generate display names for React components, while retaining tree-shaking support in bundlers.

Setup

  1. Install the dependency
yarn add --dev @probablyup/babel-plugin-react-displayname
  1. Add the plugin to your babel configuration
{
  "plugins": ["@probablyup/babel-plugin-react-displayname"]
}

Why use this?

React dev tools infer component names from the name of the function or class that defines the component. However, it does not work when anonymous functions are used.

This plugin fixes that by automatically generating the displayName property for assigned anonymous functions.

What does it do?

This plugin converts the following:

const Linebreak = React.memo(() => {
    return <br />;
});

const Img = function () {
    return <img />;
}

into:

const Linebreak = React.memo(function _Linebreak() {
  return <br />;
});
Linebreak.displayName = "Linebreak";

const Img = function () {
  return <img />;
};
Img.displayName = "Img";

Options

allowedCallees

Object.<string, string[]>, defaults to { "react": ["createContext"] }

Enables generation of displayNames for certain called functions.

{
  "plugins": ["@probablyup/babel-plugin-react-displayname", {
    "allowedCallees": {
      "react": ["createComponent"]
    }
  }]
}

Example

By default, with allowedCallees set to { "react": ["createContext"] }:

import React, { createContext } from 'react';
const FeatureContext = createContext();
const AnotherContext = React.createContext();

is transformed into:

import React, { createContext } from 'react';
const FeatureContext = createContext();
FeatureContext.displayName = "FeatureContext";
const AnotherContext = React.createContext();
AnotherContext.displayName = "AnotherContext";

template

Allows for rudimentary templating with the generated displayName. For example:

{
  "plugins": ["@probablyup/babel-plugin-react-displayname", {
    "template": "DS.%s",
  }]
}

Example

from:

const Linebreak = React.memo(() => {
    return <br />;
});

const Img = function () {
    return <img />;
}

to:

const Linebreak = React.memo(function _Linebreak() {
  return <br />;
});
Linebreak.displayName = "DS.Linebreak";

const Img = function () {
  return <img />;
};
Img.displayName = "DS.Img";

babel-plugin-react-displayname's People

Contributors

li-kai avatar quantizor avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

mortargrind

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.