14

I know it's a basic question, but I couldn't find a proper answer.

Is there a way of storing a list of my project's source files in a JSON file and load it on gulpfile.js? For example, instead of doing:

gulp.src(['a.js', 'b.js'])

Do something like:

var sources = some_load_file_func('sources.json');

gulp.src(sources.js_files))
Tzach
  • 12,889
  • 11
  • 68
  • 115

1 Answers1

29

A gulpfile is just node, and in node you can simply use require on JSON files, like so:

var sources = require('sources.json');

Now sources will be an object (or whatever is in your JSON file).

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • 1
    Where should this sources.json be? in gulp folder or in project root? – Koustuv Sinha Dec 24 '14 at 08:28
  • 16
    The `require` function is always relative to the file you are in, or it will look in `node_modules`. Th answer should have been `require('./sources.json')`, and the file next to wherever you need it, or adjust the path accordingly. – OverZealous Dec 24 '14 at 13:45
  • A word of warning: watch out when using this method with `gulp watch`: http://stackoverflow.com/a/29596396/4494577 – jannis Mar 23 '17 at 11:22