WittCode💻

Bundling Server Side Code

By

Learn the pros and cons of bundling server side code.

Table of Contents 📖

Bundling Code

Client side code is frequently bundled to decrease the size of the code that is sent over the wire from the server to the client. On the other hand, server side code isn't often bundled as it is not sent to the client. The only time it would travel over the wire would be when the code is being uploaded/deployed to a server.

Why Bundle Server Code

Bundling server side code is useful for reducing the size of the code on the server. This is particularly useful for serverless systems where, in AWS for example, AWS Lambdas are constantly being deployed or cold starting.

INFO: A cold start is the delay that occurs when a serverless function is invoked for the first time or after a prolonged idle period.

Another reason is that it reduces the amount of files that need to be managed. Often these bundles will consist of a single script file that can be ran on its own. A common, though now outdated, reason to not bundle server code is debugging. Bundling server side code used to make the debugging process more difficult. However, source maps in modern bundlers and languages negate this.

WARNING: Source maps give real stack traces and can be used to debug server code.

node --enable-source-maps ./dist/server.cjs

Why NOT Bundle Server Code

Nevertheless, not everyone is onboard with bundling server side code. The main reason being that the benefits that it provides to front end apps are not mimicked on the backend. In other words, bundling server side code just adds extra complexity and an extra build step. Also, when generating source maps, these source maps also need to be deployed along with the bundle to the server.