首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Gulp 3.9.1中在管道之间传递一个变量

在Gulp 3.9.1中在管道之间传递一个变量
EN

Stack Overflow用户
提问于 2018-01-29 16:47:28
回答 2查看 1.4K关注 0票数 1

使用GUP3.9.1

我试图返回一堆文件并执行一项任务,该任务要求在两个管道之间传递一个var。

  1. 我使用节点uuid为每个文件路径创建一个v3 UUID,最终为每个页面创建一个uuid。我要用一口指纹来抓取文件路径。
  2. 我希望将该uuid值存储为var。在下一条管道中,在构建过程中,我使用一饮而尽的注入字符串将其写入页面。

帮助:要么我需要帮助获取管道中的文件路径,要么需要在两个不同管道之间传递var。如果我全局地设置了一个在src之外具有默认值的var,那么它很容易被传递到管道(Inject)。

以下是超级简化代码:

代码语言:javascript
复制
// 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平台上匹配。

例子:

代码语言:javascript
复制
/index.html  
/dir1/file.html  
/dir1/dir2/dir3/file.html
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-07 04:22:13

我解决了问题。这是个业余错误。我返回了设置var的语句,这样var实际上就被杀死了。允许var通过管道的更新代码。

代码语言:javascript
复制
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元标记中。

票数 0
EN

Stack Overflow用户

发布于 2018-01-30 15:05:43

代码语言:javascript
复制
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作为索引的数组。但我会先尝试一下,因为它只是一个简单的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48506157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档