Skip to content

Commit 3bda508

Browse files
authored
Add bundling page (#1866)
1 parent cea2a2a commit 3bda508

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

runtime/_data.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ export const sidebar = [
257257
title: "Testing code in docs",
258258
href: "/runtime/reference/documentation/",
259259
},
260+
{
261+
title: "Bundling",
262+
href: "/runtime/reference/bundling/",
263+
},
260264
{
261265
title: "Lint Plugins",
262266
href: "/runtime/reference/lint_plugins/",

runtime/reference/bundling.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
![Image of serving bundled React app](./images/bundled_react.png)

runtime/reference/cli/bundle.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
---
2-
title: "Bundler (deprecated)"
2+
title: "Bundler"
33
oldUrl: /runtime/manual/cli/bundler/
44
command: bundle
55
openGraphLayout: "/open_graph/cli-commands.jsx"
66
openGraphTitle: "deno bundle"
77
---
88

9-
:::caution
9+
:::info
1010

11-
`deno bundle` has been deprecated and will be removed in some future release.
12-
Use [deno_emit](https://github.com/denoland/deno_emit),
13-
[esbuild](https://esbuild.github.io/) or [rollup](https://rollupjs.org) instead.
11+
`deno bundle` is currently an experimental subcommand and is subject to changes.
1412

1513
:::
1614

13.2 KB
Loading

0 commit comments

Comments
 (0)