2015-12-28 20 views
0

我在gulp.src功能,即使用正則表達式基於路徑,如何從gulp.dest函數訪問gulp.src中提到的路徑?

gulp.task('gen-templateCache', function() { 
    return gulp.src('app/client/pages/**/views/**/markup/*.html') 
     .pipe(templateCache('new-templates.js')) 
     .pipe(gulp.dest(function(file){ 
      //Access the src path 
      //Generate and Return the dest path based on the src path 
     })); 
}); 

正如你可以看到我使用正則表達式'app/client/pages/**/views/**/markup/*.html'產生這種一飲而盡任務的src路徑,因爲我的源文件都位於多個文件夾。

我的要求是我還需要將我的結果保存在相對於我的src的多個文件夾中。

即,假設我的吞嚥任務結果文件是'template.js'。現在我想將這個結果保存在'app/client/pages/**/views/template.js'

我該怎麼做?

+0

檢查[gulp-rename](https://github.com/hparra/gulp-rename)插件。 – raina77ow

+0

更改filepath.dirname和filepath.basename由於某種奇怪的原因而沒有爲我完成這項工作。 – jackOfAll

回答

1

我仍然沒能仍然找到一個通用的解決這個問題,但我是能夠成功地找到一個解決方案,適合我的特殊一飲而盡任務:

基本上我在表演上的SRC文件的任務是「吞掉-角templatecache'(https://www.npmjs.com/package/gulp-angular-templatecache

gulp.task('gen-templateCache', function() { 
    var destination = ''; 
    return gulp.src('app/client/pages/**/views/**/markup/*.html') 
     .pipe(templateCache('partials.js', { 
      root: '', 
      module: 'pageApp', 
      transformUrl: function(url) { 
       var ind = url.lastIndexOf('\\'); 
       var partialName = url.substring(ind+1); 
       var viewsIndex = url.indexOf('\\views\\'); 
       var viewsPath = url.substring(0,viewsIndex+7); 
       destination = "./app/client/pages/"+viewsPath; 
       return partialName; 
      } 
     })) 
     .pipe(gulp.dest(function() { 
      return destination; 
     })); 
}); 

此解決方案僅因爲上述任務需要在第二個參數被稱爲transformUrl一個選項,inturn是與該文件的URL作爲一個功能工作在上述任務的情況下參數。但是,我仍然會欣賞一個通用的解決方案。