Linking assets in express js depending on environment

162 views Asked by At

Is there a way of linking assets in my html files (to be precise: nunjucks files in my case) depending on the environment? I want to include several partial *.css or *.js files in the dev env in order to debug it easier and one concatanated *.min.css and *.min.js file in prod env (kind of like with assetic in Symfony).

2

There are 2 answers

2
vernak2539 On BEST ANSWER

You would probably need to do this via a build process. Good task runners include Grunt and Gulp.

You could use something like grunt-usemin. This would allow you to put direct script/link tags in your html, then run a grunt command to combine them.

2
cyberwombat On

It would vary based on your template system but the basics are like this:

In controller - make the env available to your template:

res.render("template", {
 env: process.env.NODE_ENV || 'development'
});

Template:

{% if env === 'development' %}
<script>....</script>
<script>....</script>   
{% else %}
<script src="prod.min.js"></script>
{% endif %}

In addition use the answer by @vernak2539 to build your minify your prod.min.js file using gulp or grunt and the minify/uglify/concat plugins or do it 'manually' using things like CodeKit.