react-rewrite

repository·main·Indexed 43 days ago

https://github.com/donghaxkim/react-rewrite

A visual editing tool for React applications that allows developers to edit components, Tailwind styles, and text directly in the browser during local development. It uses a local proxy to automatically write changes back to source files. Compatible with Next.js, Vite, and Create React App (CRA) on Node.js 20+ and React 18+.

Tokens
11.7K
Snippets
49
Records
60
Agent score
56%

What's inside react-rewrite

  1. Requirements for react-rewrite

    main

    To use react-rewrite, ensure your environment meets these requirements:

    • Node.js: version 20 or higher.
    • React: version 18 or higher.
    • Development Server: A running local dev server.
    • Supported Frameworks: Next.js, Vite, or Create React App.
    • Tailwind CSS (Recommended): Required for using the property editor for layout, spacing, size, typography, and color. Text editing and structural actions work without Tailwind.
  2. Quick start with react-rewrite

    main

    Follow these steps to start editing your React app visually:

    1. Start your React development server as usual (e.g., npm run dev or npm start).
    2. In a second terminal, from your project root, run the CLI:
    npx react-rewrite

    If the tool fails to auto-detect your development server's port, specify it manually:

    npx react-rewrite 3000

    react-rewrite will open a local proxy in your browser and inject an editing overlay. Changes you confirm in the UI will be written directly to your source files.

  3. Use the react-rewrite editing workflow

    main

    The visual editing process follows this flow:

    1. Select: Click an element in the browser to inspect its component name, file path, and line number.
    2. Edit:
      • Use the sidebar to edit properties (Tailwind-based layout, spacing, size, typography, and color).
      • Double-click text to edit it inline.
      • Use shortcuts to copy, paste, duplicate, or delete elements.
      • Drag elements to reorder them (where supported).
    3. Review: Check the pending changes in the UI.
    4. Apply: Click Confirm to write the changes to your project's source files.
  4. Important usage notes for react-rewrite

    main

    Keep the following constraints in mind when using react-rewrite:

    • Directory Context: Always run the command from your application's root directory.
    • Build Type: It is only compatible with development builds; it will not work with production builds.
    • File Scope: The tool can only write changes back to files that reside within the current project directory.
  5. Install and run react-rewrite

    main

    To use react-rewrite to visually edit your React application, follow these steps:

    1. Install the CLI: From the root of your React project, install the CLI as a development dependency:
      npm install -D react-rewrite-cli
    2. Start your dev server: Ensure your React application's development server is running (e.g., via Vite, Next.js, or Create React App).
    3. Launch the proxy: In a separate terminal window, run the following command to start the proxy and inject the visual editing overlay:
      npx react-rewrite

    If you want to try it without installing it to your project first, you can run:

    npx react-rewrite-cli@latest
    npm install -D react-rewrite-cli
    npx react-rewrite
  6. How Tailwind v3 configuration is resolved

    main

    For Tailwind v3 projects, the resolver uses the official resolveConfig utility from the tailwindcss package:

    1. It attempts to load a user configuration file (e.g., tailwind.config.js, tailwind.config.ts, tailwind.config.cjs, or tailwind.config.mjs) from the projectRoot.
    2. It calls resolveConfig(userConfig) to get the fully resolved theme.
    3. It flattens nested scales (like colors) and handles special v3 types (like fontSize tuples [size, lineHeight]) to ensure a consistent token map format.
  7. How Tailwind v4 configuration is resolved

    main

    For Tailwind v4 projects, the resolver uses a CSS-first approach:

    1. It scans common directories (src, app, styles, css, etc.) for .css or .scss files.
    2. It parses @theme { ... } blocks within those files to extract custom CSS properties.
    3. It maps specific CSS property prefixes to Tailwind scales:
      • --color-* $\rightarrow$ colors
      • --spacing-* $\rightarrow$ spacing
      • --text-* $\rightarrow$ fontSize
      • --font-weight-* $\rightarrow$ fontWeight
      • --radius-* $\rightarrow$ borderRadius
      • --border-* $\rightarrow$ borderWidth
      • --opacity-* $\rightarrow$ opacity
      • --tracking-* $\rightarrow$ letterSpacing
      • --leading-* $\rightarrow$ lineHeight
    4. These custom tokens are merged with the DEFAULT_V4_THEME, where custom properties take precedence.
  8. Understand JSXStructuralPath for element identity

    main

    A JSXStructuralPath provides a deterministic way to identify a specific JSX element within a component tree. This is used to ensure that operations (like updates or deletions) target the correct element even if the file structure changes slightly.

    It consists of:

    • componentName: The name of the function containing the element.
    • filePath: The relative path to the source file.
    • segments: An ordered array of JSXPathSegment objects representing the path from the component root to the target.

    Each JSXPathSegment includes a name (tag or component name) and a discriminator to resolve ambiguity (e.g., via key, id, or index).

    export interface JSXPathSegment {
      name: string;
      discriminator:
        | { type: "key"; value: string }
        | { type: "id"; value: string }
        | { type: "index"; value: number }
        | { type: "map-template" }
        | { type: "root" };
      classHint?: string[];
    }
    
    export interface JSXStructuralPath {
      componentName: string;
      filePath: string;
      segments: JSXPathSegment[];
    }
  9. Supported ClientMessage types for SketchServer

    main

    The SketchServer processes several types of messages from clients. These are categorized into sequential operations (which are queued to prevent race conditions) and concurrent/read-only operations.

    Sequential Operations (Queued)

    • reorder: Moves a component from fromLine to toLine.
    • undo: Pops the last entry from the undoStack and restores the file content.
    • updateProperty / updateProperties: Updates Tailwind classes or other properties using the batch engine.
    • updateText: Updates the text content of an element.
    • commitBatch: Executes an array of BatchOperation objects.
    • revertChanges: Reverts multiple specific changes using provided undoIds.

    Concurrent / Read-only Operations

    • ping: Returns a pong response.
    • getSiblings: Returns a list of sibling elements for a given file and parent line.
    • discoverFile: Searches for a file by componentName.
    • fileStat: Returns the mtime and size of a specific file.