React Style Bundler

1.2.3 · active · verified Tue Apr 21

React Style Bundler is a CSS-in-JS library for React applications that leverages JavaScript's native import mechanism to manage and bundle CSS. Currently at version 1.2.3, it integrates directly into your build process by tracking CSS as it's imported via `styled` components. It is particularly well-suited for Server-Side Rendering (SSR) environments, providing a bundler-agnostic approach where the JavaScript runtime's import order dictates the final CSS bundle. Unlike traditional CSS bundlers, it doesn't require specific build tool plugins; instead, you import your main React component and then retrieve the accumulated CSS string. Its release cadence appears demand-driven, without a fixed schedule. A key differentiator is its reliance on the existing JavaScript module graph for CSS dependency resolution and bundling, aiming for a simpler setup for SSR.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a styled component, integrate it into a React application, and then extract the final CSS bundle using `styled.getCSSBundle()` after the application's components have been processed.

// Import the bundler instance and styled function
import { styled } from "react-style-bundler";

// Define a simple React App component that uses styled components
function MyStyledComponent() {
  return styled.div`
    background-color: #f0f0f0;
    padding: 20px;
    border-radius: 8px;
    font-family: sans-serif;

    h1 {
      color: #333;
      margin-bottom: 10px;
    }

    button {
      background-color: #007bff;
      color: white;
      border: none;
      padding: 10px 15px;
      border-radius: 5px;
      cursor: pointer;

      &:hover {
        background-color: #0056b3;
      }
    }
  `;
}

// Assume this is your main application component,
// importing it triggers the style registration.
// In a real app, this would be your root component, e.g., <App />
const App = () => {
  const StyledDiv = MyStyledComponent(); // Create instance of styled component
  return (
    <StyledDiv>
      <h1>Hello from React Style Bundler!</h1>
      <button>Click Me</button>
    </StyledDiv>
  );
};

// Simulate rendering the app to ensure all styles are registered
// In a real SSR scenario, you'd render this to a string.
// For demonstration, just importing it is enough to register styles.
console.log("App component imported, styles are now registered.");
App(); // Calling it to ensure the styled components are processed

// After your application component has been processed,
// retrieve the bundled CSS as a string.
let cssBundle = styled.getCSSBundle();

// In a real build step, you would then save this string to a file,
// e.g., 'build/out.css'
console.log("\n--- Generated CSS Bundle ---");
console.log(cssBundle);
// Example of saving: await saveFile(cssBundle, "./build/out.css");

view raw JSON →