Skip to content

React

Add VibeLang to a React + Vite app. A component runs a VibeLang program through a ManagedRuntime and renders loading, success, and recovered-error states. Complete Add VibeLang to an existing project first; this page replaces Vite and Vitest.

1. Install the test packages

With Bun:

bun add -d happy-dom@20.14.6 @testing-library/react@16.3.3 @testing-library/dom@10.4.2

With npm:

npm install --save-dev --save-exact happy-dom@20.14.6 @testing-library/react@16.3.3 @testing-library/dom@10.4.2

2. Configure tsconfig.json

Run vibelang to write adjacent .d.vibe.ts declarations (even with --noEmit) and configure TypeScript tools to read them.

In Vite's react-ts template, tsconfig.json references tsconfig.app.json (which includes src, covering .vibe files) and tsconfig.node.json. The VibeLang plugin automatically enables rewriteRelativeImportExtensions during emit, so vite build works alongside allowImportingTsExtensions. If you also emit JavaScript directly via the vibelang CLI, add "rewriteRelativeImportExtensions": true to compilerOptions (allowImportingTsExtensions alone produces TS5096).

3. Configure Vite and Vitest

Update vite.config.ts (preserving any existing plugins) to place vibelang() before react(), pass the config covering src as project (omit project if tsconfig.json directly includes src), and import defineConfig from vitest/config:

// TypeScript (.ts)
import react from "@vitejs/plugin-react"
import vibelang from "vibelang-vite"
import { defineConfig } from "vitest/config"
 
export default defineConfig({
  plugins: [vibelang({ project: "tsconfig.app.json" }), react()],
  test: {
    environment: "happy-dom",
    include: ["src/**/*.test.vibe", "src/**/*.test.tsx"]
  }
})

4. Add a VibeLang module

Create src/users.vibe. It declares a Users service and its layer; greet recovers from UserNotFound, so it cannot fail:

// src/users.vibe
export schema User { id: string; name: string }
 
export error UserNotFound { id: string }
 
export service Users {
  find(id: string): User!UserNotFound
}
 
const directory: ReadonlyArray<User> = [
  { id: "42", name: "Ada Lovelace" },
  { id: "7", name: "Grace Hopper" }
]
 
export const UsersLive = Layer.succeed(Users, {
  effect find(id) {
    const user = directory.find((entry) => entry.id === id)
    if (user === undefined) throw new UserNotFound({ id })
    return user
  }
})
 
export effect function greet(id: string) {
  const users = try Users
  return effect {
    const user = try users.find(id)
    return `Welcome, ${user.name}`
  } catch (error) => match (error) {
    when UserNotFound: `No user ${error.id}`;
  }
}
// Run in an effect function:
//   provide UsersLive {
//     try Console.log(try greet("42"))
//     try Console.log(try greet("404"))
//     const users = try Users
//     try Console.log(users.find("404") catch (error) => `${error._tag} ${error.id}`)
//   }
Welcome, Ada Lovelace
No user 404
UserNotFound 404

5. Run a program from a component

Create src/runtime.ts. One ManagedRuntime builds UsersLive once and runs every program in the app:

// TypeScript (.ts)
// src/runtime.ts
import { ManagedRuntime } from "effect"
import { UsersLive } from "./users.vibe"
 
export const runtime = ManagedRuntime.make(UsersLive)
// Run: return import("./users.vibe").then(({ greet }) => runtime.runPromise(greet("42"))).then(console.log).finally(() => runtime.dispose())
Welcome, Ada Lovelace

Create src/Greeting.tsx. To TypeScript, greet(id) returns Effect<string, never, Users>; runtime.runPromise provides Users and returns a Promise<string>. The AbortSignal interrupts the program when the component unmounts or id changes:

// src/Greeting.tsx
import { useEffect, useState } from "react"
import { runtime } from "./runtime.ts"
import { greet } from "./users.vibe"
 
export function Greeting({ id }: { readonly id: string }) {
  const [text, setText] = useState<string>()
  useEffect(() => {
    const controller = new AbortController()
    runtime.runPromise(greet(id), { signal: controller.signal }).then(setText, (cause) => {
      if (!controller.signal.aborted) throw cause
    })
    return () => controller.abort()
  }, [id])
  return <p>{text ?? "Loading…"}</p>
}

Render it from an existing component, such as App in src/App.tsx: add import { Greeting } from "./Greeting.tsx", <Greeting id="42" />, and <Greeting id="404" />. The page shows Loading… twice, then Welcome, Ada Lovelace and No user 404.

6. Test the module and the component

Create src/users.test.vibe:

// src/users.test.vibe
import { assert, it } from "@effect/vitest"
import { greet, UserNotFound, Users, UsersLive } from "./users.vibe"
 
it.effect("greets a known user", () => effect {
  provide UsersLive {
    const message = try greet("42")
    try Effect.try(() => assert.strictEqual(message, "Welcome, Ada Lovelace"))
  }
})
 
it.effect("fails a lookup with UserNotFound", () => effect {
  provide UsersLive {
    const users = try Users
    const outcome = try result(users.find("404"))
    try Effect.try(() => {
      assert(Result.isFailure(outcome))
      if (Result.isFailure(outcome)) {
        assert.instanceOf(outcome.failure, UserNotFound)
        assert.strictEqual(outcome.failure.id, "404")
      }
    })
  }
})
 
it.effect("recovers UserNotFound in greet", () => effect {
  provide UsersLive {
    const message = try greet("404")
    try Effect.try(() => assert.strictEqual(message, "No user 404"))
  }
})

Create src/Greeting.test.tsx:

// src/Greeting.test.tsx
import { render, screen } from "@testing-library/react"
import { expect, it } from "vitest"
import { Greeting } from "./Greeting.tsx"
 
it("shows loading, then the user and the recovered error", async () => {
  render(<><Greeting id="42" /><Greeting id="404" /></>)
  expect(screen.getAllByText("Loading…")).toHaveLength(2)
  expect(await screen.findByText("Welcome, Ada Lovelace")).toBeDefined()
  expect(await screen.findByText("No user 404")).toBeDefined()
})

If package.json has no test script, add "test": "vitest run". Run npx vitest run. The output includes:

 Test Files  2 passed (2)
      Tests  4 passed (4)

7. Type-check and build

tsc -b cannot read .vibe files and fails with TS2307 (Cannot find module './users.vibe'). Replace it with vibelang -b, which builds the same project references and checks .vibe, .ts, and .tsx together:

{
  "scripts": {
    "build": "vibelang -b && vite build"
  }
}

Run npx vibelang -b (silent, exit 0), then npm run build or bun run build (exit 0, ending with ✓ built in).

Next

Configure your editor (VS Code and Cursor, Neovim, Zed, or other editors), then verify with step 4 of Set up VibeLang.