Jump to content

Including content of JS file into gulpfile.js


Recommended Posts

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

Link to comment
Share on other sites

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 by Adam
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.