
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
vite-plugin-react-sfc
Advanced tools
Single File Components (SFC) for react, inspired by Vue/Svelte SFC, but for React.
Experimental: This project is in the early stages of development and may not be suitable for production use. Use at your own risk.
React SFC is a Vite plugin that enables you to define React components using a single file format, similar to Vue and Svelte Single File Components (SFC). This makes it easier to organize your components and reduces the boilerplate code required to set up a React component.
With React SFC, you can define your component's template, logic, and styles in a single .react file. This allows you to keep your codebase clean and maintainable, with a familiar syntax that is easy to read and understand.
I’ve been using React for 4 years ( day to day job ). I love React, I love the ecosystem, I love the community. But JSX? OH, JSX MAKES ME WANT TO PUNCH A WALL. IT MAKES ME SICK. I HATE IT WITH EVERY FIBER OF MY BEING. EVERYTHING IS A FUCKING MESS. JavaScript shoved into my HTML. HTML puked into my JavaScript AAAAAHHHHH.
And don’t even get me started on the FUCKING className. WHY?! Why can’t I just use class like a normal, sane person? Oh no, JSX has to be “special.” It has to remind me on every line that I’m trapped in its twisted logic. “Oh, you wanted to style this? Sorry, class is a reserved keyword. Here, take this bastardized, verbose nonsense instead!”
All I want is SEPARATION. Clean, beautiful separation. HTML stays in HTML. CSS stays in CSS. JavaScript stays in JS where it belongs. But JSX? No, JSX is the unholy cocktail of chaos, forcing you to mix it all together like some deranged lunatic.
So I snapped. I built this vite plugin a single-file component (SFC) for React inspired by the perfection of Svelte and Vue. JSX? className? ALL OF IT CAN GO STRAIGHT TO THE GARBAGE WHERE IT BELONGS. This plugin is my rebellion. My middle finger to the madness.
If you feel the same way, you might like this plugin and contribute. If you don’t, that’s cool too. I’m just a guy who hates JSX. No big deal.
Sorry for the rant. I just really hate JSX, let's get back to business, here are some features of React SFC:
.react file.$if: Simplify conditional rendering in your templates.$for: Streamline list rendering with a concise loop syntax.<template> block, with additional directives to reduce awkward JavaScript within your HTML.lang attribute in the <script> block to specify ts or js ( default is JS).scss, less, or stylus in the <style> block with the lang attribute ( default is CSS).To get started with React SFC, you need to install the plugin and configure it in your Vite project. Once set up, you can start creating components using the .react file extension.
Here's an example of a React SFC component:
<template>
<div>
<h1>Counter</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
<p $if={count > 0}>Count: {count}</p>
<p $if={count === 0}>Start counting!</p>
</div>
</template>
<script lang="ts">
import { useState } from 'react';
const [count, setCount] = useState<number>(0);
</script>
<style>
button {
padding: 10px 20px;
font-size: 16px;
}
</style>
First, install the plugin as a development dependency:
npm install --save-dev vite-plugin-react-sfc
Or with Yarn/Bun:
yarn add --dev vite-plugin-react-sfc
Or with PNPM:
pnpm add --save-dev vite-plugin-react-sfc
Add the plugin to your Vite configuration:
import { defineConfig } from 'vite';
import reactSFC from 'vite-plugin-react-sfc';
export default defineConfig({
plugins: [
reactSFC(),
// other plugins...
],
});
Now, you can start creating .react files in your project.
The <template> block uses JSX syntax, enhanced with custom directives like $if and $for to simplify your code and avoid embedding awkward JavaScript within your HTML structure. This allows for a more declarative and readable template.
Use the $if attribute to conditionally render elements in your template.
Syntax:
<element $if="{condition}">Content</element>
Example:
<span $if="{count > 0}">Count is positive</span>
Use the $for attribute to render lists efficiently.
Syntax:
<element $for="{item in items}">{item}</element>
Example:
<li $for="{fruit in fruits}">{fruit}</li>
You can write your component logic in the <script> block. Use the lang attribute to specify the scripting language:
lang="ts" for TypeScript.lang="js" for JavaScript. (default if lang is omitted)Example:
<script lang="ts">
import { useState } from 'react';
const [count, setCount] = useState<number>(0);
</script>
Define your component styles in the <style> block. You can use CSS or preprocessors like SCSS, Less, or Stylus by specifying the lang attribute:
lang="scss" for SCSS.lang="less" for Less.lang="stylus" for Stylus.lang="css" for standard CSS (default if lang is omitted).Example:
<style lang="scss">
button {
padding: 10px 20px;
font-size: 16px;
}
</style>
For a better development experience, you can install the React SFC VS Code extension to get syntax highlighting and snippets for React SFC files, I know it's not perfect, but it's a start.
Note: To use preprocessors, ensure you have the corresponding module installed, see the Vite documentation for more information.
This project is licensed under the MIT License.
Thank you for using React SFC! If you have any questions or need support, feel free to open an issue on the GitHub repository.
FAQs
Vite plugin for React SFC
The npm package vite-plugin-react-sfc receives a total of 3 weekly downloads. As such, vite-plugin-react-sfc popularity was classified as not popular.
We found that vite-plugin-react-sfc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.