使用GUP3.9.1
我试图返回一堆文件并执行一项任务,该任务要求在两个管道之间传递一个var。
帮助:要么我需要帮助获取管道中的文件路径,要么需要在两个不同管道之间传递var。如果我全局地设置了一个在src之外具有默认值的var,那么它很容易被传递到管道(Inject)。
以下是超级简化代码:
// test code
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
var reload = browserSync.reload;
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('uuid', function() {
return gulp.src('**/*.html'])
// create uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">'))
.pipe(gulp.dest('/prod/./'))
.pipe(reload({ stream: true }));
});值得注意的是,我需要一种跨平台的方式来获得文件路径,从项目的根开始,并包含正斜杠。gulp(打印)完美地从项目的根部开始,忽略了从这一点开始的任何上游的东西。路径的格式很重要,因为它是创建uuid的一半,而uuid必须在Mac或PC平台上匹配。
例子:
/index.html
/dir1/file.html
/dir1/dir2/dir3/file.html发布于 2018-02-07 04:22:13
我解决了问题。这是个业余错误。我返回了设置var的语句,这样var实际上就被杀死了。允许var通过管道的更新代码。
var gulp = require('gulp');
var print = require('gulp-print');
var replace = require('gulp-replace');
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('build', function() {
return gulp.src('**/*.html')
// get a cross-platform filepath and create a uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
}))
// inject uuid
.pipe(replace('dc.identifier" content=""', function() {
return 'dc.identifier" content="' + uuid + '"';
}))
.pipe(gulp.dest('/prod/./'));
});var uuid现在通过管道很好。此代码基于跨平台文件路径创建UUID,并将其注入空dc.identifier元标记中。
发布于 2018-01-30 15:05:43
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
const uuidv3 = require('uuid/v3');
var tap = require('gulp-tap');
// you can declare here
var uuid;
gulp.task('pages', function() {
// or you can declare here
var uuid;
return gulp.src('**/*.html')
// bunch of stuff happens here involving templating/minifying
// create uuid
.pipe(print(function(filepath) {
// then set it here and use it further below
// it will be available
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
//.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n'))
.pipe(tap(function(file, t) {
return t.through(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n');
})
.pipe(gulp.dest('/prod/./'))
.pipe(reload({stream:true}));
});您只是在更高的范围内创建一个变量,您可以在以后设置和引用该变量。如果需要,可以创建一个以filepath作为索引的数组。但我会先尝试一下,因为它只是一个简单的值。
https://stackoverflow.com/questions/48506157
复制相似问题