Files
bun.sh/docs/runtime/jsx.md
Colin McDonnell 9b6913e1a6 More/better docs for JSX, utils, binary data, streams, hashing, bun test, Bun.serve (#3005)
* WIP

* Updates

* Document deepEquals

* WIP

* Update typeS

* Update TLS docs for Bun.serve

* Update types for tls

* Draft binary data page. Add Streams page.

* Update test runner docs

* Add hashing, flesh out utils

* Grammar

* Update types

* Fix

* Add import.meta docs

* Tee
2023-05-29 11:49:51 -07:00

6.0 KiB

Bun supports .jsx and .tsx files out of the box. Bun's internal transpiler converts JSX syntax into vanilla JavaScript before execution.

function Component(props: {message: string}) {
  return (
    <body>
      <h1 style={{color: 'red'}}>{props.message}</h1>
    </body>
  );
}

console.log(<Component message="Hello world!" />);

Configuration

Bun reads your tsconfig.json or jsconfig.json configuration files to determines how to perform the JSX transform internally. To avoid using either of these, the following options can also be defined in bunfig.json.

The following compiler options are respected.

jsx

How JSX constructs are transformed into vanilla JavaScript internally. The table below lists the possible values of jsx, along with their transpilation of the following simple JSX component:

<Box width={5}>Hello</Box>

{% table %}

  • Compiler options
  • Transpiled output

  • {
      "jsx": "react"
    }
    
  • import { createElement } from "react";
    createElement("Box", { width: 5 }, "Hello");
    

  • {
      "jsx": "react-jsx"
    }
    
  • import { jsx } from "react/jsx-runtime";
    jsx("Box", { width: 5 }, "Hello");
    

  • {
      "jsx": "react-jsxdev"
    }
    
  • import { jsxDEV } from "react/jsx-dev-runtime";
    jsxDEV("Box", { width: 5, children: "Hello" }, undefined, false, undefined, this);
    

    The jsxDEV variable name is a convention used by React. The DEV suffix is a visible way to indicate that the code is intended for use in development. The development version of React is slowers and includes additional validity checks & debugging tools.


  • {
      "jsx": "preserve"
    }
    
  • // JSX is not transpiled
    // "preserve" is not supported by Bun currently
    <Box width={5}>Hello</Box>
    

{% /table %}

jsxFactory

{% callout %} Note — Only applicable when jsx is react. {% /callout %}

The function name used to represent JSX constructs. Default value is "createElement". This is useful for libraries like Preact that use a different function name ("h").

{% table %}

  • Compiler options
  • Transpiled output

  • {
      "jsx": "react",
      "jsxFactory": "h"
    }
    
  • import { createhElement } from "react";
    h("Box", { width: 5 }, "Hello");
    

{% /table %}

jsxFragmentFactory

{% callout %} Note — Only applicable when jsx is react. {% /callout %}

The function name used to represent JSX fragments such as <>Hello</>; only applicable when jsx is react. Default value is "Fragment".

{% table %}

  • Compiler options
  • Transpiled output

  • {
      "jsx": "react",
      "jsxFactory": "myjsx",
      "jsxFragmentFactory": "MyFragment"
    }
    
  • // input
    <>Hello</>;
    
    // output
    import { myjsx, MyFragment } from "react";
    createElement("Box", { width: 5 }, "Hello");
    

{% /table %}

jsxImportSource

{% callout %} Note — Only applicable when jsx is react-jsx or react-jsxdev. {% /callout %}

The module from which the component factory function (createElement, jsx, jsxDEV, etc) will be imported. Default value is "react". This will typically be necessary when using a component library like Preact.

{% table %}

  • Compiler options
  • Transpiled output

  • {
      "jsx": "react"
      // jsxImportSource is not defined
      // default to "react"
    }
    
  • import { jsx } from "react/jsx-runtime";
    jsx("Box", { width: 5, children: "Hello" });
    

  • {
      "jsx": "react-jsx",
      "jsxImportSource": "preact"
    }
    
  • import { jsx } from "preact/jsx-runtime";
    jsx("Box", { width: 5, children: "Hello" });
    

  • {
      "jsx": "react-jsxdev",
      "jsxImportSource": "preact"
    }
    
  • // /jsx-runtime is automatically appended
    import { jsxDEV } from "preact/jsx-dev-runtime";
    jsxDEV("Box", { width: 5, children: "Hello" }, undefined, false, undefined, this);
    

{% /table %}

JSX pragma

All of these values can be set on a per-file basis using pragmas. A pragma is a special comment that sets a compiler option in a particular file.

{% table %}

  • Pragma
  • Equivalent config

  • // @jsx h
    
  • {
      "jsxFactory": "h"
    }
    

  • // @jsxFrag MyFragment
    
  • {
      "jsxFragmentFactory": "MyFragment"
    }
    

  • // @jsxImportSource preact
    
  • {
      "jsxImportSource": "preact"
    }
    

{% /table %}

Logging

Bun implements special logging for JSX to make debugging easier. Given the following file:

import { Stack, UserCard } from "./components";

console.log(
  <Stack>
    <UserCard name="Dom" bio="Street racer and Corona lover" />
    <UserCard name="Jakob" bio="Super spy and Dom's secret brother" />
  </Stack>
);

Bun will pretty-print the component tree when logged:

{% image src="https://github.com/oven-sh/bun/assets/3084745/d29db51d-6837-44e2-b8be-84fc1b9e9d97" / %}

Prop punning

The Bun runtime also supports "prop punning" for JSX. This is a shorthand syntax useful for assigning a variable to a prop with the same name.

function Div(props: {className: string;}) {
  const {className} = props;

  // without punning
  return <div className={className} />;
  // with punning
  return <div {className} />;
}