37 | Click on the Vite and React logos to learn more
38 |
39 | >
40 | )
41 | }
42 |
43 | export default App
44 |
--------------------------------------------------------------------------------
/public/vite.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | :root {
2 | font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3 | line-height: 1.5;
4 | font-weight: 400;
5 |
6 | color-scheme: light dark;
7 | color: rgba(255, 255, 255, 0.87);
8 | background-color: #242424;
9 |
10 | font-synthesis: none;
11 | text-rendering: optimizeLegibility;
12 | -webkit-font-smoothing: antialiased;
13 | -moz-osx-font-smoothing: grayscale;
14 | -webkit-text-size-adjust: 100%;
15 | }
16 |
17 | a {
18 | font-weight: 500;
19 | color: #646cff;
20 | text-decoration: inherit;
21 | }
22 | a:hover {
23 | color: #535bf2;
24 | }
25 |
26 | body {
27 | margin: 0;
28 | display: flex;
29 | place-items: center;
30 | min-width: 320px;
31 | min-height: 100vh;
32 | }
33 |
34 | h1 {
35 | font-size: 3.2em;
36 | line-height: 1.1;
37 | }
38 |
39 | button {
40 | border-radius: 8px;
41 | border: 1px solid transparent;
42 | padding: 0.6em 1.2em;
43 | font-size: 1em;
44 | font-weight: 500;
45 | font-family: inherit;
46 | background-color: #1a1a1a;
47 | cursor: pointer;
48 | transition: border-color 0.25s;
49 | }
50 | button:hover {
51 | border-color: #646cff;
52 | }
53 | button:focus,
54 | button:focus-visible {
55 | outline: 4px auto -webkit-focus-ring-color;
56 | }
57 |
58 | @media (prefers-color-scheme: light) {
59 | :root {
60 | color: #213547;
61 | background-color: #ffffff;
62 | }
63 | a:hover {
64 | color: #747bff;
65 | }
66 | button {
67 | background-color: #f9f9f9;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/assets/react.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to quickly deploy Bun React project to Netlify
2 |
3 | This is a template showing how you can use Bun to build React project and deploy it to Netlify. We are using Vite and React, if you want to change this for your project, please do.
4 |
5 | # Important clarifications
6 |
7 | A few notes on bun vs bun :)
8 |
9 | When you run `bun dev` it will use `bun` just as a script runner, and your vite will actually be running in Node. But if you run `bun --bun dev` if will use Bun JavaScript runtime to run Vite.
10 |
11 | If this all sounds crazy, just do this (after following the guide below)):
12 |
13 | ```bash
14 | echo 'console.log(Bun);' >> vite.config.ts
15 | bun --bun dev # to test it
16 | bun dev # to see that it crashes
17 | ```
18 |
19 | Again, this doesn't actually matter that much, since it only afffects vite plugins and such, your users are just going to get the same application bundle no matter what runtime vite was using :)
20 |
21 | # Requirements
22 |
23 | - [Bun 1.0 or higher](https://bun.sh/)
24 |
25 | # What to do
26 |
27 | You can clone this repo or just follow the steps below.
28 |
29 | ## 1. Create new React project with Vite using Bun
30 |
31 | ```bash
32 | bunx --bun create-vite -t react-ts
33 | cd vite-project
34 | bun install
35 | ```
36 |
37 | ## 2. Development
38 |
39 | ```
40 | bun --bun dev
41 | ```
42 |
43 | open http://localhost:5173/
44 |
45 | ## 3. Build
46 |
47 | ```
48 | bun --bun run build
49 | ```
50 |
51 |
52 |
53 | Is it fast? Well, almost as fast as Node https://twitter.com/jarredsumner/status/1700680788231311789 :)
54 |
55 |
56 | Keep in mind that `bun run build` or even `npm run build` will probably run slightly faster. Let me say it again, currently building Vite using Node runtime is faster (shocking, I know).
57 |
58 | ```
59 | $ hyperfine --warmup=2 "bun --bun run build" "bun run build" "npm run build"
60 |
61 | Benchmark 1: bun --bun run build
62 | Time (mean ± σ): 2.052 s ± 0.012 s [User: 2.098 s, System: 0.176 s]
63 | Range (min … max): 2.037 s … 2.072 s 10 runs
64 |
65 | Benchmark 2: bun run build
66 | Time (mean ± σ): 1.363 s ± 0.021 s [User: 1.210 s, System: 0.090 s]
67 | Range (min … max): 1.344 s … 1.410 s 10 runs
68 |
69 | Benchmark 3: npm run build
70 | Time (mean ± σ): 1.599 s ± 0.029 s [User: 2.527 s, System: 0.192 s]
71 | Range (min … max): 1.570 s … 1.666 s 10 runs
72 |
73 | Summary
74 | 'bun run build' ran
75 | 1.17 ± 0.03 times faster than 'npm run build'
76 | 1.51 ± 0.03 times faster than 'bun --bun run build'
77 | ```
78 |
79 |
80 | ## 4. Connect to Netlify
81 |
82 | make sure to connect your repo to github and netlify to enable automatic builds.
83 |
84 | ```
85 | ntl init
86 | ```
87 |
88 | Edit `netlify.toml`:
89 |
90 | ```toml
91 | [build]
92 | # custom build command that install bun and run build
93 | command = "./scripts/build.sh"
94 | # vite output folder
95 | publish = "dist"
96 |
97 | [build.environment]
98 | # disable NPM install
99 | NPM_FLAGS = "--version"
100 | ```
101 |
102 | Edit `scripts/build.sh`:
103 |
104 | ```sh
105 | #!/bin/bash
106 | set -e
107 | curl -fsSL https://bun.sh/install | bash
108 | export PATH="/opt/buildhome/.bun/bin:$PATH"
109 | bun --version
110 | bun install
111 | bun --bun run build
112 | ```
113 |
114 | ## 5. Deploy
115 |
116 | Just push to github and netlify will build and deploy your site.
117 |
118 | ## Learn More
119 |
120 | - [How to build production build of Bun React](https://dev.to/ashirbadgudu/create-a-react-app-with-bun-125o)
121 | - [How to install Bun](https://bun.sh/)
122 | - [Install Deno on Netlify](https://dbushell.com/2021/07/22/netlify-deno-builds/) --- we use the same idea to install Bun
123 | - [How to disable NPM install](https://answers.netlify.com/t/prevent-npm-from-running-on-deploy/66882/3)
124 | - [How to use Bun with Netlify](https://stackoverflow.com/a/74883541)
125 |
126 | ## Note on `bun dev`
127 |
128 | This repo used to use `bun dev` which was deprecated in Bun 1.0 but you can still see those branches:
129 |
130 | - [Using "bun dev" and react-scripts for prod](https://github.com/JLarky/bun-netlify/tree/react-scripts)
131 | - [Using "bun dev" and vite for prod](https://github.com/JLarky/bun-netlify/tree/vite)
132 |
--------------------------------------------------------------------------------