Optimizing your server's bundle size and build time

I’m not sure how many people know that with Meteor apps, packages put in your devDependencies are never put into your server-side bundle, but will be put in your client-side bundle.

With larger projects, this can lead to much smaller build times, as your server-bundle will be much smaller.

In our team, we haven’t always added packages properly, and so now I’m trying to clean this up and move lots of client-only dependencies into devDependencies.

Does someone know of a quick way to find these packages that could safely be moved into devDependencies because the server-bundle never imports them anywhere? That would speed up this process, and could maybe even help out a lot of other Meteor projects!

1 Like

This is interesting, why would dev dependencies go into either bundle? I assume you’re talking about prod bundles?

Use recast to parse your sources or bundles and locate all static and dynamic import statements.

1 Like

There is a discussion about this problem.

We have a step in our docker file to remove typescript and some trash in app folder(bundle/programs/server/):

# Remove unused files to reduce image size
RUN find . -type f -name 'README.md' -delete \
    && find . -type f -name 'LICENSE' -delete \
    && find . -type f -name '*.d.ts' -delete \
    && rm -r ./npm/node_modules/meteor/babel-compiler/node_modules/typescript

IFAIK we saved ~50mb.

Another tip to reduce image size is using multi stage docker file. It allows us to cut another 300-400mb

So, the final Dockerfile:

FROM node:14.19.1-stretch-slim as base

ENV APP_DIR=/meteor
# Install as root (otherwise node-gyp gets compiled as nobody)
USER root
WORKDIR $APP_DIR/bundle/programs/server/

RUN apt-get update \
    && apt-get install -y --no-install-recommends g++ build-essential python \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Copy bundle and scripts to the image APP_DIR
ADD ./editor-app.tar.gz $APP_DIR

# Remove unused files to reduce image size
RUN find . -type f -name 'README.md' -delete \
    && find . -type f -name 'LICENSE' -delete \
    && find . -type f -name '*.d.ts' -delete \
    && rm -r ./npm/node_modules/meteor/babel-compiler/node_modules/typescript

# the install command for debian
RUN echo "Installing the node modules..." \
	&& npm install -g node-gyp \
    && npm install --production --silent

FROM node:14.19.1-stretch-slim as app

ENV APP_DIR=/meteor

# Copy in app bundle
COPY --from=base $APP_DIR/bundle $APP_DIR/bundle/

# start the app
WORKDIR /meteor/bundle
CMD ["node","main.js"]