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))

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Where should this sources.json be? in gulp folder or in project root?
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.
A word of warning: watch out when using this method with gulp watch: stackoverflow.com/a/29596396/4494577

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.