{"id":26179983,"url":"https://github.com/jellydn/bun-dx-tips","last_synced_at":"2025-04-14T22:43:24.764Z","repository":{"id":199019956,"uuid":"702008653","full_name":"jellydn/bun-dx-tips","owner":"jellydn","description":"Embark on a streamlined journey with Bun, unraveling a collection of simple yet impactful tips to elevate your development experience","archived":false,"fork":false,"pushed_at":"2025-03-20T01:04:28.000Z","size":87,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T10:50:31.019Z","etag":null,"topics":["bun","debugging","docker","environment-variables","monorepo","traefik","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jellydn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jellydn"],"ko_fi":"dunghd","buy_me_a_coffee":"dunghd","polar":"jellydn"}},"created_at":"2023-10-08T08:21:05.000Z","updated_at":"2025-03-20T01:04:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"efdc68f7-5785-4dfd-95e9-2ae7f9761b52","html_url":"https://github.com/jellydn/bun-dx-tips","commit_stats":{"total_commits":26,"total_committers":3,"mean_commits":8.666666666666666,"dds":"0.46153846153846156","last_synced_commit":"5299f8b71d022205955ac792a86193b6b1b6b907"},"previous_names":["jellydn/bun-tips","jellydn/bun-dx-tips"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fbun-dx-tips","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fbun-dx-tips/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fbun-dx-tips/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jellydn%2Fbun-dx-tips/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jellydn","download_url":"https://codeload.github.com/jellydn/bun-dx-tips/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975305,"owners_count":21192197,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bun","debugging","docker","environment-variables","monorepo","traefik","typescript"],"created_at":"2025-03-11T21:54:02.455Z","updated_at":"2025-04-14T22:43:24.745Z","avatar_url":"https://github.com/jellydn.png","language":"TypeScript","funding_links":["https://github.com/sponsors/jellydn","https://ko-fi.com/dunghd","https://buymeacoffee.com/dunghd","https://polar.sh/jellydn","https://paypal.me/dunghd","https://www.buymeacoffee.com/dunghd"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eWelcome to bun-dx-tips 👋\u003c/h1\u003e\n\u003cp\u003e\n  A collection of simple tips for optimizing your experience with Bun. This repository encapsulates practical snippets, configurations, and insights to make your Bun projects more efficient and enjoyable.\n\u003c/p\u003e\n\n[![IT Man - A Dive into Environment Variables, Debugging, and Workspaces with Bun [Vietnamese]](https://i.ytimg.com/vi/BxmuOe1eFOo/hqdefault.jpg)](https://www.youtube.com/watch?v=BxmuOe1eFOo)\n\n## Table of Contents\n\n- [Environment Variables](#environment-variables)\n- [Debugging Bun](#debugging-bun)\n- [Workspaces](#workspaces)\n\n## Environment variables\n\n### Automatic Loading of .env Files\n\nBun auto-loads the following `.env` files (in order of increasing precedence):\n\n1. `.env`\n2. `.env.production`, `.env.development`, `.env.test` (depending on `NODE_ENV` value)\n3. `.env.local`\n\nExample `.env` file:\n\n```plaintext\nPORT=3000\n```\n\n### Setting Environment Variables\n\n- **Via Command Line:**\n  ```bash\n  PORT=3000 bun run dev\n  ```\n\n### Reading Environment Variables\n\nAccess current environment variables via `process.env` or `Bun.env`.\n\n```javascript\nprocess.env.API_TOKEN; // =\u003e \"secret\"\nBun.env.API_TOKEN; // =\u003e \"secret\"\n```\n\nTo print all set environment variables to the command line, run `bun run env`.\n\n### Environment Variable Expansion\n\nConstruct compound values by referencing previously-defined variables.\n\n```plaintext\n.env\nPORT=3000\nSERVER_URL=http://localhost:${PORT}\n```\n\nDisable expansion by escaping the `$` with a backslash.\n\n### TypeScript Support\n\nUtilize interface merging for autocompletion and non-optional string treatment.\n\n```typescript\ndeclare module \"bun\" {\n  interface Env {\n    PORT: number;\n    SERVER_URL: string;\n  }\n}\n```\n\nAdd the above line to any file in your project for global augmentation of `process.env` and `Bun.env`.\n\n## Debugging Bun\n\nDebugging in Bun is facilitated through the WebKit Inspector Protocol. Use the `--inspect` flag when running your Bun code to enable debugging. This section provides a step-by-step guide on how to debug Bun effectively using the web debugger and other tools.\n\n### Enabling Debugging\n\n1. **Flag Usage:**\n   Run your Bun file with the `--inspect` flag to start the debugging session.\n\n   ```bash\n   bun --inspect server.ts\n   ```\n\n2. **Web Debugger:**\n   Navigate to [debug.bun.sh](https://debug.bun.sh) to access Bun's web-based debugger, which is a modified version of WebKit's Web Inspector Interface.\n\n### Debugging Session\n\n1. **Setting Breakpoints:**\n   Within the Sources tab of the debugger, set breakpoints by clicking on the desired line number.\n\n2. **Inspecting Variables:**\n   Inspect the local variables, control program execution, and run arbitrary code in the console at the breakpoint.\n\n3. **Control Flow:**\n   Utilize control flow buttons to step over, step into, or continue script execution.\n\n4. **Viewing Logs:**\n   Console logs and errors will be displayed in the Console tab, aiding in identifying issues.\n\n## Workspaces\n\nBun facilitates monorepo management through the `workspaces` feature in `package.json`. This feature eases the development of complex software by organizing it into multiple independent packages within a single repository (monorepo).\n\nA typical monorepo structure looks like this:\n\n```plaintext\n\u003croot\u003e\n├── README.md\n├── bun.lockb\n├── server.ts\n├── package.json\n├── tsconfig.json\n└── packages\n    ├── logger\n    │   ├── index.ts\n    │   ├── package.json\n    │   └── tsconfig.json\n    ├── db\n    │   ├── index.ts\n    │   ├── package.json\n    │   └── tsconfig.json\n    └── mailer\n        ├── index.ts\n        ├── package.json\n        └── tsconfig.json\n```\n\nIn the root `package.json`, the `workspaces` key specifies the subdirectories to be treated as packages or workspaces within the monorepo. Conventionally, all workspaces are housed in a directory named `packages`.\n\n```json\n{\n  \"name\": \"my-project\",\n  \"version\": \"1.0.0\",\n  \"workspaces\": [\"packages/*\"],\n  \"devDependencies\": {\n    \"@productsway/logger\": \"workspace:*\",\n    \"@productsway/db\": \"workspace:*\",\n    \"@productsway/mailer\": \"workspace:*\"\n  }\n}\n```\n\n### Features\n\n- **Glob Support:**\n  Bun accommodates simple `\u003cdirectory\u003e/*` globs in `workspaces`. However, the full glob syntax (e.g., `**` and `?`) is not supported at the moment.\n\n- **Versioning:**\n  Utilize `workspace:*` in the `dependencies` section of your `package.json` when referencing other packages within the monorepo.\n\n```json\n{\n  \"name\": \"@productsway/mailer\",\n  \"version\": \"1.0.0\",\n  \"dependencies\": {\n    \"@productsway/db\": \"workspace:*\"\n  }\n}\n```\n\n### Benefits\n\n1. **Modular Code Organization:**\n   Split code into logical parts. For dependencies within the monorepo, simply add them as dependencies in `package.json`. For instance, if package `mailer` depends on package `db`, `bun install` will link to your local `packages/db` directory instead of fetching it from the npm registry.\n\n2. **Dependency De-duplication:**\n   Common dependencies between `mailer` and `db` are hoisted to the root `node_modules` directory, reducing disk usage and mitigating dependency conflicts.\n\n3. **⚡️ Speedy Installs:**\n   Enjoy fast installations, even for large monorepos. Check out [benchmark](https://github.com/oven-sh/bun/tree/main/bench/install) for a detailed benchmarking report.\n\n## 📚 Resources\n\n- [Environment variables – Runtime | Bun Docs](https://bun.sh/docs/runtime/env)\n- [Debugging Bun with the web debugger | Bun Examples](https://bun.sh/guides/runtime/web-debugger)\n- [Workspaces – Package manager | Bun Docs](https://bun.sh/docs/install/workspaces)\n- [Performance Best Practices Using Express in Production](https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production)\n- [Working on Multiple Web Projects with Docker Compose and Traefik | gpk blog](https://georgek.github.io/blog/posts/multiple-web-projects-traefik/)\n- [Bun playlist on my channel](https://www.youtube.com/playlist?list=PLOdXIcVPTyB91lDwMvH68QdxyMMKzgQLC)\n\n## Author\n\n👤 **Huynh Duc Dung**\n\n- Website: https://productsway.com/\n- Twitter: [@jellydn](https://twitter.com/jellydn)\n- Github: [@jellydn](https://github.com/jellydn)\n\n## 🙏 Show Your Support\n\nIf you found this project helpful, consider giving it a ⭐️!\n\n[![kofi](https://img.shields.io/badge/Ko--fi-F16061?style=for-the-badge\u0026logo=ko-fi\u0026logoColor=white)](https://ko-fi.com/dunghd)\n[![paypal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge\u0026logo=paypal\u0026logoColor=white)](https://paypal.me/dunghd)\n[![buymeacoffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge\u0026logo=buy-me-a-coffee\u0026logoColor=black)](https://www.buymeacoffee.com/dunghd)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellydn%2Fbun-dx-tips","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjellydn%2Fbun-dx-tips","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjellydn%2Fbun-dx-tips/lists"}