問題描述
我的 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/
.
這篇關于gulp.dest 沒有創建目標文件夾的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!