gulp-load-utils
v0.0.4
Published
Conveniently wrapped utility functions for working with gulp
Downloads
2,141
Readme
gulp-load-utils [![Build Status][travis-image]][travis-url]
Recommended Gulp utilities wrapped up conveniently without creating an unnecessary dependency tree.
This is a fork of gulp-util, sharing much of the same code. Use instead of gulp-util to load only the utilities you want, as well as other recommended packages.
You'll need the devDependencies in your package.json for the utils you request, which are listed with each utlity below. If you try to use a utility which you hadn't requested, or you request one without its dependencies installed, an error will be thrown telling you what's missing.
Usage
npm install --save-dev gulp-load-utils
Also npm install or add to package.json the devDependencies you need, as listed below.
Pass the properties of gutil you want to use like so:
var gutil = require('gulp-load-utils')(['colors', 'env', 'log', 'pipeline']);
// Run `gulp --production`
var isProduction = gutil.env.production;
if(isProduction) {
gutil.log( 'Building for', gutil.colors.magenta('production') );
gutil.beep();
}
// [gulp] [23:09:09] Building for production *system beep*
This keeps your code cleaner and loads less dependencies as opposed to starting with:
var gutil = require('gulp-util');
var colors = require('chalk');
var env = require('minimist')(process.argv.slice(2));
var pipeline = require('multipipe');
Suggested usage with gulp-load-plugins:
var $ = require('gulp-load-plugins')();
var _ = $.loadUtils(['log', 'colors']);
_.log( _.colors( ... ) );
beep()
Trigger a system beep. No dependencies, always present.
buffer(cb)
This is similar to es.wait but instead of buffering text into one string it buffers anything into an array (so very useful for file objects).
Returns a stream that can be piped to.
The stream will emit one data event after the stream piped to it has ended. The data will be the same array passed to the callback.
Callback is optional and receives two arguments: error and data
gulp.src('stuff/*.js')
.pipe(gutil.buffer(function(err, files) {
});
colors
An instance of chalk.
Use when logging values with gutil.log
.
gutil.log( gutil.colors.red('Red text!'));
date
An instance of node-dateformat.
Using gutil.log
prefixes the output with this already, as HH:MM:ss
.
var header = 'Compiled on ' + gutil.date('mmm d, yyyy h:MM:ss TT Z');
// Compiled on Mar 7, 2014 5:09:09 PM EST
defineSrc(filename)
An instance of vinyl-source-stream.
Use to convert regular text streams to the vinyl file streams used by Gulp. Pass a fake filename/path to control the destination.
See the watchify recipe for an example.
env
See minimist.
// Run `gulp --type production`
var isProduction = gutil.env.type === 'production';
new File(obj)
An instance of vinyl.
var file = new gutil.File({
base: join(__dirname, './fixtures/'),
cwd: __dirname,
path: join(__dirname, './fixtures/test.coffee')
});
isStream(obj)
Returns true or false if an object is a stream. No dependencies, always present.
isBuffer(obj)
Returns true or false if an object is a Buffer. No dependencies, always present.
lazypipe()
An instance of lazypipe.
Combine streams in a reusable manner.
var jsTasks = gutil.lazypipe()
.pipe(compileJS, jsOpts)
.pipe(minifyJS)
;
gulp.task('scripts', function() {
return gulp.src('src/**/*.js')
.pipe(jsTasks())
.pipe(gulp.dest('dist/'));
});
log(msg...)
Prefixes the message with [gulp]
and the current time. Multiple arguments are joined with a space, just like console.log
. Use the right colors for values.
values (files, module names, etc.) = magenta
numbers (times, counts, etc) = cyan
gutil.log( 'Something happened in', gutil.colors.magenta(file), 'after', gutil.colors.cyan(count), 'things');
noop()
Returns a stream that does nothing but pass data straight through.
gulp.task('scripts', function() {
gulp.src('src/**/*.js')
.pipe(isProduction ? minifyJS() : gutil.noop())
.pipe(gulp.dest('dist/');
});
pipeline(streams...)
An instance of multipipe.
Use to combine streams, apply events to them. For a reusable approach use gutil.lazypipe
.
gulp.task('scripts', function() {
return gutil.pipeline(
gulp.src('src/**/*.js'),
compileJS(),
minifyJS(),
gulp.dest('dist/')
).on('error', errHandler);
});
new PluginError(pluginName, message[, options])
- pluginName should be the module name of your plugin
- message can be a string or an existing error
- By default the stack will not be shown. Set
options.showStack
to true if you think the stack is important for your error. - If you pass an error in as the message the stack will be pulled from that, otherwise one will be created.
These are all acceptable forms of instantiation:
var err = new gutil.PluginError('test', {
message: 'something broke'
});
var err = new gutil.PluginError({
plugin: 'test',
message: 'something broke'
});
var err = new gutil.PluginError('test', 'something broke');
var err = new gutil.PluginError('test', 'something broke', {showStack: true});
var existingError = new Error('OMG');
var err = new gutil.PluginError('test', existingError, {showStack: true});
replaceExtension(path, newExtension)
An instance of replace-ext.
Replaces a file extension in a path. Returns the new path.
var filepath = '/some/dir/file.js';
var newpath = replaceExt(filepath, '.coffee');// /some/dir/file.coffee
template
This is a lodash.template function wrapper. You must pass in a valid gulp file object so it is available to the user or it will error. You can not configure any of the delimiters. Look at the lodash docs for more info.
var opt = {
name: 'todd',
file: someGulpFile
};
gutil.template('test <%= name %> <%= file.path %>', opt) // test todd /js/hi.js
```
[travis-url]: https://travis-ci.org/DSKrepps/gulp-load-utils
[travis-image]: https://travis-ci.org/DSKrepps/gulp-load-utils.png?branch=master