codecamp

Custom Tasks and Extensions

Sometimes, you'll want to hook your own Gulp tasks into Elixir. Perhaps you have a special bit of functionality that you'd like Elixir to mix and watch for you. No problem!

As an example, imagine that you have a general task that simply speaks a bit of text when called.

gulp.task("speak", function() {
    var message = "Tea...Earl Grey...Hot";

    gulp.src("").pipe(shell("say " + message));
});

Easy enough. From the command line, you may, of course, call gulp speak to trigger the task. To add it to Elixir, however, use the mix.task() method:

elixir(function(mix) {
    mix.task('speak');
});

That's it! Now, each time you run Gulp, your custom "speak" task will be executed alongside any other Elixir tasks that you've mixed in. To additionally register a watcher, so that your custom tasks will be re-triggered each time one or more files are modified, you may pass a regular expression as the second argument.

elixir(function(mix) {
    mix.task('speak', 'app/**/*.php');
});

By adding this second argument, we've instructed Elixir to re-trigger the "speak" task each time a PHP file in the "app/" directory is saved.

For even more flexibility, you can create full Elixir extensions. Using the previous "speak" example, you may write an extension, like so:

var gulp = require("gulp");
var shell = require("gulp-shell");
var elixir = require("laravel-elixir");

elixir.extend("speak", function(message) {

    gulp.task("speak", function() {
        gulp.src("").pipe(shell("say " + message));
    });

    return this.queueTask("speak");

 });

请注意我们 扩增( extend ) Elixir 的 API 时所使用的第一个参数,稍后我们需要在 Gulpfile 中使用它,以及建立 Gulp 任务所使用的回调函数。

如果你想要让你的自定义任务能被监控,只要在监控器注册就行了。

this.registerWatcher("speak", "app/**/*.php");

这行程序的意思是指,当符合正则表达式 app/*/.php 的文件一经修改,就会触发 message 任务。

很好!接着你可以将这行程序写在 Gulpfile 的顶端,或者将它放到自定义任务的文件里。如果你选择后者,那么你必须将它加载至你的 Gulpfile,例如:

require("./custom-tasks")

大功告成!最后你只需要将他们结合。

elixir(function(mix) {
    mix.speak("Tea, Earl Grey, Hot");
});

加入之后,每当你触发 Gulp,Picard 就会要求一些茶。

Gulp
加密
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

环境配置

系统服务

哈希

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }