Passing a variable between pipes in Gulp 3.9.1

孤街浪徒 提交于 2021-01-28 05:29:39

问题


Using gulp 3.9.1

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

  1. I'm using node uuid to create a v3 UUID for each file path to ultimately end up with a uuid for each page. I'm grabbing the file path with gulp-print.
  2. I want to store that uuid value as a var. In the next pipe Im using gulp-inject-string to write it into the page during the build.

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).

Super simplified code below:

// 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 }));
});

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.

examples:

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

回答1:


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}));    
});

You are just creating a variable at a higher scope that you can set and refer to later. If you need a bunch of them create an array with filepath as an index. But I would try it first as just a simple value.




回答2:


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/./'));
});

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.



来源:https://stackoverflow.com/questions/48506157/passing-a-variable-between-pipes-in-gulp-3-9-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!