143

在我的 gulp.js 文件中,我将文件夹中的所有 HTML 文件流式传输examplesbuild文件夹中。

创建 gulp 任务并不难:

var gulp = require('gulp');

gulp.task('examples', function() {
    return gulp.src('./examples/*.html')
        .pipe(gulp.dest('./build'));
});

但是我不知道如何检索在任务中找到(和处理)的文件名,或者我找不到合适的插件。

4

6 回答 6

181

我不确定您要如何使用文件名,但其中之一应该会有所帮助:

  • 如果您只想查看名称,可以使用类似gulp-debug的内容,其中列出了乙烯基文件的详细信息。将其插入您想要列表的任何位置,如下所示:

    var gulp = require('gulp'),
        debug = require('gulp-debug');
    
    gulp.task('examples', function() {
        return gulp.src('./examples/*.html')
            .pipe(debug())
            .pipe(gulp.dest('./build'));
    });
    
  • 另一个选项是gulp-filelog,我没有使用过,但听起来很相似(它可能更干净一些)。

  • 另一个选项是gulp-filesize,它同时输出文件及其大小。

  • 如果你想要更多的控制,你可以使用类似的东西gulp-tap,它可以让你提供你自己的函数并查看管道中的文件。

于 2014-02-16T03:37:49.250 回答
25

我发现这个插件正在做我所期望的:gulp-using

简单使用示例:搜索项目中所有扩展名为 .jsx 的文件

gulp.task('reactify', function(){
        gulp.src(['../**/*.jsx']) 
            .pipe(using({}));
        ....
    });

输出:

[gulp] Using gulpfile /app/build/gulpfile.js
[gulp] Starting 'reactify'...
[gulp] Finished 'reactify' after 2.92 ms
[gulp] Using file /app/staging/web/content/view/logon.jsx
[gulp] Using file /app/staging/web/content/view/components/rauth.jsx
于 2014-05-22T22:34:39.427 回答
12

这是另一种简单的方法。

var es, log, logFile;

es = require('event-stream');

log = require('gulp-util').log;

logFile = function(es) {
  return es.map(function(file, cb) {
    log(file.path);
    return cb(null, file);
  });
};

gulp.task("do", function() {
 return gulp.src('./examples/*.html')
   .pipe(logFile(es))
   .pipe(gulp.dest('./build'));
});
于 2014-06-26T21:14:02.443 回答
5

您可以使用gulp-filenames模块来获取路径数组。您甚至可以按命名空间对它们进行分组:

var filenames = require("gulp-filenames");

gulp.src("./src/*.coffee")
    .pipe(filenames("coffeescript"))
    .pipe(gulp.dest("./dist"));

gulp.src("./src/*.js")
  .pipe(filenames("javascript"))
  .pipe(gulp.dest("./dist"));

filenames.get("coffeescript") // ["a.coffee","b.coffee"]  
                              // Do Something With it 
于 2015-09-11T16:26:19.373 回答
3

就我而言,gulp-ignore是完美的。作为选项,您可以在那里传递一个函数:

function condition(file) {
 // do whatever with file.path
 // return boolean true if needed to exclude file 
}

任务看起来像这样:

var gulpIgnore = require('gulp-ignore');

gulp.task('task', function() {
  gulp.src('./**/*.js')
    .pipe(gulpIgnore.exclude(condition))
    .pipe(gulp.dest('./dist/'));
});
于 2016-04-11T08:09:02.050 回答
0

如果你想在 Typescript 中使用 @OverZealous 的答案 ( https://stackoverflow.com/a/21806974/1019307 ),你需要import代替require

import * as debug from 'gulp-debug';

...

    return gulp.src('./examples/*.html')
        .pipe(debug({title: 'example src:'}))
        .pipe(gulp.dest('./build'));

(我还添加了一个title)。

于 2016-09-14T03:17:40.190 回答