Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Gulpjs连接和文件名_Javascript_Gulp - Fatal编程技术网

Javascript Gulpjs连接和文件名

Javascript Gulpjs连接和文件名,javascript,gulp,Javascript,Gulp,我正在寻找一种方法,将文件名作为注释插入流中的每个文件。 因此,在连接的目标文件中,您将有一个带有文件路径的注释行 它现在的作用是: pseudocode: concat(file1, file2) # output: # contents of file 1 # contents of file 2 我想要达到的目标: pseudocode: concat(add_comments(file1, file2)) # output: # // file1 # contents of fi

我正在寻找一种方法,将文件名作为注释插入流中的每个文件。 因此,在连接的目标文件中,您将有一个带有文件路径的注释行

它现在的作用是:

pseudocode: concat(file1, file2) 
# output: 
# contents of file 1
# contents of file 2
我想要达到的目标:

pseudocode: concat(add_comments(file1, file2))
# output: 
# // file1
# contents of file 1
# // file2
# contents of file 2
在连接之前,可以使用在文件名前加前缀:

var wrap=require('gulp-wrap');
//在你的任务中。。。
返回gulp.src('src/***.js')
.pipe(包裹('/\n'))
.pipe(concat('output.js'))
.pipe(吞咽目标(“构建”))
wrap插件允许您使用lodash(或下划线)模板包装流中项目的内容。它自动提供
内容
文件。*
属性

我创建的模板非常简单:它为注释输出两个斜杠,文件的
路径
,一个换行符,然后输出与传入的内容相同的内容。

。 它有一个“transform”函数,在该函数中,您将获得文件内容和乙烯基文件对象,然后返回新内容。所以你可以这样做:

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

正是我要找的!谢谢谢谢现在,如何获取文件名而不是完整路径?“file.name”不起作用。另外,如何发现文件对象中的所有属性是什么?@brOlite-
FILE
似乎是一个乙烯基对象,请参阅。使用
file.relative
对我来说很有效。不是说接受的答案不是解决方案,但我发现gulp insert对于我的用例来说更灵活。