denno020 Posted June 11, 2016 Share Posted June 11, 2016 I'm having trouble figuring out how to import the contents of one JS file into another JS file - a gulpfile. I've got my gulp file set up and I would like to store an array of file names in a separate JS file, that the gulpfile will load in and pass as the parameter to the src command. The array in the separate JS file will be sort of like configuration. But it will be something that will change, and I don't like the idea of constantly editing an array inside my gulpfile To illustrate what I'm after, this is the folder structure I would have //directory root // - gulpfile.js // - arrayfile.js Then my files would look something like this //arrayfile.js var array = [ 'somefile.js', 'someotherfile.js' ]; //gulpfile.js import arrayfile.js //This is the part that I need to figure out properly gulp.task('javascript', () => { gulp.src(array) //where 'array' comes from arrayfile.js .pipe() //etc }); As you can see, I'm using ES2015, however I haven't been able to get import to work with gulp. What else could I do to be able to use the array defined in arrayfile.js as the src parameter in the gulpfile? The versions I'm using: Gulp cli: 1.2.1 Gulp local version: 3.9.1 Node: 6.2.1 Thanks, Denno Quote Link to comment Share on other sites More sharing options...
Adam Posted June 14, 2016 Share Posted June 14, 2016 (edited) Gulp runs in the node environment, so you can just use node's module loader to import the data within arrayfile, then export it as an array for your Gulp script to import. arrayfile.js: module.exports = { somefile: require('./somefile'), someotherfile: require('./someotherfile') }; gulpfile.js: var data = require('./arrayfile'); console.log(data.somefile); console.log(data.someotherfile); You could export an array from arrayfile.js (the name of which obviously doesn't make much sense when you export an object), however having named properties to access a different file's data feels a little more elegant, and is better for readability. Edited June 14, 2016 by Adam Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.