簡體   English   中英

如何為傳遞給gulp.src的glob添加路徑前綴?

[英]How to add a path prefix to globs passed to gulp.src?

考慮以下兩個文件:

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"
  ]
}

使用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/

你不能做

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選項,盡管沒有記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM