Skip to content

Commit f4ed475

Browse files
committed
feat: using bun
1 parent 2909599 commit f4ed475

13 files changed

+350
-2882
lines changed

Dockerfile

+37-28
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
FROM node:20-alpine AS base
2-
WORKDIR /app
3-
4-
FROM base AS deps
5-
COPY package-lock.json ./package-lock.json
6-
COPY package.json ./package.json
7-
RUN npm install
8-
9-
FROM base AS prod-deps
10-
COPY package-lock.json ./package-lock.json
11-
COPY package.json ./package.json
12-
RUN npm install --omit=dev
13-
1+
# use the official Bun image
2+
# see all versions at https://hub.docker.com/r/oven/bun/tags
3+
FROM oven/bun:1.2-alpine AS base
4+
WORKDIR /usr/src/app
5+
6+
# install dependencies into temp directory
7+
# this will cache them and speed up future builds
8+
FROM base AS install
9+
RUN mkdir -p /temp/dev
10+
COPY package.json bun.lock /temp/dev/
11+
RUN cd /temp/dev && bun install --frozen-lockfile
12+
13+
# install with --production (exclude devDependencies)
14+
RUN mkdir -p /temp/prod
15+
COPY package.json bun.lock /temp/prod/
16+
RUN cd /temp/prod && bun install --frozen-lockfile --production
17+
18+
# copy node_modules from temp directory
19+
# then copy all (non-ignored) project files into the image
1420
FROM base AS dev
15-
COPY --from=deps /app/node_modules ./node_modules
21+
COPY --from=install /temp/dev/node_modules node_modules
1622
COPY . .
1723

18-
FROM base AS runner
19-
ENV NODE_ENV production
20-
COPY --from=prod-deps /app/node_modules ./node_modules
24+
# copy node_modules from temp directory
25+
# then copy all (non-ignored) project files into the image
26+
FROM base AS prerelease
27+
COPY --from=install /temp/dev/node_modules node_modules
2128
COPY . .
22-
23-
ENV PORT 3000
24-
25-
EXPOSE ${PORT}
26-
27-
# set hostname to localhost
28-
ENV HOSTNAME "0.0.0.0"
29-
30-
# server.js is created by next build from the standalone output
31-
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
32-
CMD ["node", "."]
29+
ENV NODE_ENV=production
30+
RUN bun build --target=bun . --outdir build/
31+
32+
# copy production dependencies and source code into final image
33+
FROM base AS release
34+
COPY --from=install /temp/prod/node_modules node_modules
35+
COPY --from=prerelease /usr/src/app/build/server/index.js .
36+
COPY --from=prerelease /usr/src/app/package.json .
37+
38+
# run the app
39+
USER bun
40+
EXPOSE 3000/tcp
41+
ENTRYPOINT [ "bun", "index.js" ]

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ This is a [Redis](https://redis.io/) starter template for JS and [Node](https://
55

66
## Requirements
77

8-
- [node>=22](https://nodejs.org/)
8+
- [bun](https://bun.sh/)
99
- [docker](https://www.docker.com/)
10-
- Optional
10+
- Optional
1111

1212
## Getting started
1313

@@ -46,7 +46,7 @@ You should have a server running on `http://localhost:<port>` where the port is
4646
There are some tests in the `__tests__` folder that can be run with the following command:
4747

4848
```bash
49-
npm test
49+
bun test
5050
```
5151

5252
These tests setup and teardown on their own. You can modify them if you want to leave data in Redis.
@@ -56,17 +56,17 @@ These tests setup and teardown on their own. You can modify them if you want to
5656
To run the development server outside of docker:
5757

5858
```bash
59-
npm install
59+
bun install
6060
# then
61-
npm run dev
61+
bun dev
6262
```
6363

6464
## Other Scripts
6565

6666
Formatting code:
6767

6868
```bash
69-
npm run format
69+
bun format
7070
```
7171

7272
## Connecting to Redis Cloud

__tests__/todos.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
describe,
66
expect,
77
test,
8-
} from "vitest";
8+
} from "bun:test";
99
import request from "supertest";
1010
import app from "../server/app.js";
1111
import * as todos from "../server/components/todos/store.js";

bun.lock

+286
Large diffs are not rendered by default.

bun.pretest.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "dotenv/config";

bunfig.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
preload = ["./bun.pretest.js"]

compose.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ services:
1818
context: ./
1919
dockerfile: ./Dockerfile
2020
target: dev
21-
command: npm run dev
21+
command: bun dev
2222
ports:
2323
- 3000:3000
2424
env_file:
2525
- ./.env
2626
- ./.env.docker
2727
volumes:
28-
- ./server:/app/server
29-
- /app/node_modules
28+
- ./server:/usr/src/app/server
29+
- /usr/src/app/node_modules
3030
restart: always
3131
depends_on:
3232
- redis

0 commit comments

Comments
 (0)