問題描述
我想在 gulp 任務(wù)正在運(yùn)行或已經(jīng)運(yùn)行時(shí)登錄到標(biāo)準(zhǔn)輸出(配置環(huán)境).
I want to log to stdout (the config environment) when a gulp task is running or has run.
類似這樣的:
gulp.task('scripts', function () {
var enviroment = argv.env || 'development';
var config = gulp.src('config/' + enviroment + '.json')
.pipe(ngConstant({name: 'app.config'}));
var scripts = gulp.src('js/*');
return es.merge(config, scripts)
.pipe(concat('app.js'))
.pipe(gulp.dest('app/dist'))
.on('success', function() {
console.log('Configured environment: ' + environment);
});
});
我不確定我應(yīng)該響應(yīng)什么事件或在哪里可以找到這些事件的列表.任何指針?非常感謝.
I am not sure what event I should be responding to or where to find a list of these. Any pointers? Many thanks.
推薦答案
(2017 年 12 月,提供日志記錄的 gulp-util
模塊,已棄用.Gulp 團(tuán)隊(duì)建議開發(fā)人員將此功能替換為 fancy-log
模塊.此答案已更新以反映這一點(diǎn).)
(In December 2017, the gulp-util
module, which provided logging, was deprecated. The Gulp team recommended that developers replace this functionality with the fancy-log
module. This answer has been updated to reflect that.)
fancy-log
提供日志記錄,最初構(gòu)建由 Gulp 團(tuán)隊(duì)提供.
fancy-log
provides logging and was originally built by the Gulp team.
var log = require('fancy-log');
log('Hello world!');
要添加日志記錄,Gulp 的 API 文檔告訴我們.src
返回:
To add logging, Gulp's API documentation tell us that .src
returns:
返回可以通過管道傳輸?shù)讲寮?Vinyl 文件流.
Returns a stream of Vinyl files that can be piped to plugins.
Node.js 的 Stream
文檔提供了事件列表.放在一起,這里有一個(gè)例子:
Node.js's Stream
documentation provides a list of events. Put together, here's an example:
gulp.task('default', function() {
return gulp.src('main.scss')
.pipe(sass({ style: 'expanded' }))
.on('end', function(){ log('Almost there...'); })
.pipe(minifycss())
.pipe(gulp.dest('.'))
.on('end', function(){ log('Done!'); });
});
注意:end
事件可能會(huì)在插件完成之前被調(diào)用(并且已經(jīng)發(fā)送了它自己的所有輸出),因?yàn)楫?dāng)所有數(shù)據(jù)都已刷新到底層系統(tǒng)"時(shí)會(huì)調(diào)用該事件".
Note: The end
event may be called before the plugin is complete (and has sent all of its own output), because the event is called when "all data has been flushed to the underlying system".
這篇關(guān)于console.log 到標(biāo)準(zhǔn)輸出的 gulp 事件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!