【问题标题】:How to add a path prefix to globs passed to gulp.src?如何为传递给 gulp.src 的 glob 添加路径前缀?
【发布时间】:2014-06-20 08:04:37
【问题描述】:

考虑以下两个文件:

config.json

{
  "vendorFiles": [
    "vendor/angular/angular.js",
    "vendor/angular-ui-router/release/angular-ui-router.js",
    "vendor/angular-ui-utils/modules/utils.js"
  ]
}

gulpfile.js

var gulp = require("gulp"),
    concat = require("gulp-concat"),
    config = require("./config");

gulp.task("build-vendor", function() {
    gulp.src(config.vendorFiles)
        .pipe(concat("vendor.js"))
        .pipe(gulp.dest("build"));
});

如何消除为 config.json 中的每个文件指定 vendor/ 的需要?该文件是由其他开发人员手动编辑的文件,所以我想让他们尽可能轻松。

理想情况下,我希望我的 gulpfile.js 负责添加该前缀(以某种方式),并使我的 config.json 看起来像这样:

{
  "vendorFiles": [
    "angular/angular.js",
    "angular-ui-router/release/angular-ui-router.js",
    "angular-ui-utils/modules/utils.js"
  ]
}

【问题讨论】:

    标签: javascript json node.js glob gulp


    【解决方案1】:

    Gulp 特定的解决方案可能有更好的方法,但这应该可行。

    var gulp = require("gulp"),
        concat = require("gulp-concat"),
        config = require("./config");
    
    gulp.task("build-vendor", function() {
        gulp.src(config.vendorFiles.map(function(a) {return 'vendor/' + a}))
            .pipe(concat("vendor.js"))
            .pipe(gulp.dest("build"));
    });
    

    演示http://jsfiddle.net/AK4tP/

    【讨论】:

      【解决方案2】:

      你就不能这样做

      var gulp = require("gulp"),
          concat = require("gulp-concat"),
          config = require("./config");
      
      gulp.task("build-vendor", function() {
          gulp.src(config.vendorFiles, {root: 'vendor/'})
              .pipe(concat("vendor.js"))
              .pipe(gulp.dest("build"));
      });
      

      Gulp 应该接受 src() 中的 root 选项,尽管它没有记录在案。

      【讨论】:

      猜你喜欢
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2014-02-18
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多