Heroku: How to reduce a .NET Core/ Angular app slug size?

374 views Asked by At

I'm getting Heroku warnings about my slug size being too big:

-----> Compressing...
   Done: 304.9M
-----> Launching...
 !     Warning: Your slug size exceeds our soft limit (304 MB) which may affect boot time.

How can I reduce my slug size? It's a .NET Core 2.1.401 and Angular 6 app, so I'm using the following buildpacks:

Running heroku run "du . -h --max-depth=3 --threshold=1M" yields the following result:

2.5M    ./App/obj
524M    ./App/ClientApp/node_modules
16M     ./App/ClientApp/dist
9.4M    ./App/ClientApp/src
549M    ./App/ClientApp
2.5M    ./App/bin/Release
3.3M    ./App/bin
556M    ./App
16M     ./heroku_output/ClientApp/dist
16M     ./heroku_output/ClientApp
130M    ./heroku_output
189M    ./.heroku/dotnet/sdk
140M    ./.heroku/dotnet/shared
329M    ./.heroku/dotnet
27M     ./.heroku/node/lib
3.0M    ./.heroku/node/include
34M     ./.heroku/node/bin
63M     ./.heroku/node
391M    ./.heroku
11M     ./.apt/usr/lib
3.3M    ./.apt/usr/include
15M     ./.apt/usr
15M     ./.apt
1.1G    .

The bulk is in node_modules/ (524M), ./heroku/dotnet/ (329M) and ./heroku_output (130M).

I don't think I can use a .slugignore file, because the node_modules folder is required for the build process, so I tried deleting the node_modules/ folder after dotnet publish using this entry in the app's .csproj, but it didn't reduce the slug size.

<Target Name="CleanupNodeModules" AfterTargets="Publish">
  <Exec WorkingDirectory="$(SpaRoot)" Command="rm -rf node_modules" Condition=" '$(OS)' != 'Windows_NT' " ConsoleToMSBuild="true" />
</Target>

Any ideas?

2

There are 2 answers

0
dstj On BEST ANSWER

I created a pull request to build the .NET Core project as a self-contained exe and delete the .NET Core SDK afterwards.

With that, I passed from a 304MB to a 149MB slug size!

Looks like node_modules has no effect...

2
Maxim Saplin On

You might want to use IL Linker to further reduce the size of .NET Core output. Essentially it'll check for unused dependencies and remove those class libraries with unreachable code. I saw 30-50% reduction in size on self-contained .NET Core console app in macOS and Windows.