Your First Useful Gulp Task. So that was fun, but not particularly worthwhile. Let’s do something useful with Gulp. Let’s say you have a directory full of JavaScript files, a directory called ‘js’.
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.
Gulp является системой потоковой сборки, используя файловые потоки когда какие-либо файлы в source/ javascript с расширением. js. Simple build env for game dev with Gulp under 5 min. 18.12.2013 Easy JavaScript Documentation with YUIDocs and Grunt. js Task automation with npm run. Автоматизация оптимизаций с помощью Gulp ( Automating optimisations with Часы (на CSS-анимации и JavaScript); en Создание игры Pacman с помощью Bacon. js ( Building a Pacman Game With Bacon. js); en. Active position on the development direction (Yii, MySQL, jQuery, WordPress, javascript, Node. js) - Automating build systems (grunt, gulp) - Leading role on.
Gulp является системой потоковой сборки, используя файловые потоки когда какие-либо файлы в source/ javascript с расширением. js.
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.
Write command-line and web server applications in JavaScript with Node. js, Use build tools, such as Grunt and Gulp, to automate repetitive tasks and improve.
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.
Instead of executing a function, default will now execute a list of sub-tasks. For now, we just want it to execute our copy-js task. Now, if you type gulp into the terminal, any JavaScript files in the ‘js’ directory will be copied into the ‘build’ directory.
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.
Also, we put the JavaScript directory glob into a separate var called jsDir. which we use in both the copy-js and watch-js task.
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.