gulp.dest 没有创建目标文件夹

2022-01-12 00:00:00 javascript gulp

我的 gulp 代码部分看起来像这样

My gulp code looks like this, in part

gulp.src(['../application-base/**/**.js', '!../application-base/assets/**/**.js'], { base: './' })
    .pipe(gulpPlumber({
        errorHandler: function (error) {
            console.log(`
Error ${error}`);
            this.emit('end');
        }
    }))
    .pipe(gprint(filePath => "Transpiling: " + filePath.replace('..\application-base\', '')))
    .pipe(babel({ compact: false }))
    .pipe(gulp.dest('../application-base-transpiled/'))
    .on('end', () => done());

如果我改变了

.pipe(gulp.dest('../application-base-transpiled/'))

.pipe(gulp.dest(''))

然后创建转译文件,并覆盖原始文件.问题是

then the transpiled files are created, and overwrite the originals. The problem is that

.pipe(gulp.dest('../application-base-transpiled/'))

不保存文件,在 application-base-transpiled

如您所见,我正在使用一个底座,否则它似乎可以工作.

As you can see, I am using a base, which seems to work otherwise.

我错过了什么?

编辑

我看的更仔细了,好像和

I looked more closely, and it seems even with

.pipe(gulp.dest('../application-base-transpiled/'))

Gulp 仍在将转译后的文件放到原来的位置,覆盖原来的位置.Gulp 不喜欢我正在传递的目标,并且默默地忽略了这一点.

Gulp is still placing the transpiled files into the original place, overwriting the original. There's something about the dest I'm passing that Gulp doesn't like, and is ignoring silently.

编辑 2

似乎删除 base 选项解决了这个问题,这与我在其他地方看到的建议相反.gulp.dest 的文档并没有真正讨论这个问题.

It seems removing the base option solves this problem, contrary to advice I've seen elsewhere. The docs for gulp.dest don't really discuss this.

谁能提供一些对此的见解?

Can anyone provide some insight into this?

编辑 3

根据 Sven 的回答,我试过了

Per Sven's answer, I tried this

gulp.src(['**/**.js', '!assets/**/**.js'], { base: '../application-base' })
    .pipe(gulpPlumber({
        errorHandler: function errorHandler(error) {
            console.log('
Error ' + error);
            this.emit('end');
        }
    }))
    .pipe(gprint(filePath => "Transpiling: " + filePath.replace('..\application-base\', '')))
    .pipe(babel({ compact: false }))
    .pipe(gulp.dest('../application-base-transpiled/'))
    .on('end', () => done());

但似乎基础被忽略了,我自己当前目录中的文件被抓取并转换到位(我想要的最后一件事 - 幸运的是 GIT 有助于消除损坏).

But it seems the base is being ignored, and the files from my own current directory are being grabbed and transpiled in place (the last thing I want - fortunately GIT was helpful in undoing the damage).

src 使用数组时会忽略基本参数吗?

Is the base parameter ignored when using an array for src?

推荐答案

在 gulp 流中,文件的目标路径遵循以下伪等式:

In gulp streams the destination path of a file follows this pseudo-equation:

gulp.dest + (gulp.src 没有前导 base) = 文件的目标路径

gulp.dest + (gulp.src without leading base) = dest path of file

例子:

gulp.src('src/js/foo.js', {base:'src/'}).pipe(gulp.dest('dist/'));

结果:

'dist/' + ('src/js/foo.js' 没有前导 'src/') ='dist/js/foo.js'

'dist/' + ('src/js/foo.js' without leading 'src/') = 'dist/js/foo.js'

在你的情况下:

'../application-base-transpiled/' + ('../application-base/foo/bar.js' 不带前导'./') = '../application-base-transpiled/../application-base/foo/bar.js'

'../application-base-transpiled/' + ('../application-base/foo/bar.js' without leading './') = '../application-base-transpiled/../application-base/foo/bar.js'

所以你的文件最终在原始目录中.

So your files end up in the original directory.

您必须将 {base: '../application-base/'} 传递给 gulp.src() 以使您的示例工作.

You have to pass {base: '../application-base/'} to gulp.src() to make your example work.

注意

您仍然需要在 src 路径中包含 '../application-base/'.base 的目的是根据我上面的公式来操纵 dest 路径;它确实不用于减少您在 gulp.src 中键入的击键次数.所以最终的结果应该是这样的

You still need to include '../application-base/' in your src path. The purpose of base is to manipulate the dest path, per my equation above; it does not serve the purpose of lessening the number of keystrokes you type in gulp.src. So the end result should be something like this

gulp.src(['../application-base/**/**.js'], { base: '../application-base' })
        .pipe(gulpPlumber({
            errorHandler: function errorHandler(error) {
                console.log('
Error ' + error);
                this.emit('end');
            }
        }))
        .pipe(gprint(filePath => "Transpiling: " + filePath.replace('..\application-base\', '')))
        .pipe(babel({ compact: false }))
        .pipe(gulp.dest('../application-base-transpiled'))
        .on('end', () => done());

<小时>

如果你没有通过 base 选项 到 gulp.src() 设置了默认值:


If you don't pass a base option to gulp.src() a default is set:

默认值:glob 开始之前的所有内容(参见 glob2base)

Default: everything before a glob starts (see glob2base)

这意味着你传递给 gulp.src() 的模式中的第一个 *** 之前的所有内容用作 base 选项.由于您的模式是 ../application-base/**/**.js,您的 base 选项会自动变为 ../application-base/.

What this means is that everything up to the first ** or * in the pattern that you pass to gulp.src() is used as the base option. Since your pattern is ../application-base/**/**.js, your base option automatically becomes ../application-base/.

相关文章