在 Gulp 3.9.1 中的管道之间传递变量 [英] Passing a variable between pipes in Gulp 3.9.1

查看:74
本文介绍了在 Gulp 3.9.1 中的管道之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 gulp 3.9.1

Using gulp 3.9.1

我试图返回一堆文件并执行需要在两个管道之间传递 var 的任务.

I am attempting to return a bunch of files and perform a task that requires a var to be passed between two pipes.

  1. 我正在使用节点 uuid 为每个文件路径创建一个 v3 UUID最终每个页面都有一个 uuid.我正在使用 gulp-print 获取文件路径.
  2. 我想将该 uuid 值存储为 var.在下一个管道中,我使用gulp-inject-string 在构建期间将其写入页面.

帮助:要么我需要帮助获取 gulp-inject-string 管道内的文件路径,要么我需要在两个不同的管道之间传递 var.如果我在 src 之外全局设置一个具有默认值的 var,它会很容易地传递到管道(注入).

Help: Either I need help getting the file path inside the gulp-inject-string pipe or I need to pass the var between the two different pipes. If I globally set a var with a default value outside the src it gets passed easily to the pipe(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(print) 完美地从项目的根开始执行此操作,并忽略从该点开始的任何上游.路径的格式很重要,因为它是创建 uuid 的等式的一半,并且 uuid 在 Mac 或 PC 平台上必须匹配.

It's worth noting that I need a cross platform way to get the file path starting in the root of the project and including forward slashes. The gulp(print) does this perfectly starting at the root of the project and ignoring anything upstream from that point. The format of the path is important because it's one half of the equation in creating the uuid and the uuid's must match on Mac or PC platforms.

示例:

/index.html  
/dir1/file.html  
/dir1/dir2/dir3/file.html

推荐答案

我解决了这个问题.这是一个业余错误.我返回了设置 var 的语句,因此 var 基本上被杀死了.更新了允许 var 通过管道的代码.

I solved the problem. It was an amateur mistake. I returned the statement where the var was set so the var was essentially killed. Updated code that allows the var to pass through the pipes.

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 元标记中.

The var uuid passes through the pipes just fine now. This code creates a UUID based on a cross-platform file path and injects it into an empty dc.identifier meta tag.

这篇关于在 Gulp 3.9.1 中的管道之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆