Skip to content

JSX in .vibex files

A .vibex file is a .vibe file that also accepts JSX. It is to .vibe what .tsx is to .ts: every JSX rule is TypeScript's, and every Vibe form works as it does in a .vibe file. Use .vibex for components in React, Solid, Vue and other frameworks that use JSX. VIP-39 defines the file kind.

// Profile.vibex
error ProfileMissing { id: string }
 
const names: Record<string, string> = { "1": "ada lovelace" }
 
export effect function loadName(id: string) {
  const name = names[id]
  if (name === undefined) throw new ProfileMissing({ id })
  return name
}
 
const titleCase = (text: string) => text.replace(/\b\w/g, (letter) => letter.toUpperCase())
 
export function ProfileCard(props: { name: string; role: "admin" | "member" }) {
  return (
    <section class="card">
      <h2>{props.name |> titleCase(%)}</h2>
      {match (props.role) { when "admin": <span>admin</span>; default: null; }}
    </section>
  )
}

The file mixes three Vibe forms with JSX: an error declaration, an effect function, and, inside the JSX, the pipe operator |> and a match expression. The documentation checker does not compile .vibex samples, so this sample was compiled by hand with "jsx": "preserve" for this page. The component compiles to the following JavaScript:

export function ProfileCard(props) {
    return (<section class="card">
      <h2>{titleCase(props.name)}</h2>
      {(() => {
        const $match1 = props.role;
        {
            if ($match1 === "admin") {
                return <span>admin</span>;
            }
        }
        return null;
    })()}
    </section>);
}

Configure JSX

Set compilerOptions.jsx, and jsxImportSource when your framework needs one, in tsconfig.json exactly as you do for .tsx files. vibelang reads the same options:

{
  "compilerOptions": { "jsx": "react-jsx" },
  "include": ["src/**/*.vibe", "src/**/*.vibex", "src/**/*.ts", "src/**/*.tsx"]
}

With "jsx": "preserve", Profile.vibex compiles to Profile.jsx, and a framework plugin compiles the JSX. For the Vite plugin and each framework's setup, see Vite and Vitest, React, Vue and Svelte.

Where JSX and Vibe syntax meet

One rule decides every case where JSX and a Vibe form compete for the same characters: < at the start of an expression begins a JSX element, as it does in a .tsx file.

  • Generic arrow functions need a trailing comma. Write effect <T,>(value: T) => value, as you write async <T,>(value: T) => value in .tsx.
  • Type assertions use as. <User>value is JSX in a .vibex file. Write value as User.
  • An operand can be JSX. throw <A/> reads the element as the operand, and so do try, fork and join.
  • Text inside an element is text. In <p>50% off</p>, the % is not the pipe operator's topic.