Found something wrong or missing? Open an issue!
The module has 2 static methods
Gulp.create - Gulp.createClass
the same gulp API methods we know and love
gulp.src - gulp.dest - gulp.task - gulp.watch
and 3 more to bundle/run tasks
gulp.series - gulp.parallel - gulp.stack
-
Install
npm install --save-dev gulp-runtime -
Open a
gulpfile, or create one, and
change this line
var gulp = require('gulp');with
var gulp = require('gulp-runtime').create();After that run the gulpfile with node directly from the command line
node gulpfile.js default watch serveThats it! When no arguments are given the default task will run instead.
- What about the CLI? Can I just run
gulp-runtimefrom the terminal?
Yes. For this add an alias to your .bashrc/.zshrc
alias gulp-runtime='node $(find . -name "gulpfile.js" -not -path "./node_modules/*" | head -n1)'which will use the first gulpfile.js found in the current working directory excluding node_modules.
Right after this open a new terminal tab and write
gulp-runtime --tasks default watch serve
The module exports a constructor function
var Gulp = require('gulp-runtime');which has two static methods: Gulp.create and Gulp.createClass.
function create([Object props])Gulp.create returns a new instance with the given props.
Defaults are:
-
props.log = truetask logging is enabled, passfalseto disable it -
props.repl = falsethe REPL is disabled, passtrueto enable it -
props.wait = falsetasks will run in parallel by default. Passwait: trueto make series the default when running tasks -
props.onStackEndcalled when a stack has ended, defaults to empty function -
props.onHandleEndcalled after a task has ended, defaults to empty function -
props.onHandleStartcalled before a task starts, defaults to empty function -
props.onHandleErrorcalled when a task throws, defaults to empty function
These callbacks can be overridden gulp.series, gulp.parallel and gulp.stack.
function createClass([Object mixin])Gulp.createClass returns a new constructor function that inherits from its parent prototype.
- When
mixinis given it overrides its parent prototype. - When
mixin.createis given it will be used as the instance constructor.
Example:
Say we always want to make instances that log and have a REPL.
var Gulp = require('gulp-runtime').createClass({
create: function Gulp (props) {
props = props || {};
props.log = props.repl = true;
Gulp.super_.call(this, props);
}
});
exports = module.exports = Gulp;gulp.src, gulp.dest, gulp.watch and gulp.task behave the same as described in the gulp API documentation.
In addition task names can use :parameters (like expressjs routes) and have arguments passed from other task or task runner.
:parameters example:
var gulp = require('gulp-runtime').create();
gulp.task('build:mode', function (done) {
console.log(this.params.mode);
done(); // or do async things
});done will be always passed as first argument to the task. It should be used if the task does not return a stream, promise or RxJS observable.
Tasks parameters can also use regular expressions using parens right after the parameter
var gulp = require('gulp-runtime').create();
gulp.task('build:mode(-dev|-prod)', function (done){
done(); // or do async things
});To know more about how this tasks names can be set see parth.
arguments example:
var gulp = require('gulp-runtime').create();
gulp.task('build', function (done, sources, dest) {
var stream = gulp.src(sources)
// some build steps here
stream.on('end', function () {
// pass stream to next task
done(stream);
});
});
gulp.task('minify', function (done, stream) {
return stream
// minify
.pipe(gulp.dest(dest));
});
gulp.task('build and min', function (done) {
gulp.series('build', 'minify')('src/**/*.js', 'dest/source.min.js', done);
});function series(tasks...[, Object options])series bundles the given tasks... into one async function and returns it. This function will always run the tasks... in series.
Its sugar on top of [gulp.stack][#gulpstack]. See [gulp.stack][#gulpstack] for more information about options.
function parallel(tasks...[, Object options])parallel bundles the given tasks... into one async function and returns it. This function will always run the tasks... in parallel.
Its sugar on top of [gulp.stack][#gulpstack]. See [gulp.stack][#gulpstack] for more information about options.
function stack(tasks...[, Object options])stack bundles the given tasks... into one async function and returns it.
Each tasks... can be either a string or a function.
If given, options will override the instance props for this stack.
The initial aim of this project was to be able to run gulp tasks directly from a REPL. But when that was then possible, one also wants to be able to run the CLI while using the REPL, right?
For this reason the gulp cli commands are set as tasks for each instance. Which lets one use them as tasks.
Example:
var gulp = require('gulp-runtime').create();
gulp.task('info', ['--tasks', '--version']);
// other tasks...
gulp.task('default', ['info']);var gulp = require('gulp-runtime').create({repl: true});When an instance passes repl: true the process running does not exit when all tasks are done. What happens then its that the process will have a REPL listening on stdin.
$ node gulpfile.jspress enter and you will see a prompt > that will run the tasks defined in the gulpfile
>
> build less compress pngThis way you can run tasks in the same way you run commands on the terminal.
How will tasks run with the REPL?
- If those tasks are defined they will run in parallel
- If there is more than one instance with
repl: truethe REPL will go through them and run the first task that matched one of those tasks - If one or more of those tasks is not defined there will be a warning and none of the tasks will run for that instance of any of the other ones.
For more information see gulp-repl.