|
| 1 | +--- |
| 2 | +title: "Bundling" |
| 3 | +description: "An overview of `deno bundle` subcommand that can be used to produce a single file application created from multiple source files for optimized execution." |
| 4 | +--- |
| 5 | + |
| 6 | +:::caution |
| 7 | + |
| 8 | +This is an experimental feature and requires Deno `2.4.0` or newer. |
| 9 | + |
| 10 | +::: |
| 11 | + |
| 12 | +The `deno bundle` command outputs a single JavaScript file with all |
| 13 | +dependencies. |
| 14 | + |
| 15 | +`deno bundle` is powered by [ESBuild](https://esbuild.github.io/) under the |
| 16 | +hood. |
| 17 | + |
| 18 | +This tool is useful for deploying or distributing a project as a single |
| 19 | +optimized JS file. |
| 20 | + |
| 21 | +## Supported features |
| 22 | + |
| 23 | +- Resolves and inlines all dependencies |
| 24 | +- Supports JSX/TSX, TypeScript, and modern JavaScript, including |
| 25 | + [import attributes](/runtime/fundamentals/modules/#import-attributes) and CSS |
| 26 | +- Optional minification (`--minify`) and source maps (`--sourcemap`) |
| 27 | +- Code splitting |
| 28 | +- Platform targeting (`--platform`, supports Deno and browser) |
| 29 | +- JSX support when configured |
| 30 | + |
| 31 | +## Basic example |
| 32 | + |
| 33 | +```ts, title="main.ts" |
| 34 | +import chalk from "npm:chalk"; |
| 35 | +
|
| 36 | +console.log(chalk.red("Hello from `deno bundle`!")); |
| 37 | +``` |
| 38 | + |
| 39 | +```bash |
| 40 | +$ deno bundle main.ts > bundle.js |
| 41 | + |
| 42 | +# Or with an explicit output file: |
| 43 | + |
| 44 | +$ deno bundle -o bundle.js main.ts |
| 45 | +``` |
| 46 | + |
| 47 | +Above invocation produces a single `bundle.js` file that contains all the |
| 48 | +dependencies, resulting in a self-contained application file: |
| 49 | + |
| 50 | +```bash |
| 51 | +$ deno bundle.js |
| 52 | +Hello from `deno bundle`! |
| 53 | +``` |
| 54 | + |
| 55 | +You can use JSR, npm, http(s) and local imports in the file you are bundling, |
| 56 | +`deno bundle` will take care of collecting all the sources and producing a |
| 57 | +single ooutput file. |
| 58 | + |
| 59 | +## Options Overview |
| 60 | + |
| 61 | +| Flag | Description | |
| 62 | +| ----------------------- | ---------------------------------------------------- | |
| 63 | +| `-o`, `--output <file>` | Write bundled output to a file | |
| 64 | +| `--minify` | Minify the output for production | |
| 65 | +| `--format <format>` | Output format (`esm` by default) | |
| 66 | +| `--code-splitting` | Enable code splitting | |
| 67 | +| `--platform <platform>` | Bundle for `browser` or `deno` (default: `deno`) | |
| 68 | +| `--sourcemap` | Include source maps (`linked`, `inline`, `external`) | |
| 69 | +| `--watch` | Automatically rebuild on file changes | |
| 70 | +| `--inline-imports` | Inline imported modules (`true` or `false`) | |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Bundle a React page for the web |
| 75 | + |
| 76 | +Start with an `app.jsx` and `index.html`: |
| 77 | + |
| 78 | +```jsx |
| 79 | +import React from "npm:react"; |
| 80 | +import { createRoot } from "npm:react-dom/client"; |
| 81 | + |
| 82 | +function App() { |
| 83 | + return <h1>Hello, React!</h1>; |
| 84 | +} |
| 85 | + |
| 86 | +const root = createRoot(document.getElementById("root")); |
| 87 | +root.render(<App />); |
| 88 | +``` |
| 89 | + |
| 90 | +```html |
| 91 | +<!DOCTYPE html> |
| 92 | +<html lang="en"> |
| 93 | + <body> |
| 94 | + <div id="root"></div> |
| 95 | + <script type="module" src="/bundle.js"></script> |
| 96 | + </body> |
| 97 | +</html> |
| 98 | +``` |
| 99 | + |
| 100 | +Now, let's bundle: |
| 101 | + |
| 102 | +```bash |
| 103 | +$ deno bundle --platform=browser app.jsx -o bundle.js |
| 104 | +⚠️ deno bundle is experimental and subject to changes |
| 105 | +Bundled 9 modules in 99ms |
| 106 | + app.bundle.js 874.67KB |
| 107 | +``` |
| 108 | + |
| 109 | +At this point, we're ready to serve our page, let's use |
| 110 | +[`@std/http/file-server` from JSR](https://jsr.io/@std/http/file-server) to |
| 111 | +serve our app: |
| 112 | + |
| 113 | +```bash |
| 114 | +$ deno run -ENR jsr:@std/http/file-server |
| 115 | +Listening on http://127.0.0.1:8000 |
| 116 | +``` |
| 117 | + |
| 118 | +Visiting the page in your browser should show: |
| 119 | + |
| 120 | + |
0 commit comments