gpt4 book ai didi

stream - Gulp:在另一个任务中使用一个任务的输出

转载 作者:行者123 更新时间:2023-12-03 13:29:57 25 4
gpt4 key购买 nike

我有三个gulp任务,其中最后一个任务(allScripts)首先运行两个从属任务,然后从中加入结果文件。

在上一个任务中,我可以从前两个任务中删除两个结果文件,并从此以后幸福地生活在一起。

但是我在想,是否可以通过以某种方式“直接”将它们放入allScripts任务来避免这两个临时文件?

gulp.task('firstGroup', function() {
return gulp.src('some/files/*.js')
.pipe(doSomething())
.pipe(concat('some-scripts.js'))
.pipe(gulp.dest('dest'));
});

gulp.task('secondGroup', function() {
return gulp.src('some/other/files/*.js')
.pipe(doSomethingElse())
.pipe(concat('some-other-scripts.js'))
.pipe(gulp.dest('dest'));
});

gulp.task('allScripts', ['firstGroup','secondGroup'], function() {
return gulp.src(['dest/some-scripts.js','dest/some-other-scripts.js'])
.pipe(concat('all-scripts.js'))
.pipe(gulp.dest('dest'))
// delete the two src-files
});

最佳答案

如果一切都可以是一个任务,则可以使用gulp-merge插件将多个流合并为一个。如果任务需要分开,下面还有一个解决方案,但是请注意,该方法是一种hack,因为它依赖于Gulp中的暴露属性。

如果没有黑客解决方案,则将一项任务的输出用于另一项任务的输出,将需要中间存储,就像您对文件所做的一样。

单任务解决方案:

这是使用gulp-merge的准系统演示:



var gulp = require('gulp');
var gulpMerge = require('gulp-merge');
var concat = require('gulp-concat');
var replace = require('gulp-replace');

gulp.task('all-txt', function() {
return gulpMerge(
gulp.src('file1.txt')
.pipe(replace(/foo/g, 'bar')),
gulp.src('file2.txt')
.pipe(replace(/baz/g, 'qux'))
)
.pipe(concat('all-text.txt'))
.pipe(gulp.dest('dest'));
});




就您而言,并使用问题中的代码,它看起来像:

var gulp = require('gulp');
var gulpMerge = require('gulp-merge');
var concat = require('gulp-concat');
// ... your plugins

gulp.task('allScripts', function() {
return gulpMerge(
gulp.src('some/files/*.js')
.pipe(doSomething())
.pipe(concat('some-scripts.js')),
gulp.src('some/other/files/*.js')
.pipe(doSomethingElse())
.pipe(concat('some-other-scripts.js'))
)
.pipe(concat('all-scripts.js'))
.pipe(gulp.dest('dest'));
});




多任务解决方案:

如果您的任务结构无法使用上述方法将它们合并为一个任务,那么这是最好的选择。从某种意义上说,它依赖于 Gulp.tasks,这是一个非标准的公开属性,这有点怪异。没有任何保证可以在将来的Gulp版本中使用(当前已在Gulp v3.8.10中进行了测试)。

此代码段依赖于 event-stream包,因为它更健壮,并且我在 runTasksAndGetStreams函数中使用了它们的某些实用程序。

var gulp = require('gulp');

var concat = require('gulp-concat');
var replace = require('gulp-replace');
var es = require('event-stream');


gulp.task('all-txt', function() {
return es.merge.apply(null, runTasksAndGetStreams(['file1-txt', 'file2-txt']))
.pipe(concat('all-text.txt'))
.pipe(gulp.dest('dest'));
});


gulp.task('file1-txt', ['random-task-dep'], function() {
return gulp.src('file1.txt')
.pipe(replace(/foo/g, 'bar'));
});

gulp.task('file2-txt', function() {
return gulp.src('file2.txt')
.pipe(replace(/baz/g, 'qux'));
});

gulp.task('random-task-dep', function() {
return gulp.src('random-file.txt')
.pipe(gulp.dest('dest'));
});



// Run the given tasks and returns their streams
// This will also take care of any task dependencies
//
// This is basically a custom gulp task orchestartor.
//
// Written for this SO question: http://stackoverflow.com/q/28334314/796832
// Gist: https://gist.github.com/MadLittleMods/d4083d2ba35e2f850161
//
// Params:
// taskNames: string or array of strings of task names
// debugLog: *optional* boolean to print some debug information to the console
function gulpRunTasksAndGetStreams(taskNames, /*optional*/debugLog) {
// You can pass in a single task or an array of tasks to complete
taskNames = [].concat(taskNames);

// We polyfill the pieces of `gulp-util` that we use in case some one wants to use it without installing `gulp-util`
var gutil;
try {
gutil = require('gulp-util');
}
catch(err) {
gutil = {
log: console.log,
colors: {
cyan: function(str) {
return str;
},
magenta: function(str) {
return str;
}
}
};
}

var resultantTaskInfo = [];
var taskMap = gulp.tasks;

// Satisfy all of the task dependencies, create a placeholder stream, and collect the func
// to make the real stream to feed in later when the dependencies are done `mergedDepStream.on('end')`
var mergedDepStream = null;
taskNames.forEach(function(taskName) {
var task = taskMap[taskName];

if(debugLog) {
gutil.log('root task:', gutil.colors.cyan(taskName), 'started working');
}

// Run any dependencies first
var depStreamResult = runDependenciesRecursivelyForTask(taskName, taskMap);

if(depStreamResult) {
mergedDepStream = mergedDepStream ? es.merge(mergedDepStream, depStreamResult) : depStreamResult;
}

if(debugLog) {
if(depStreamResult) {
depStreamResult.on('end', function() {
gutil.log('root task:', gutil.colors.cyan(taskName), 'deps done');
});
}
else {
gutil.log('root task:', gutil.colors.cyan(taskName), 'no deps present');
}
}

// Then push the task itself onto the list
resultantTaskInfo.push({
stream: es.through(),
fn: task.fn
});
});

// Once all of the dependencies have completed
mergedDepStream.on('end', function() {

if(debugLog) {
gutil.log('All dependencies done, piping in real root tasks');
}

// Pipe the actual task into our placeholder
resultantTaskInfo.forEach(function(taskInfo) {
var actualTaskStream = taskInfo.fn();
actualTaskStream.pipe(taskInfo.stream);
});
});


// Recursively gets all of dependencies for a task in order
function runDependenciesRecursivelyForTask(taskName, taskMap, mergedDependencyStream) {
var task = taskMap[taskName];

task.dep.forEach(function(depTaskName) {
var depTask = taskMap[depTaskName];
if(debugLog) {
gutil.log('dep task:', gutil.colors.cyan(depTaskName), 'started working');
}

// Dependencies can have dependencies
var recursiveStreamResult = null;
if(depTask.dep.length) {
recursiveStreamResult = runDependenciesRecursivelyForTask(depTaskName, taskMap, mergedDependencyStream);
mergedDependencyStream = mergedDependencyStream ? es.merge(mergedDependencyStream, recursiveStreamResult) : recursiveStreamResult;
}

if(depTask.fn) {
var whenStreamHandledCallback = function(/* we only use `noDeps` for logging */noDeps) {
if(debugLog) {
if(!noDeps) {
gutil.log('dep task:', gutil.colors.cyan(depTask.name), 'deps done');
}
else {
gutil.log('dep task:', gutil.colors.cyan(depTask.name), 'no deps present');
}
}

var depTaskStream = depTask.fn();
// Merge it in overall dependency progress stream
mergedDependencyStream = mergedDependencyStream ? es.merge(mergedDependencyStream, depTaskStream) : depTaskStream;
};

if(recursiveStreamResult === null) {
whenStreamHandledCallback(true);
}
else {
recursiveStreamResult.on('end', whenStreamHandledCallback);
}
}
});

return mergedDependencyStream;
}


// Return the (placeholder) streams which will get piped the real stream once the dependencies are done
return resultantTaskInfo.map(function(taskInfo) {
return taskInfo.stream;
});
}

关于stream - Gulp:在另一个任务中使用一个任务的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28334314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com