Search code examples
gulpgulp-concat

gulp-concat: multiple external "config" files, is it possible?


Disclaimer: new to node, streams, gulp

I am wanting to update/improve our current build process to use gulp through the frontend-maven-plugin and I would like to know if it is possible to use external config files to tell gulp concat what files to work with?

Currently we are doing this with a bit of ruby and json that lives under /scripts/config/ and gets outputed to a combined folder. There are dozens of these configs, which can grow or shrink release by release.

An example config file looks like so:

{
"outputFileName": "foo.js",
"dependencies":["blah.js","blah.js","blah.js"]
}

All the examples I have seen have been with a predefined hard coded list in the gulp file. I guess what I am really asking is if this functionality exists out of the box or do I need to write a plugin for this, assumes of course that one does not already exist.

Thanks for you time


Solution

  • Yes, loading the json is plain node:

    var gulp   = require('gulp');
    var concat = require('gulp-concat');
    var config = require('./scripts/config/something.json');
    
    gulp.task('scripts', function() {
        return gulp.src(config.dependencies)
            .pipe(concat(config.outputFileName))
            .pipe(gulp.dest('somewhere'));
    });
    

    Example for loading multiple files:

    var gulp       = require('gulp');
    var concat     = require('gulp-concat');
    var requireDir = require('require-dir');
    var config     = requireDir('./scripts/config');
    
    gulp.task('scripts', function() {
        return gulp.src(config.something.dependencies)
            .pipe(concat(config.something.outputFileName))
            .pipe(gulp.dest('somewhere'));
    });