All these files need to be copied over to a directory called ‘build’ before you can publish your app. No problem! Put this into your gulpfile:. There’s a lot going on here, so I’ll explain bit-by-bit:. We created a new task called copy-js. which will do all our copying for us.
The first line inside that task, beginning with gulp. src. tells gulp where to look for the files we want to copy. That bunch of / s and * s we gave it is a pattern-matching string called a glob. Here’s how to interpret this glob:. The js/ part tells gulp to look inside the directory named ‘js’. The **/ part tells gulp to look inside any subdirectories within the ‘js’ directory.
The *. js part tells gulp to find all files that end with the. js file extension. On the next line, we chain a method onto the end of gulp.
src … specifically, the. pipe() method.
pipe() takes the output of the previous method (i.
the. js files we found) and lets us use it as input for another method, just like a unix pipe. This is extremely useful, as you’ll soon see.
pipe() passes the files we found to gulp. dest(‘build’).
gulp. dest() is used to save files to a particular location. Which location? Why, the one we told it: the ‘build’ directory. Finally (and importantly!) we changed the default task.
Gulp will even create a ‘build’ directory for you if it can’t find one. How thoughtful. “This is all well and good,” you might be thinking, “but how does this actually save me time?” After all, you still have to keep typing gulp into the terminal every time you want this copy and paste to happen, right. The answer is no, you don’t. Gulp can run tasks for you, automatically.
Enter gulp. watch() :. Ok, so what happened here. We made a new task called watch-js. When this task is executed, gulp.
watch() will keep a close eye on the directory we tell it, watching for files inside to change. When they do, the tasks in the array we provide will be executed… in this case, the copy-js task. To put it simply, whenever we change a. js file, it’ll be copied over automatically. How cool is that. We chained. on() to the end of gulp.
watch(). This lets us execute code when certain conditions are met. In this case, when a file changes, we execute a function.
This function uses the event parameter to let us console log which file changed, and how it was changed (added, changed, deleted, etc.
That way, we can make sure it stays consistent. Finally, we change the default task to execute watch-js when it’s called. By the way, you’ll notice this is an array; we can comma-separate multiple sub-task names to be called here, if we choose. Sweet! What Else.
Gulp can help you automate all kinds of development-related tasks, including but not limited to:. Unit / Integration Testing. Bundling / Concatenation.
Minifying / Compression. CSS pre-processor compilation (i. Sass / Less).
Image resizing / processing. Asset versioning. Running shell commands. To learn more, check out Gulp’s documentation and browse their extensive, searchable list of plugins.
To use a plugin, npm install it, require it at the top of your gulpfile as a variable, and then use it based on the plugin’s documentation. Like the following example does with gulp-sass :. That should be enough to get you started. Happy gulping.