問題描述
我想在 gulp 任務正在運行或已經運行時登錄到標準輸出(配置環境).
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);
});
});
我不確定我應該響應什么事件或在哪里可以找到這些事件的列表.任何指針?非常感謝.
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 團隊建議開發人員將此功能替換為 fancy-log
模塊.此答案已更新以反映這一點.)
(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
提供日志記錄,最初構建由 Gulp 團隊提供.
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:
返回可以通過管道傳輸到插件的 Vinyl 文件流.
Returns a stream of Vinyl files that can be piped to plugins.
Node.js 的 Stream
文檔提供了事件列表.放在一起,這里有一個例子:
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
事件可能會在插件完成之前被調用(并且已經發送了它自己的所有輸出),因為當所有數據都已刷新到底層系統"時會調用該事件".
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".
這篇關于console.log 到標準輸出的 gulp 事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!