base 选项如何影响 gulp.src &gulp.dest [英] how base option affects gulp.src & gulp.dest

查看:18
本文介绍了base 选项如何影响 gulp.src &gulp.dest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试复制一组文件时遇到问题,并且在调用 .dest('some folder') 时整个文件夹结构丢失了.

I encountered an issue trying to copy a set of files and when calling .dest('some folder') the entire folder structure was lost.

我搜索并找到了一个答案,建议我应该在调用 gulp.src(...) 时提供 {base:'.'} 作为选项来解决这个问题.

I searched and found an answer suggesting that I should provide {base:'.'} as an option on my call to gulp.src(...) to resolve this issue.

gulp.src 选项的文档说它的选项是:

通过 glob-stream 传递给 node-glob 的选项.

Options to pass to node-glob through glob-stream.

查看 node-glob 文档以了解其选项 基础根本没有列出.
glob-stream 选项文档 仅声明

Looking into node-glob documentation for its options base is not listed there at all.
And the glob-stream options documentation only states that

默认值是 glob 开始之前的所有内容(参见 glob-parent)"

"the Default is everything before a glob starts (see glob-parent)"

所以这里也没有太多帮助.

So no much help here either.

那么,传递给gulp.srcbase 选项对创建的流中的viny6l 文件有什么影响,以及它如何影响gulp.dest 命令?

So, what effect does the base option passed to gulp.src have on the viny6l files in the created stream and how does it effect the gulp.dest command ?

推荐答案

(你没有看官方的 gulp 文档.http://github.com/arvindr21/gulp 只是 gulpjs github repo 的一些人的分支.官方 repo 是 http://github.com/gulpjs/gulp/ 其中 base 选项确实记录在案.)

(You're not looking at the official gulp documentation. http://github.com/arvindr21/gulp is just some guy's fork of the gulpjs github repo. The official repo is http://github.com/gulpjs/gulp/ where the base option is indeed documented.)

回答你的问题:

如果您自己没有指定 base 选项,那么您的 gulp.src() 路径中第一个 glob 之前的所有内容都会自动用作 base 选项并在写入目标文件夹时省略.

If you don't specify the base option yourself, then everything before the first glob in your gulp.src() paths is automatically used as the base option and ommitted when writing to the destination folder.

假设你有以下文件:

some/path/example/app/js/app.js
some/path/example/vendor/js/vendor.js
some/path/example/vendor/lib/js/lib.js

这是你的 Gulpfile.js:

And this is your Gulpfile.js:

gulp.src('some/path/**/js/*.js')
  .pipe(gulp.dest('output'));

在这种情况下,** 之前的所有内容都会自动用作您的 base 选项.所以上面基本上等价于这个:

In this case everything before the ** is automatically used as your base option. So the above is essentially equivalent to this:

gulp.src('some/path/**/js/*.js', {base:'some/path/'})
  .pipe(gulp.dest('output'));

这意味着 some/path/ 会从与 gulp.src() 中的模式匹配的每个文件的路径中删除.output 文件夹中的结果结构如下所示:

What this means is that some/path/ is stripped from the path of every file that matches the pattern in gulp.src(). The resulting structure in the output folder looks like this:

output/example/app/js/app.js
output/example/vendor/js/vendor.js
output/example/vendor/lib/js/lib.js

因此,您的源文件目录结构的某些部分确实丢失了.您丢失了多少目录结构取决于 gulp.src() 模式中的第一个 glob 的位置.

So a certain part of the directory structure of your source files is indeed lost. How much of your directory structure you lose depends on where the first glob in your gulp.src() pattern is.

如果你想避免这种情况,你必须明确指定 base 选项:

If you want to avoid this you have to explicitly specify the base option:

gulp.src('some/path/**/js/*.js', {base:'.'})
  .pipe(gulp.dest('output'));

现在 some/path/ 不会从您的文件路径中删除,导致 output 中的文件夹结构如下:

Now some/path/ will not be stripped from your file paths, resulting in the following folder structure in output:

output/some/path/example/app/js/app.js
output/some/path/example/vendor/js/vendor.js
output/some/path/example/vendor/lib/js/lib.js

如果您将一组模式传递给 gulp.src(),则无法为每个模式指定不同的 base 选项的数组元素.这例如 不会 工作:

If you pass an array of patterns to gulp.src() there's no way to specify a different base option for each of the array elements. This for example won't work:

gulp.src(
  ['source1/examples/**/*.html',
   'source2/examples/**/*.html'],
  { base: ['source1/',    // Doesn't work. 
           'source2/']}   // Needs to be a string.
).pipe(gulp.dest('dist'));

相反,您必须遵循 在一项任务中使用多个来源"配方.这使您可以合并两个流,每个流都可以接收自己的 base 选项:

Instead you have to follow the "Using multiple sources in one task" recipe. This lets you merge two streams each of which can receive its own base option:

var merge = require('merge-stream');

gulp.task('default', function() {
  merge(gulp.src('source1/examples/**/*.html', {base: 'source1/'}),
        gulp.src('source2/examples/**/*.html', {base: 'source2/'}))
   .pipe(gulp.dest('dist'));
});

这篇关于base 选项如何影响 gulp.src &gulp.dest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆