【问题标题】:Gulp loosing relative path for files from gulp.src(Array) to gulp.destGulp 丢失从 gulp.src(Array) 到 gulp.dest 的文件的相对路径
【发布时间】:2016-02-12 04:34:15
【问题描述】:

我想使用 gulp 从父目录的 src 集合中复制和粘贴所有 HTML 到子目录,但保留它们的路径

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

我需要 gulp 将所有 HTML 文件复制到相对路径到 Development/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:我知道如果我使用 'Source/**/*.html',会正确复制所有文件,而且我确信我也可以使用 删除 ComponentC! strong>,但是我需要在我的 gulp.src 上定义每个组件,这样我就可以为每个网站编译一组文件。

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

我试过设置 cwd 或 base,但也没有用。

谢谢

【问题讨论】:

    标签: path gulp base cwd


    【解决方案1】:

    我想出了如何使用 base 来解决我的问题。

    很简单,只需添加一个带有 __dirname 的 base,就像这样。 只要确保你在 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'));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多