【问题标题】:Excluding files/directories from Gulp task从 Gulp 任务中排除文件/目录
【发布时间】:2014-06-16 13:07:07
【问题描述】:

我有一个 gulp rjs 任务,它连接和丑化我所有的自定义 .JS 文件(任何非供应商库)。

我想做的是从这个任务中排除一些文件/目录(控制器和指令)。

这是我的树:

 - application
    - resources
      - js
        main.js
        - vendor
            - jquery
            - modernzr
            - angular
        - controllers
            - controller1
            - controller2
            - controller3
        - directives
            - directives1
            - directives2
            - directives3
        - widgets
            - widget1
            - widget2
            - widget3
            - widget4
        - modules
            - modules1
            - modules2
            - modules3
            - modules4

这是我的 gulp.js

dir = {
    app:        'application',
    dest:       'dest',
};

config = {
    src: {
        js: dir.app + '/resources/js'
    },
    dest: {
        js: dir.dest + '/resources/js'
    }
};

gulp.task('rjs', function() {

      rjs({
            baseUrl: config.src.js,
            out: 'main.js',
            name: 'main',
            mainConfigFile: config.src.js + '/main.js',
            exclude: [ 'jquery', 'angular']         
        })
        .pipe(prod ? uglify({ mangle: false, outSourceMap: true, compress: { drop_console: true } }) : gutil.noop())
        .pipe(gulp.dest(config.dest.js))
        .pipe(filesize())
        .pipe(dev ? connect.reload() : gutil.noop());

});

【问题讨论】:

标签: requirejs gulp minify uglifyjs


【解决方案1】:

快速解答

在 src 上,您始终可以使用“!”指定要忽略的文件。

示例(您要排除 js 文件夹和子文件夹中的所有 *.min.js 文件:

gulp.src(['js/**/*.js', '!js/**/*.min.js'])

您也可以对单个文件执行此操作。

扩展答案:

Extracted from gulp documentation:

gulp.src(globs[, options])

发出与提供的 glob 或 glob 数组匹配的文件。返回可以通过管道传输到插件的 Vinyl 文件流。

glob 指的是 node-glob 语法,也可以是直接的文件路径。

所以,查看node-glob documentation,我们可以看到它使用 minimatch 库进行匹配。

minimatch documentation,他们指出以下内容:

如果模式以 !字符,则取反。

这就是为什么使用 !符号将从 gulp 任务中排除文件/目录

【讨论】:

  • 不应该避免这种情况吗?这来自 node-glob 文档:Negation The intent for negation would be for a pattern starting with ! to match everything that doesn't match the supplied pattern. However, the implementation is weird, and for the time being, this should be avoided. The behavior is deprecated in version 5, and will be removed entirely in version 6.
  • 我需要使用:gulp.src([/*file globs*/], {base:"."}) 以使 glob 模式正常工作。
  • @avcajaraville 我同意您的回答是完全有效的。如果没有我在评论中回复的内容,我无法让发布的回复正常工作。不知道这与答案/问题无关,只是想发布对我有用的内容。
  • @MisterOh 实际上,gulp.src 是vinyl-fs.src,它将通配符传递给glob-stream,而glob-stream 又使用node-glob 表示正glob,minimatch 表示负glob glob,所以可以使用负模式。查看glob-stream documentation and sample positive and negative glob patterns herehave a look at the code
  • 否定是正确的方法。 gulp 文档不正确。我打开了一个关于文档的issue,并提交了PR,其中包含corrected documentation
【解决方案2】:

Gulp 在底层使用微匹配来匹配 glob,因此如果您想排除任何 .min.js 文件,您可以使用 extended globbing 功能来实现相同的目的:

src("'js/**/!(*.min).js")

基本上它的意思是:抓取 js 内不以 *.min.js

结尾的任何级别的所有内容

【讨论】:

  • 这适用于文件夹,与接受的答案不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2016-05-04
  • 1970-01-01
  • 2018-07-09
  • 2015-04-17
相关资源
最近更新 更多