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).
Asked
Active
Viewed 158 times
2 Answers
0
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.

vernak2539
- 574
- 3
- 14
-
That's exactly what I was looking for :) Btw, I'm using gulp, so that'll be [gulp-usemin](https://www.npmjs.org/package/gulp-usemin) – jalooc Dec 02 '14 at 12:52
-
Perfect. Moving most of my stuff to gulp. seems like there's always a gulp package for every grunt package. – vernak2539 Dec 02 '14 at 16:46
0
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.

cyberwombat
- 38,105
- 35
- 175
- 251
-
Thanks for that answer. I'd been already using that method, but it always seemed to me not enough suited for that task. @vernak2539 gave right the answer that I expected :) – jalooc Dec 02 '14 at 12:51
-
Great - I use both since I need all my js files separated for dev and minified for prod. I tried using the gulp onlu approach and it makes debugging harder. – cyberwombat Dec 02 '14 at 15:35