Question

Consider the following two files:

config.json

{
  "vendorFiles": [
    "vendor/angular/angular.js",
    "vendor/angular-ui-router/release/angular-ui-router.js",
    "vendor/angular-ui-utils/modules/utils.js"
  ]
}

gulpfile.js

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    config = require("./config");

gulp.task("build-vendor", function() {
    gulp.src(config.vendorFiles)
        .pipe(concat("vendor.js"))
        .pipe(gulp.dest("build"));
});

How can I eliminate the need to specify vendor/ for each file in config.json? That file is one that is manually edited by other developers by hand, so I want to make it as hassle-free on them as possible.

Ideally I'd like my gulpfile.js to take care of adding that prefix (somehow), and for my config.json to look like this:

{
  "vendorFiles": [
    "angular/angular.js",
    "angular-ui-router/release/angular-ui-router.js",
    "angular-ui-utils/modules/utils.js"
  ]
}
Was it helpful?

Solution

There may be a better way with a Gulp specific solution, but this should work.

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    config = require("./config");

gulp.task("build-vendor", function() {
    gulp.src(config.vendorFiles.map(function(a) {return 'vendor/' + a}))
        .pipe(concat("vendor.js"))
        .pipe(gulp.dest("build"));
});

Demo: http://jsfiddle.net/AK4tP/

OTHER TIPS

Can't you just do

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    config = require("./config");

gulp.task("build-vendor", function() {
    gulp.src(config.vendorFiles, {root: 'vendor/'})
        .pipe(concat("vendor.js"))
        .pipe(gulp.dest("build"));
});

Gulp should accept root option in src() although it's not documented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top