WIP: test esbuild and rollup as bundlers for backend#742
WIP: test esbuild and rollup as bundlers for backend#742gmaclennan wants to merge 1 commit intodevelopfrom
Conversation
|
Just adding another reason for this: noderify does not support source maps, which limits the utility of error reporting through bugsnag etc. When debugging locally I've ended up scrolling through the bundle to figure out which module the error is in. I think this would be worth picking up again, and shifting to ES modules for the backend, but I think sticking with JSDoc for typescript is working well, particularly as it simplifies the test runner setup for backend tests. |
|
On a recent long train journey I did some further hacking on this using ESBuild. ESBuild is very fast, but it does not give as much control over code transforms as I think Rollup might, and seems to have fewer examples of bundling Node code. Resolving |
|
As a side-note, bundle analysis shows up a lot of duplicate modules (caused by multiple dependencies requiring the same module but slightly different versions). This is significant for modules such as AJV, which is large, and is duplicated 5-6 times in the bundle. Some of this might be resolved by updating npm or using pnpm, and updating the package-lock to attempt to deduplicate deps. A thorough bundle review and cleanup (perhaps after the Mapeo Core update) could reduce the bundle size (and hence APK size) and also reduce the initial parse time at app load. |
We currently bundle the backend JS code into a single file using noderify. We do this because loading the unbundled code takes time, especially on low-powered devices like phones.
This PR is an experiment to use a different bundler for backend code. It currently includes build scripts to use esbuild and rollup. The JS bundle has not yet been tested, and probably needs some changes for native modules that use
node-gyporbindingse.g. see https://github.com/staltz/bindings-noderify-nodejs-mobileWe do not need to change the build tool, but there are some reasons to consider it:
debugandreadable-streammodules are duplicated multiple times in the bundle, which adds to the bundle size (and parse time). They are duplicated becausenpmwill only dedupe the most recent version of a dependency - sub-dependencies which are older versions are not deduped e.g. Mapeo dependencies usedebugv4, v3 and v2, but only v4 is deduped.Based on this experiment with esbuild and rollup, I found esbuild to be easier to learn and implement. Rollup required learning about the plugin ecosystem and reading the docs for each plugin, whereas I found the esbuild config easier to learn from the docs. Esbuild was much faster than Rollup, and produced a smaller bundle. This needs more investigation though and some quantitative analysis.
In parallel to this work, we should consider a bundler for the backend code for Mapeo Desktop. Currently we ship the Desktop backend code unbundled, and the shipped
node_modulesfolder is a significant proportion of the app download size. Bundling could significantly reduce the app size, and potentially increase startup time. It would be simpler if we use the same bundler for mobile and desktop.