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-hoist-constant-jsx-attributes

v1.0.0

Published

Prevents unnecessary rerenders and GC pressure by hoisting constant objects out of JSX attributes and into the top-level scope, ex. for objects in a 'style' prop.

Downloads

6

Readme

babel-plugin-hoist-constant-jsx-attributes

Version NPM Version License Build Status Supported Node Version

This plugin can reduce React rerenders and garbage collection pressure by pulling constant object attribute values (like style={{ color: 'red' }}) out of functions and into the highest possible scope. This means the objects will only get allocated once, and they will keep the same identity across renders.

Objects are only pulled out if they are plain old data and don't reference any variables, so more dynamic use cases will still work as written.

Example

In

const Component = () => <div style={{ color: 'red', fontSize: 14 }}>Text</div>;

Out

const _style = { color: 'red', fontSize: 14 };
const Component = () => <div style={_style}>Text</div>;

Install

# npm
npm i --save-dev babel-plugin-hoist-constant-jsx-attributes
# yarn
yarn add --dev babel-plugin-hoist-constant-jsx-attributes
{
  "plugins": [
    ["babel-plugin-hoist-constant-jsx-attributes", { /* options (see below) */ }]
  ]
}

Options

include

RegExp, or string to be matched with micromatch

Only hoist attribute values if the element name matches.

exclude

RegExp, or string to be matched with micromatch

Only hoist attributes values if the element name does not match.

includeAttributes

RegExp, or string to be matched with micromatch

Only hoist attribute values if the attribute name matches.

excludeAttributes

RegExp, or string to be matched with micromatch

Only hoist attributes values if the attribute name does not match.

lowerCaseOnly

boolean, defaults to false

Only hoist object attribute values on primitive elements like div and button.

alwaysHoist

boolean, defaults to false

By default, this plugin only hoists attribute values if the JSX element is contained in a function or method. This is not recommended, since otherwise the problems this plugin solves don't occur. Setting this option to true will hoist even if the JSX element is at the top level.

freezeObjects

false, 'development', or 'always', defaults to false.

If set to 'development', check at runtime to see if process.env.NODE_ENV === 'development', and if so deeply freeze hoisted objects. If set to 'always', deeply freeze hoisted objects unconditionally.

Use this option for extra safety if you want to ensure that the attribute values are never mutated. If there is an attempt to mutate a hoisted attribute value, an exception will be thrown. Slightly increases the compiled code size.

Note

The React team considers this transformation unsafe to run by default, because it is possible to rely on object attribute values to have different identities every render. See this GitHub issue for more information.

However, the vast majority of cases will benefit from this transformation. The main reason they consider it unsafe is precisely because it can reduce the number of times a component rerenders, which is likely to be what you want. Unless you were to intentionally tinker with object referential equality, or mutate received props —and you would know if you were—this transformation will be safe.

Still, to be extra careful, you can set the lowerCaseOnly plugin option to true. You can also set the freezeObjects plugin option to 'development' or 'always' to receive an error if a component tries to mutate its props.