Django on Heroku: installing NodeJS and Less for static assets compilation
Heroku provides a bunch of different buildpacks that target many popular
platforms like Python, Ruby, NodeJS and Java web apps and backends.
While this is great and allows you to deploy virtually anything with a simple
git command, the out-of-the-box solutions offer a limited set of utilities
that are available during the Slug compilation phase.
In particular, no NodeJS, NPM or LESS Compiler
is available in the heroku-buildpack-python
. This means that there
is no straightforward way of compiling .less
stylesheets during
the app deployment.
Fortunately, the Python buildpack provides hooks for running pre-compile
and post-compile scripts. This can be used for customizing the compilation
step and running additional commands without the necessity of maintaining
a separate fork of Heroku's buildpack.
The only thing you need to do is to create a proper bin/post_compile
bash script
in the root directory of your application.
Head on to the Django and Heroku Cookbook repository for a set of post-compile scripts.
The bin directory
contains a set of scripts that can be used to install NodeJS/Less and invoke
the manage.py compress
command in your Django application:
Just copy them over to your app reposiory and have your Less stylesheets compiled with an assets compressor like Django Compressor.