2016-02-12 48 views
0

的文件的相对路径我想使用gulp将src集合中的所有HTML从父目录复制并粘贴到子目录,但要保留它们路径gulp丢失从gulp.src(Array)到gulp.dest

Project 
+-- /Development 
| +- gulpfile.js 
| 
+-- /Source 
    +- /ComponentA 
    | +- /DirA 
    |  +- fileA.html 
    | 
    +- /ComponentB 
    | +- /DirB 
    |  +- fileB.html 
    | 
    +- /ComponentC 
     +- /DirC 
      +- fileC.html 

我需要吞掉所有的HTML文件复制到相对路径为发展/的public_html

Project 
+-- /Development 
    +- gulpfile.js 
    | 
    +- /public_html 
     +- /ComponentA 
     | +- /DirA 
     | +- fileA.html 
     | 
     +- /ComponentC 
      +- /DirC 
       +- fileC.html 

我一饮而尽任务

gulp.task('copyHTMLwrong', function() { 
    return gulp.src([ 
     '../Source/ComponentA/**/*.html', 
     '../Source/ComponentC/**/*.html' 
    ]) 
    .pipe(gulp.dest('public_html')); 
}); 

,但我得到(松路):

Project 
+-- /Development 
    +- gulpfile.js 
    | 
    +- /public_html 
     +- fileA.html 
     +- fileC.html 

PS:我知道,如果我使用 '源/ **/* HTML',将正确地复制所有文件,我敢肯定,我也可以使用删除ComponentC,但我需要在我的gulp.src上定义每个组件,因此我可以为每个网站编译一组文件。

gulp.task('copyHTMLnotUseful', function() { 
    return gulp.src([ 
     '../Source/**.*.html', 
     '!../Source/ComponentB/**/*.html' 
    ]) 
    .pipe(gulp.dest('public_html')); 
}); 

我试过设置CWD基地,但也不能工作。

感谢

回答

0

我想通了如何使用基地解决我的问题。

很简单,只需用__dirname添加一个基地,就像这样。 只要确保你设置path.resolve()内:

gulp.task('copyHTML', function() { 
    return gulp.src([ 
     '../Source/ComponentA/**/*.html', 
     '../Source/ComponentC/**/*.html' 
    ], 
    {base: path.resolve(__dirname + '/../Source')}) 
    .pipe(gulp.dest('public_html')); 
});