npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-display-name-custom

v0.1.3

Published

Display name inference for your custom React component creators.

Downloads

29

Readme

babel-plugin-display-name-custom

display name inference for your custom react component creators

so that instead of

unknown

you could see

unknown

in React Developer Tools for code like

import {createComponent} from "./create";

const Container = createComponent("div", {
    border: "1px solid black",
});

const Red = createComponent("div", {
    color: "red",
});

const Green = createComponent("div", {
    color: "green",
});

const Blue = createComponent("div", {
    color: "blue",
});

const Root = () => (
    <Container>
        <Red>red</Red>
        <Green>green</Green>
        <Blue>blue</Blue>
    </Container>
);

usage

install

yarn add babel-plugin-display-name-custom

add display-name-custom to .babelrc

{
    "presets": ["es2015", "react"],
    "plugins": [
        "syntax-object-rest-spread",
        "transform-object-rest-spread",
        ["display-name-custom", {
            "modules": {
                "./create": {
                    "createComponent": true
                }
            }
        }]
    ]
}

The modules object is a mapping of module names to the exported functions which return new React components.

In the above example ./create is

export const createComponent = (Component, styles) => {
    return props => <Component {...props} style={styles} />;
};

You can also add inference to 3rd party modules

{
    "modules": {
        "react-redux": {
            "connect": true
        }
    }
}

The default export can be added using the default keyword

{
    "modules": {
        "create-component": {
            "default": true
        }
    }
}

how it works?

After the plugin is configured it knows which functions return new React components. Using that information it scans your source code for variable declarations which are initialized using those. When a declaration is found it sets the displayName property of the component to the variable name.

In practice it transpiles

const Red = createComponent("div", {
    color: "red",
});

to

const Red = createComponent("div", {
    color: "red",
});
Red.displayName = "Red";

library author?

For library authors the plugin also exports a createPlugin factory for generating preconfigured library specific display name plugins.

Just add babel.js to the root of your npm module with contents like

module.exports = require("babel-plugin-display-name-custom").createPlugin({
    modules: {
        "mylib": {createComponent: true},
    },
});

and your users then can use it with just

{
    "presets": ["es2015", "react"],
    "plugins": [
        "mylib/babel"
    ]
}

Checkout react-simple for a real world example