All Projects → esamattis → babel-plugin-display-name-custom

esamattis / babel-plugin-display-name-custom

Licence: MIT License
display name inference for your custom react component creators

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
HTML
75241 projects

Projects that are alternatives of or similar to babel-plugin-display-name-custom

babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (+193.33%)
Mutual labels:  babel-plugin
babel-plugin-react-scope-binding
🍖 Babel plugin for React component to take event handler to bind context automatically.
Stars: ✭ 21 (+40%)
Mutual labels:  babel-plugin
core-web
like core-js but for Web APIs (based on polyfill.io)
Stars: ✭ 34 (+126.67%)
Mutual labels:  babel-plugin
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (+73.33%)
Mutual labels:  babel-plugin
babel-plugin-inline-svg
Babel plugin to optimise and inline svg
Stars: ✭ 50 (+233.33%)
Mutual labels:  babel-plugin
babel-plugin-loop-optimizer
Optimizes statements such as `forEach` and `map` to for loops.
Stars: ✭ 70 (+366.67%)
Mutual labels:  babel-plugin
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (+153.33%)
Mutual labels:  babel-plugin
babel-plugin-transform-replace-expressions
A Babel plugin for replacing expressions with other expressions
Stars: ✭ 23 (+53.33%)
Mutual labels:  babel-plugin
babel-plugin-flow-relay-query
Babel plugin which converts Flow types into Relay fragments
Stars: ✭ 38 (+153.33%)
Mutual labels:  babel-plugin
babel-plugin-proposal-pattern-matching
the minimal grammar, high performance JavaScript pattern matching implementation
Stars: ✭ 34 (+126.67%)
Mutual labels:  babel-plugin
babel-plugin-object-styles-to-template
Babel plugin to transpile object styles to template literal
Stars: ✭ 33 (+120%)
Mutual labels:  babel-plugin
babel-plugin-transform-hoist-nested-functions
Babel plugin to move nested functions to outer scopes
Stars: ✭ 32 (+113.33%)
Mutual labels:  babel-plugin
flair
a lean, component-centric style system for React components
Stars: ✭ 19 (+26.67%)
Mutual labels:  babel-plugin
babel-plugin-transform-react-qa-classes
Add component's name in `data-qa` attributes to React Components
Stars: ✭ 41 (+173.33%)
Mutual labels:  babel-plugin
babel-plugin-transform-stitches-display-name
www.npmjs.com/package/babel-plugin-transform-stitches-display-name
Stars: ✭ 12 (-20%)
Mutual labels:  babel-plugin
babel-plugin-react-directives
A babel plugin that provides some directives for react(JSX), similar to directives of vue.
Stars: ✭ 80 (+433.33%)
Mutual labels:  babel-plugin
babel-plugin-transform-export-default-name
Babel plugin that transforms default exports to named exports.
Stars: ✭ 20 (+33.33%)
Mutual labels:  babel-plugin
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (+46.67%)
Mutual labels:  babel-plugin
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (+173.33%)
Mutual labels:  babel-plugin
babel-plugin-transform-pipeline
Compile pipeline operator to ES5
Stars: ✭ 58 (+286.67%)
Mutual labels:  babel-plugin

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>
);

For background read this https://medium.com/@esamatti/improving-component-names-in-react-developer-tools-4894247510c5#.tuw8mnbjj

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].