GithubHelp home page GithubHelp logo

preactjs / babel-plugin-transform-hook-names Goto Github PK

View Code? Open in Web Editor NEW
8.0 5.0 2.0 256 KB

Add custom hook names for devtools

License: MIT License

JavaScript 28.54% TypeScript 71.46%
preact devtools babel

babel-plugin-transform-hook-names's Introduction

babel-plugin-transform-hook-names

A babel plugin to automatically infer hook names from your code and show them in the Preact Devtools extension.

Before:

Screenshot of Preact devtools without this plugin

After:

Screenshot of Preact devtools with this plugin

Requires: Babel >= 7.12

Usage

Install babel-plugin-transform-hook-names in your project:

npm install --save-dev babel-plugin-transform-hook-names
# or via yarn
yarn add -D babel-plugin-transform-hook-names

Then add it to your babel configuration:

{
	"plugins": ["babel-plugin-transform-hook-names"]
}

How does it work?

The way it works is that each hook is wrapped with a function that is passed the same name as the variable:

Input:

// Works for "preact/compat" or "react" too
import { useState, useReducer, useRef } from "preact/hooks";

function Foo() {
	const [text, setText] = useState("hello");
	const [counter, increment] = useReducer(c => c + 1, 0);
	const rootElement = useRef();
	const memo = useMemo(() => text.toUpperCase(), ["text"]);
}

Output:

import { addHookName } from "preact/devtools";
import { useState, useReducer, useRef } from "preact/hooks";

function Foo() {
	const [text, setText] = addHookName(useState("hello"), "text");
	const [counter, increment] = addHookName(
		useReducer(c => c + 1, 0),
		"counter",
	);
	const rootElement = addHookName(useRef(), "rootElement");
	const memo = addHookName(
		useMemo(() => text.toUpperCase(), ["text"]),
		"memo",
	);
}

License

MIT, see the LICENSE file

babel-plugin-transform-hook-names's People

Contributors

cmlenz avatar marvinhagemeister avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

babel-plugin-transform-hook-names's Issues

Build error caused by babel version incompatibility

I added transform-hook-names to my project to try it for the first time, and got the following error during the build.

/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/src/view/utils/hooks/dom.ts: /Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/src/view/utils/hooks/dom.ts: this.input.charCodeAt is not a function
    at Parser.skipSpace (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babylon/lib/index.js:838:27)
    at Parser.nextToken (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babylon/lib/index.js:755:56)
    at Parser.parse (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babylon/lib/index.js:1672:10)
    at Object.parse (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babylon/lib/index.js:7305:37)
    at getAst (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-template/lib/index.js:29:21)
    at /Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-template/lib/index.js:53:24
    at PluginPass.CallExpression (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-plugin-transform-hook-names/src/index.js:32:6)
    at newFn (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-traverse/lib/path/context.js:48:17)from
    at exports.default (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-template/lib/index.js:12:11)
    at module.exports (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-plugin-transform-hook-names/src/index.js:3:25)
    at Function.memoisePluginContainer (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)
    at Function.normalisePlugin (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
    at /Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
    at Array.map (<anonymous>)
    at Function.normalisePlugins (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
    at OptionManager.mergeOptions (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
    at OptionManager.init (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/EGottlieb/Documents/thrive-2.0/web/packages/ui/node_modules/babel-core/lib/transformation/file/index.js:212:65)

I found that updating babel from [email protected] to @babel/[email protected] (and [email protected] to @babel/[email protected]) made the error go away.

It would be good to have a minimum babel version for this plugin in the README. Thanks!

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.