問題描述
我對 Gulp 非常陌生.我基本上是在嘗試查看修改過的 JavaScript 文件,然后用新名稱制作它的新副本.(最終會有一些處理,但羅馬不是一天建成的).
I'm extremely new to Gulp. I'm basically trying to watch for a modified JavaScript file, and then make a new copy of it with a new name. (eventually there'll be some processing on it, but Rome wasn't built in a day).
我的(天真的)嘗試是這樣的:
My (naive) attempt is this:
gulp.task('default', function() {
return gulp.watch('../**/**.js', function(obj){
gulp.src(obj.path)
.pipe(gulp.dest('foobar.js'));
});
});
這會獲取修改后的文件并成功地將其復制到現在名為 foobar.js 的文件夾中.有什么簡單的方法可以替換 gulp.dest('foobar.js')
,只需復制并重命名 src 文件嗎?
This takes the modified file and successfully copies it into a folder now called foobar.js. Is there anything simple I can replace gulp.dest('foobar.js')
with that will simply copy and rename the src file in place?
編輯
就地復制,我的意思是我想獲取修改后的文件,并在它當前所在的位置用一個新名稱復制它.相當于單擊文件(在 Windows 中)并按 control-c control-v,然后重命名生成的文件.
By copy in place, I mean I want to take the modified file, and make a copy of it right where it currently is with a new name. The equivalent of clicking the file (in windows) and hitting control-c control-v, then renaming the resulting file.
推薦答案
我不是 100% 確定你的意思
I'm not 100% certain what you mean by
復制并重命名 ... 就地
copy and rename ... in place
但是,根據您當前的代碼,如果您只是希望:
But, based on your current code, if you simply wish to:
- 查看父目錄下的所有
.js
文件和 - 將它們復制到
cwd
(當前工作目錄)并 - 命名所有副本,無論源文件,相同的東西
- Watch all
.js
files in the parent directory and - Copy them to the
cwd
(current working directory) and - Name all copies, regardless of source file, the same thing
那么你可以使用 gulp-rename 來做到這一點:
Then you could use gulp-rename to do just that:
var gulp = require('gulp');
var rename = require('gulp-rename');
gulp.task('default', function() {
return gulp.watch('../**/**.js', function(obj) {
gulp.src(obj.path)
.pipe(rename('newFileName.js'))
.pipe(gulp.dest('.'));
});
});
在這種情況下,輸出文件名是 newFileName.js
In this case, the output filename is newFileName.js
為了使用該模塊,您需要使用 npm 安裝 gulp-rename
包(即:npm install gulp-rename
).
In order to use the module, you'll need to install the gulp-rename
package with npm (ie: npm install gulp-rename
).
更多示例可在 npm @ https://www 的包詳細信息頁面上找到.npmjs.com/package/gulp-rename#usage
More examples are available on the package details page on npm @ https://www.npmjs.com/package/gulp-rename#usage
這篇關于Gulp - 復制和重命名文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!