简体   繁体   中英

Changing Laravel's Gulp/Elixir `watch` task

I want to use Laravel's Elixir along with Semantic UI in my new project.

In Semantic UI docs, they suggest how to include their gulp tasks to your current project's gulpfile. In Laravel, they suggest (briefly) how to extend Elixir. But how can I include another gulp task to the watch command?

Currently I'm running gulp watch watch-ui , but I wanted to include the watch-ui task inside watch . Is it possible?

This is my current gulpfile.js :

var gulp     = require('gulp');
var elixir   = require('laravel-elixir');
var semantic = {
  watch: require('./resources/assets/semantic/tasks/watch'),
  build: require('./resources/assets/semantic/tasks/build')
};

gulp.task('watch-ui', semantic.watch);
gulp.task('build-ui', semantic.build);

elixir(function(mix) {
  mix.task('build-ui');
});

If I understand your question correctly, you want to add something to the Semantic UI task. However they never really define a task, but only a function that you can assign to a task.

You can't really include anything in the semantic watch task, unless you want to patch files, but you can add two watch tasks and make sure they both run.

The following code should enable you to do what you need:

var gulp     = require('gulp');
var elixir   = require('laravel-elixir');
var semantic = {
  watch: require('./resources/assets/semantic/tasks/watch'),
  build: require('./resources/assets/semantic/tasks/build')
};

// Define a task for the semantic watch function.
gulp.task('semantic-watch', semantic.watch);

// Define the main watch task, that is depended on the semantic-watch, this will run both watch tasks when you run this one.
gulp.task('watch', ['semantic-watch'], function() {
    // Do your own custom watch logic in here.
});

gulp.task('build-ui', semantic.build);

elixir(function(mix) {
  mix.task('build-ui');
});

You can see how Semantic UI actually defines that their watch tasks should use the watch function here: https://github.com/Semantic-Org/Semantic-UI/blob/master/gulpfile.js#L46

If you mean is it possible to add custom watcher logic through the Elixir API then the short answer is no ...

... but, since Elixir is a wrapper on top of Gulp, you can modify gulp.tasks directly to achieve the same result: simply rename the watch task that Elixir defines to something else, then create your own watch task that runs both Elixir's watch and the Semantic UI watch:

gulp.tasks['watch-elixir'] = gulp.tasks.watch;

gulp.task('watch', ['watch-elixir', 'watch-ui']);


In Elixir itself, the closest you get to custom watchers is a second argument to mix.task() that takes a set of file paths to watch and retriggers the task on change. So, although you could do something like...

mix.task('build-ui', './resources/assets/semantic/src/**/*')

... that would trigger a full rebuild on every change, which isn't the same as the more granular watch task provided by semantic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM