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-preset-react-optimised

v1.0.0

Published

Replaces `React.createElement` and `React.createFragment` to a minifier-friendly variable to squeeze the extra bytes out of your [React](http://reactjs.org/) code.

Downloads

4

Readme

babel-preset-react-optimised

Replaces React.createElement and React.createFragment to a minifier-friendly variable to squeeze the extra bytes out of your React code.

Example

Input

From the create-react-app template.

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

Before (521 bytes minified)

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return React.createElement(
    'div',
    {
      className: 'App',
    },
    React.createElement(
      'header',
      {
        className: 'App-header',
      },
      React.createElement('img', {
        src: logo,
        className: 'App-logo',
        alt: 'logo',
      }),
      React.createElement(
        'p',
        null,
        'Edit ',
        React.createElement('code', null, 'src/App.js'),
        ' and save to reload.'
      ),
      React.createElement(
        'a',
        {
          className: 'App-link',
          href: 'https://reactjs.org',
          target: '_blank',
          rel: 'noopener noreferrer',
        },
        'Learn React'
      )
    )
  );
}

export default App;

After (469 bytes minified, 9.98% reduction)

import React from 'react';

/*Injected by babel-preset-react-optimised*/
const __jsx = React.createElement;
import logo from './logo.svg';
import './App.css';

function App() {
  return __jsx(
    'div',
    {
      className: 'App',
    },
    __jsx(
      'header',
      {
        className: 'App-header',
      },
      __jsx('img', {
        src: logo,
        className: 'App-logo',
        alt: 'logo',
      }),
      __jsx(
        'p',
        null,
        'Edit ',
        __jsx('code', null, 'src/App.js'),
        ' and save to reload.'
      ),
      __jsx(
        'a',
        {
          className: 'App-link',
          href: 'https://reactjs.org',
          target: '_blank',
          rel: 'noopener noreferrer',
        },
        'Learn React'
      )
    )
  );
}

export default App;

Usage

To install:

# using npm
npm install --save-dev babel-preset-react-optimised
# using yarn
yarn add --dev babel-preset-react-optimised

babel-plugin-preset-react-optimised is a drop-in replacement of @babel/babel-preset-react

{
-  "presets": ["@babel/preset-react"],
+  "presets": ["babel-preset-react-optimised"],
 "env": {
    "development": {
-      "presets": [["@babel/preset-react", {
+      "presets": [["babel-preset-react-optimised", {
          "development": true
        }]]
    }
  }
}

The preset will inject

  • const __jsx = React.createElement and
  • const __jsxFrag = React.Fragment

if used in the code, and replace all the usages of React.createElement -> __jsx and React.Fragment -> __jsxFrag respectively.

Therefore, __jsx and __jsxFrag is a reserved variable name, and can't be used in anywhere of the code.

You can rename the variable __jsx and __jsxFrag via the pragmaAs and pragmaFragAs options to avoid conflict.

Options

pragma

string, defaults to React.createElement.

Replace the function used when compiling JSX expressions.

See @babel/preset-react

pragmaFrag

string, defaults to React.Fragment.

Replace the component used when compiling JSX fragments.

See @babel/preset-react

useBuiltIns

boolean, defaults to false.

Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.

See @babel/preset-react

useSpread

boolean, defaults to false.

When spreading props, use inline object with spread elements directly instead of Babel's extend helper or Object.assign.

See @babel/preset-react

development

boolean, defaults to false.

Toggles plugins that aid in development, such as @babel/plugin-transform-react-jsx-self and @babel/plugin-transform-react-jsx-source.

This is useful when combined with the env option configuration or js config files.

See @babel/preset-react

throwIfNamespace

boolean, defaults to true.

Toggles whether or not to throw an error if a XML namespaced tag name is used. For example:

<f:image />

Though the JSX spec allows this, it is disabled by default since React's JSX does not currently have support for it.

See @babel/preset-react

pragmaAs

string, defaults to __jsx

Replace the variable used to replace the pragma function

pragmaFragAs

string, defaults to __jsxFrag

Replace the variable used to replace the pragmaFrag function

.babelrc

{
  "presets": ["babel-preset-react-optimised"]
}

You can read more about configuring preset options here

License

MIT