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

vite-plugin-react-control-statements

v1.0.1

Published

A Vite plugin for using React control statements

Downloads

590

Readme

Vite-React Control Statements

A Vite plugin for using React control statements. Inspired by traditional templating engines, jsx-control-statements, and logic blocks from Svelte.


Installation

Install (use npm, pnpm, yarn, etc.)

npm i vite-plugin-react-control-statements

Add the plugin to your vite config.

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import reactControlStatements from 'vite-plugin-react-control-statements'

export default defineConfig({
    plugins: [react(), reactControlStatements()],
})

And you're ready to use it! Just import the components you need, and use them. Take a look at some examples below.

Note: you can declare them as global types if you want to avoid importing them in your code. These imports are just for TypeScript to recognize the components and are not needed by the Vite plugin.

import { If, Choose, Otherwise, When } from "vite-plugin-react-control-statements";

Usage

Use <If>, <Choose>, <When>, and <Otherwise> components to conditionally render content. The plugin automatically converts them into traditional js control statements in your JSX or TSX code. Since it works directly on JSX and TSX files in general, you could also use it with Vue or any other libraries that use JSX/TSX.

Using code transformation with this plugin avoids some issues that arise when using actual React components for conditional rendering (see the notes at the end for more details).

If Block - Conditionally render content.

<If condition={value}>
    <p>Hello!</p>
</If>

Choose Block (When/Otherwise) - This emulates a switch statement.

<Choose>
    <When condition={value === 1}>
        <p>When: Value is 1</p>
    </When>
    <When condition={value === 2}>
        <p>When: Value is 2</p>
    </When>
    <Otherwise>
        <p>Otherwise: Value is above 2</p>
    </Otherwise>
</Choose>

Automatic Wrapping

The plugin automatically replaces the blocks with empty jsx tags (fragments), so a block can have multiple children without needing to wrap children with <></> tags.

// Multiple children
<If condition={value}>
    <p>Hello!</p>
    <p>The if condition is true.</p>
</If>

// Text only
<If condition={value}>Hello!</If>

Code Transform Examples

Here are some examples of what the Vite plugin does to your code.

If Block - Input/Output:

// Input
<If condition={value}>
    <p>Hello</p>
</If>

// Output
{value ? (<>
    <p>Hello</p>
</>) : null}

Choose Block - Input/Output:

// Input
<Choose>
    <When condition={value === 1}>
        <p>When: Value is 1</p>
    </When>
    <When condition={value === 2}>
        <p>When: Value is 2</p>
    </When>
    <Otherwise>
        <p>Otherwise: Value is above 2</p>
    </Otherwise>
</Choose>

// Output
{value === 1 ? (<>
	<p>When: Value is 1</p>
</>) : value === 2 ? (<>
	<p>When: Value is 2</p>
</>) : value === 3 ? (<>
	<p>When: Value is 3</p>
</>) : <>
	<p>Otherwise: Value is above 2</p>
</>}

Development

How to work on this project.

  • Run pnpm install to install dependencies
  • Edit the plugin in package/src/plugin.ts to make changes
  • Run pnpm build to build the plugin
  • Run pnpm run dev in the example folder to test the plugin
  • Navigate to http://localhost:5173/__inspect to check the plugin transformation step (uses vite-plugin-inspect)

Potential Improvements


Similar Libraries

Notes

  • What about ForEach loops?

    • They were not implemented because they make code more verbose than the existing [].map() which already works well. Since we require TypeScript definitions, it is difficult to create a ForEach component that is small.
  • Why not make them global types by default?

    • Global types don't seem to work in npm packages unless the name is prefixed with '@types' or it requires manually adding the types to your tsconfig.
    • More info here
    • There is a current PR in progress on the DefinitelyTyped team repo to add global types for each of the control statements.
  • Why not just use custom React components (like with react-if)?

    • This article provides a good explanation of the issues that using control components can cause. react-if actually solves one of the biggest issues of eager evaluation by recommending to use arrow functions, but it makes the code less readable. In the end, I thought code transpilation with Vite would be a better solution.