0

I'm banging my head on this one;

I want to alter my html file based on development-environment or production. I followed a topic here on Stackoverflow but somehow the preprocess-context arguments are always the last one in the files. Okay I'll include some snippets.

Piece of Grunt.js

    //Grunt task to automate environment configuration for future tasks.
    env : {
        //USER : 'raymond',
        //PATH : '/bin:/usr/bin',

        options : {
            /* Shared Options Hash */
            //globalOption : 'foo'
        },
        dev: {
            NODE_ENV : 'DEVELOPMENT'
        },
        prod : {
            NODE_ENV : 'PRODUCTION'
        }
    },
    //Grunt task around preprocess npm module.
    preprocess : {
        options: {
            context : {
                DEBUG       : true,
                now         : '<%= grunt.template.today() %>',//@TODO .toISOString()
                ver         : '<%= pkg.version %>'
            }
        },
        dev : {
            options : {
                context : {
                    url         : 'http://www.<%= pkg.name %><%= pkg.tld.dev %>',
                    concat      : '<%= pkg.name %><%= pkg.tld.dev %>.concat.js',
                    uglify      : '<%= pkg.name %><%= pkg.tld.dev %>.concat.min.js',
                    css         : '<%= pkg.name %><%= pkg.tld.dev %>.css',
                    cssMin      : '<%= pkg.name %><%= pkg.tld.dev %>.min.css'
                }
            },
            src : 'index.html',
            dest : 'dist/dev/index.html'
            // @TODO write to dist/dev-directory fails cause probably permissions checked it;
            // I have to 777 on www-data to run Nginx properly. Strange cause raymond is part of group www-data

        },
        prod : {
            options : {
                context : {
                    url         : 'http://www.<%= pkg.name %><%= pkg.tld.prod %>',
                    concat      : '<%= pkg.name %><%= pkg.tld.prod %>.concat.js',
                    uglify      : '<%= pkg.name %><%= pkg.tld.prod %>.concat.min.js',
                    css         : '<%= pkg.name %><%= pkg.tld.prod %>.css',
                    cssMin      : '<%= pkg.name %><%= pkg.tld.prod %>.min.css'
                }
            },
            src : '<%= preprocess.dev.src %>',
            dest : 'dist/prod/index.html'
        }
    },

Source index.html

    <!-- @if NODE_ENV == 'DEVELOPMENT' -->
    <script type="text/javascript" src="/js/vendor/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="/js/vendor/jquery.hammer-1.0.6.js"></script>
    <script type="text/javascript" src="/js//vendor/snap.svg-0.2.0.js"></script>
    <script type="text/javascript" src="/js/vendor/helper.js"></script>
    <script type="text/javascript" src="/js/plugins.js"></script>
    <script type="text/javascript" src="/js/myradon.js"></script>
    <script type="text/javascript" src="/js/myradon-snap.js"></script>
    <!--
    <script src="<!-- @echo url -->/js/<!-- @echo concat -->"></script>
    <script src="<!-- @echo url -->/js/<!-- @echo uglify -->"></script>
    -->
    <!-- @endif -->
    <!-- @if NODE_ENV == 'PRODUCTION' -->
    <!-- <script src="<!-- @echo url -->/js/<!-- @echo concat -->"></script> -->
    <script src="<!-- @echo url -->/js/<!-- @echo uglify -->"></script>
    <script>
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-5427260-3']); //www.myradon.net = UA-5427260-2, www.myradon.nl = UA-5427260-3, www2.myradon.net = UA-5427260-4
        _gaq.push(['_trackPageview']);
        (function () {
            var ga = document.createElement('script');
            ga.type = 'text/javascript';
            ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(ga, s);
        })();
    </script>
    <!-- @endif -->
</body>

This is the output of index.html that gets rendered by preprocess:dev;

    <!-- <script src="http://www.myradon.net/js/myradon.net.concat.js"></script> -->
    <script src="http://www.myradon.net/js/myradon.net.concat.min.js"></script>
    <script>
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-5427260-3']); //www.myradon.net = UA-5427260-2, www.myradon.nl = UA-5427260-3, www2.myradon.net = UA-5427260-4
        _gaq.push(['_trackPageview']);
        (function () {
            var ga = document.createElement('script');
            ga.type = 'text/javascript';
            ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(ga, s);
        })();
    </script>
</body>

As you can see @if NODE_ENV == 'DEVELOPMENT' is ignored but the context arguments from preprocess:dev are passed to @if NODE_ENV == 'PRODUCTION' This really makes no sence to me. Do you have a clue?!

myradon
  • 421
  • 1
  • 4
  • 13
  • I'm assuming you have checked the value of the `NODE_ENV` environment variable on the system you are running this code on? Is it set to `DEVELOPMENT`? Can you update the question with the output of that for verification? – Jordan Kasper Jan 28 '14 at 17:33
  • I don't understand what you mean. Nor the Github grunt-env examples help me out. Hihih...Could you level with a Grunt/Node-noob? EDIT: Grunt.js snippet is in the OP. – myradon Jan 28 '14 at 17:38
  • So the `NODE_ENV` variable is an environment variable that is set on your system, not in Grunt or Node per se. You can see the value of this variable on the command line using: `echo $NODE_ENV` (Mac or Linux) or `echo %NODE_ENV%` for Windows. If it is blank (not set) then you need to set it ([Windows](http://www.computerhope.com/issues/ch000549.htm), [Mac/Linux](http://www.cyberciti.biz/faq/set-environment-variable-linux/)). – Jordan Kasper Jan 28 '14 at 18:57
  • You wont be suprised when I'm going to say 'echo $NODE_ENV' as root doesn't output anything! – myradon Jan 28 '14 at 19:09
  • :) I am not. So, I think you'll need to set that before the `preprocess` task does anything. Of course, I don't know why it's showing the "PRODUCTION" block... seems like if the variable is blank it shouldn't be... – Jordan Kasper Jan 28 '14 at 19:18
  • 1
    If you just need to generate both results, can you just run `grunt preprocess:dev` and `grunt preprocess:prod` separately? Do you even need the environment variable?? – Jordan Kasper Jan 28 '14 at 19:20
  • Maybe because these conditions are sort of ignored and PRODUCTION is last one in index.html. Do you mean it is a system wide setting OR run prod or dev?! I just want Grunt to spit out production index.html, which I will upload to hosting provider on tld= .nl and development is for dev-webserver on tld= .net. – myradon Jan 28 '14 at 19:21
  • Do you even need the environment variable? I believe I don't BUT I have to have conditions in HTML! So no NODE_ENV but then what?! like '' I'm actually thinking about using them if possible in my variables.less to reflect prod-domain and dev-domain – myradon Jan 28 '14 at 19:27

2 Answers2

1

Okay guys i'm using Grunt-processhtml. It does exactly what I had in mind. you can set variables in a context. Remove blocks of html, set url's or metatags innerHTML based on envirenment really nice. Only downside, maybe there is a setting for it, it leaves html-comment-markup for processing the html-code.

myradon
  • 421
  • 1
  • 4
  • 13
0

I don't know if it matters but I use single '=' just like per example at: grunt-preprocess on npm

<!-- @if NODE_ENV='production' -->
<script src="some/production/lib/like/analytics.js"></script>
<!-- @endif -->
beltrone
  • 100
  • 1
  • 1
  • 8