Gulp - 复制和重命名文件

Gulp - copy and rename a file(Gulp - 复制和重命名文件)
本文介绍了Gulp - 复制和重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我对 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:

  1. 查看目录下的所有.js文件和
  2. 将它们复制到 cwd(当前工作目录)并
  3. 命名所有副本,无论源文件,相同的东西
  1. Watch all .js files in the parent directory and
  2. Copy them to the cwd (current working directory) and
  3. 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 - 复制和重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)