this repo has no description
1
2Default to using Bun instead of Node.js.
3
4- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
5- Use `bun test` instead of `jest` or `vitest`
6- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
7- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
8- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
9- Bun automatically loads .env, so don't use dotenv.
10
11## APIs
12
13- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
14- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
15- `Bun.redis` for Redis. Don't use `ioredis`.
16- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
17- `WebSocket` is built-in. Don't use `ws`.
18- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
19- Bun.$`ls` instead of execa.
20
21## Testing
22
23Use `bun test` to run tests.
24
25```ts#index.test.ts
26import { test, expect } from "bun:test";
27
28test("hello world", () => {
29 expect(1).toBe(1);
30});
31```
32
33## Frontend
34
35Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
36
37Server:
38
39```ts#index.ts
40import index from "./index.html"
41
42Bun.serve({
43 routes: {
44 "/": index,
45 "/api/users/:id": {
46 GET: (req) => {
47 return new Response(JSON.stringify({ id: req.params.id }));
48 },
49 },
50 },
51 // optional websocket support
52 websocket: {
53 open: (ws) => {
54 ws.send("Hello, world!");
55 },
56 message: (ws, message) => {
57 ws.send(message);
58 },
59 close: (ws) => {
60 // handle close
61 }
62 },
63 development: {
64 hmr: true,
65 console: true,
66 }
67})
68```
69
70HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
71
72```html#index.html
73<html>
74 <body>
75 <h1>Hello, world!</h1>
76 <script type="module" src="./frontend.tsx"></script>
77 </body>
78</html>
79```
80
81With the following `frontend.tsx`:
82
83```tsx#frontend.tsx
84import React from "react";
85
86// import .css files directly and it works
87import './index.css';
88
89import { createRoot } from "react-dom/client";
90
91const root = createRoot(document.body);
92
93export default function Frontend() {
94 return <h1>Hello, world!</h1>;
95}
96
97root.render(<Frontend />);
98```
99
100Then, run index.ts
101
102```sh
103bun --hot ./index.ts
104```
105
106For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.