Skip to content

Commit 148e76f

Browse files
Docs: Document desktop app bundling
1 parent 891cb38 commit 148e76f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

readme/dev/spec/desktop_bundling.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Desktop app bundling
2+
3+
For performance and to reduce the application size, the desktop app is bundled with [esbuild](https://esbuild.github.io/). Bundling packs most of the desktop application's JavaScript into one or two JavaScript files. This occurs as a part of both `yarn dist` and `yarn start`.
4+
5+
## Why bundle the app?
6+
7+
- **Performance**: Bundling the app [is recommended by the Electron performance guide](https://www.electronjs.org/docs/latest/tutorial/performance#7-bundle-your-code). The guide states that "Loading modules is a surprisingly expensive operation, especially on Windows." Bundling the application reduces the number of `require` calls.
8+
- **Application size**: Bundling can reduce the size of the app created by `yarn dist`.
9+
10+
## How does bundling reduce the application size?
11+
12+
Bundling allows both:
13+
1. Reducing the size of the JavaScript included in the app through [minification](https://esbuild.github.io/api/#minify), and
14+
2. Reducing the number of dependencies included in `node_modules` in the version of the app built with `electron-builder`. Dependencies often include files unnecessary for the final build (e.g. README images, test files).
15+
16+
17+
## Excluding dependencies from `node_modules`
18+
19+
After bundling the app, most dependencies are completely included within `main.bundle.js` or `main-html.bundle.js`. As a result, the copies of these dependencies in `node_modules` are completely unused.
20+
21+
Some dependencies need to be included in `node_modules` at runtime. This is the case, for example, if the dependency needs to be required with `shim.requireDynamic`, or if the dependency includes native `.node` assets that can't be bundled. For example, `sqlite3` includes native `.node` assets that need to be in `node_modules` at runtime.
22+
23+
Electron-builder can be instructed to exclude dependencies from the built application [by moving them to `devDependencies`](https://github.com/electron-userland/electron-builder/blob/84657680ba5688f1594bc77be3df5c2c78125723/README.md?plain=1#L73). A dependency should only be in the production `dependencies` if it needs to be included in `node_modules` at runtime.
24+
25+
## Determining what contributes to the bundle size
26+
27+
To see what contributes to the size of the application's bundled JavaScript, consider using [esbuild's bundle size analyzer](https://esbuild.github.io/analyze/). The size analyzer accepts esbuild metafiles. To build these metafiles, manually run `yarn bundle`. Bundle metadata will be written to the `app-desktop` directory as `.meta.json` files.

0 commit comments

Comments
 (0)