Dhanan Dhanan - 6 months ago 14
Node.js Question

Grunt j with regular expressions

grunt.config('sass', {
options: {
sourceMap: true
},
dist: {
options: {
outputStyle: 'compact'
},
files: {
'/css/site.css': 'main/scss/site.scss',
'/css/template.home.css': 'main/scss/templates/home.scss'
'/css/template.contact.css': 'main/scss/templates/contact.scss'
'/css/template.something.css': 'main/scss/templates/something.scss'
}
}
});


is there is any way to handle this part nicely ( Thinking about a pattern but I' not sure about the possibility )

'/css/template.home.css': 'main/scss/templates/home.scss'
'/css/template.contact.css': 'main/scss/templates/contact.scss'
'/css/template.something.css': 'main/scss/templates/something.scss'

Answer

If you declare the config as a JS object you could do it like this:

var cfg = {
        options: {
            sourceMap: true
        },
        dist: {
            options: {
                outputStyle: 'compact'
            },
            files: {
                '/css/site.css': 'main/scss/site.scss',
                '/css/template.home.css': 'main/scss/templates/home.scss',
                '/css/template.contact.css': 'main/scss/templates/contact.scss',
                '/css/template.something.css': 'main/scss/templates/something.scss'
            }
        }
    },
    grunt = {
      config: function() {
        document.write('<br/><strong>grunt config set</string><br/>');
      }
    };

    document.write('<br/>"files" before new config:<br/><br/>');

    for (var property in cfg.dist.files) {
        if (cfg.dist.files.hasOwnProperty(property)) {
            document.write(property + ' = ' + cfg.dist.files[property] + '<br/>');
        }
    }

    document.write('<br/>"files" after added config:<br/><br/>');

    cfg.dist.files['/css/what ever...'] = 'the/latest/path';

    for (var property in cfg.dist.files) {
        if (cfg.dist.files.hasOwnProperty(property)) {
            document.write(property + ' = ' + cfg.dist.files[property] + '<br/>');
        }
    }

    grunt.config('sass', cfg);

Note the

    cfg.dist.files['/css/what ever...'] = 'the/latest/path';

That's where the additional config is added.

Comments