温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/runtime/jsx
点击访问原文链接
JSX - BunDocumentation Index

Fetch the complete documentation index at: /docs/llms.txt

Use this file to discover all available pages before exploring further.

Skip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationFile & Module SystemJSXRuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedback:first-child]:!hidden peer-[.is-custom]:[&>:first-child]:sm:!hidden peer-[.is-custom]:[&>:first-child]:md:!hidden peer-[.is-custom]:[&>:first-child]:lg:!hidden peer-[.is-custom]:[&>:first-child]:xl:!hidden">Get StartedWelcome to BunInstallationQuickstartTypeScriptTypeScript 6 and 7bun initbun createCore RuntimeBun RuntimeWatch ModeDebuggingREPLbunfig.tomlFile & Module SystemFile TypesModule ResolutionJSXAuto-installPluginsFile System RouterHTTP serverServerRoutingCookiesTLSError HandlingMetricsNetworkingFetchWebSocketsTCPUDPDNSData & StorageCookiesFile I/OStreamsBinary DataArchiveSQLSQLiteS3RedisConcurrencyWorkersProcess & SystemEnvironment VariablesShellSpawnWebViewCronInterop & ToolingNode-APIFFIC CompilerTranspilerUtilitiesCSRF ProtectionSecretsConsoleTOMLYAMLMarkdownJSON5JSONLHTMLRewriterImageHashingGlobSemverColorUtilsStandards & CompatibilityGlobalsBun APIsWeb APIsNode.js CompatibilityContributingRoadmapBenchmarkingContributingBuilding WindowsBindgenLicenseOn this pageConfigurationjsxjsxFactoryjsxFragmentFactoryjsxImportSourceJSX pragmaLoggingProp punningFile & Module SystemJSXCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_n4ctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">

Built-in JSX and TSX support in Bun with configurable transpilation options

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_1cctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">Bun supports .jsx and .tsx files out of the box. Bun’s internal transpiler converts JSX syntax into vanilla JavaScript before execution. react.tsx
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.toml. 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}>HelloBox>
Compiler optionsTranspiled outputjson
{
"jsx": "react"
}
tsx
import { createElement } from "react";
createElement("Box", { width: 5 }, "Hello");
json
{
"jsx": "react-jsx"
}
tsx
import { jsx } from "react/jsx-runtime";
jsx("Box", { width: 5 }, "Hello");
json
{
"jsx": "react-jsxdev"
}
tsx
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 slower and includes additional validity checks & debugging tools.json
{
"jsx": "preserve"
}
tsx
// JSX is not transpiled
// "preserve" is not supported by Bun currently
Hello
jsxFactory Note — Only applicable when jsx is react. 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"). Compiler optionsTranspiled outputjson
{
"jsx": "react",
"jsxFactory": "h"
}
tsx
import { h } from "react";
h("Box", { width: 5 }, "Hello");
jsxFragmentFactory Note — Only applicable when jsx is react. The function name used to represent JSX fragments such as <>Hello; only applicable when jsx is react. Default value is "Fragment". Compiler optionsTranspiled outputjson
{
"jsx": "react",
"jsxFactory": "myjsx",
"jsxFragmentFactory": "MyFragment"
}
tsx
// input
<>Hello;

// output
import { myjsx, MyFragment } from "react";
myjsx(MyFragment, null, "Hello");
jsxImportSource Note — Only applicable when jsx is react-jsx or react-jsxdev. 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. Compiler optionsTranspiled outputjsonc
{
"jsx": "react",
// jsxImportSource is not defined
// default to "react"
}
tsx
import { jsx } from "react/jsx-runtime";
jsx("Box", { width: 5, children: "Hello" });
jsonc
{
"jsx": "react-jsx",
"jsxImportSource": "preact",
}
tsx
import { jsx } from "preact/jsx-runtime";
jsx("Box", { width: 5, children: "Hello" });
jsonc
{
"jsx": "react-jsxdev",
"jsxImportSource": "preact",
}
tsx
// /jsx-runtime is automatically appended
import { jsxDEV } from "preact/jsx-dev-runtime";
jsxDEV(
"Box",
{ width: 5, children: "Hello" },
undefined,
false,
undefined,
this,
);
​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. PragmaEquivalent configts
// @jsx h
jsonc
{
"jsxFactory": "h",
}
ts
// @jsxFrag MyFragment
jsonc
{
"jsxFragmentFactory": "MyFragment",
}
ts
// @jsxImportSource preact
jsonc
{
"jsxImportSource": "preact",
}
​Logging Bun implements special logging for JSX to make debugging easier. Given the following file: index.tsx
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: ​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. react.tsx
function Div(props: {className: string;}) {
const {className} = props;

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

Was this page helpful?

YesNoSuggest editsRaise issueModule ResolutionPreviousAuto-installNext⌘IxgithubdiscordyoutubePowered byThis documentation is built and hosted on Mintlify, a developer documentation platform

JSX - Bun,AI智能索引,全网链接索引,智能导航,网页索引

    Built-in JSX and TSX support in Bun with configurable transpilation options