admin管理员组

文章数量:1289830

I am looking for a way to insert the file name as a ment to each file in the stream. So after all you'll have a ment line with the file path, in the concatenated destination file.

What it does right now:

pseudocode: concat(file1, file2) 
# output: 
# contents of file 1
# contents of file 2

What I want to achieve:

pseudocode: concat(add_ments(file1, file2))
# output: 
# // file1
# contents of file 1
# // file2
# contents of file 2

I am looking for a way to insert the file name as a ment to each file in the stream. So after all you'll have a ment line with the file path, in the concatenated destination file.

What it does right now:

pseudocode: concat(file1, file2) 
# output: 
# contents of file 1
# contents of file 2

What I want to achieve:

pseudocode: concat(add_ments(file1, file2))
# output: 
# // file1
# contents of file 1
# // file2
# contents of file 2
Share Improve this question asked Apr 19, 2014 at 11:26 M KM K 9,4167 gold badges45 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You can use the gulp-wrap plugin to prepend the file name before concatenating:

var wrap = require('gulp-wrap');

// in your task...
    return gulp.src('src/**/*.js')
        .pipe(wrap('//<%= file.path %>\n<%= contents %>'))
        .pipe(concat('output.js'))
        .pipe(gulp.dest('build'))

The wrap plugin allows you to wrap the contents of an item in the stream with a lodash (or underscore) template. It provides contents and file.* properties automatically.

The template I created is very simple: it outputs two slashes for the ment, the file's path, a newline, and then outputs the same contents as passed in.

I did it with gulp-insert. It has a "transform" function where you are given the file contents, and the Vinyl file object, and you return the new contents. So you can do this:

.pipe(insert.transform(function(contents, file){
    return '// ' + file.path + '\n' + contents;
}));

本文标签: javascriptGulpjs concatenation and file namesStack Overflow