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

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

问题描述

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



找到了一个答案,提示我应该在 gulp.src(...)的基础上提供 {base:'。'} / code>来解决此问题。



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


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


展望其选项的node-glob文档基本没有在那里列出。

glob-stream选项文档仅表明:



默认是每个在glob开始之前的事情(见glob-parent)



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

解决方案

你没有看官方的gulp文档。 http://github.com/arvindr21/gulp 只是gulpjs github回购的一些人的分支。官方回购是 http://github.com/gulpjs/gulp/ ,其中确实记录了基本选项



回答您的问题:



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



假设您有以下文件:

  some / path / example / app / js / app.js 
some / path / example / vendor / js / vendor.js
some / path / example / vendor / lib / js / lib。这是你的Gulpfile.js:

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

在这种情况下, ** 会自动用作 base 选项。所以上面的内容基本上是这样的:

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

这意味着 some / path / 从与 gulp.src()中的模式匹配的每个文件的路径中被删除。在输出文件夹中生成的结构如下所示:

  output /示例/ app / js / app.js 
输出/ example / vendor / js / vendor.js
输出/ example / vendor / lib / js / lib.js
gulp.src()
模式中第一个glob的位置。



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

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

现在 some / path / 不会从您的文件路径剥离,导致输出中的以下文件夹结构:

  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 选项。例如不会

  gulp.src(
['source1 /examples/**/*.html',
'source2 / examples / ** / *。html'],
{base:['source1 /',// does not work。
'source2 /']} //需要是一个字符串
).pipe(gulp.dest('dist'));

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

  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')) ;
});


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

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.

The documentation for gulp.src options only says that its options are:

Options to pass to node-glob through 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

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

So no much help here either.

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 ?

解决方案

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.

To answer your question:

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.

Say you have the following files:

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

And this is your Gulpfile.js:

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

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'));

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

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.

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'));

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

EDIT: 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'));

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'));
});

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

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