如何使用 gulp 替换文件中的字符串?

How can I use gulp to replace a string in a file?(如何使用 gulp 替换文件中的字符串?)
本文介绍了如何使用 gulp 替换文件中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 gulp 对我的 javascript 文件进行 uglify 并准备好用于生产.我所拥有的是这段代码:

I am using gulp to uglify and make ready my javascript files for production. What I have is this code:

var concat = require('gulp-concat');
var del = require('del');
var gulp = require('gulp');
var gzip = require('gulp-gzip');
var less = require('gulp-less');
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');

var js = {
    src: [
        // more files here
        'temp/js/app/appConfig.js',
        'temp/js/app/appConstant.js',
        // more files here
    ],

gulp.task('scripts', ['clean-js'], function () {
    return gulp.src(js.src).pipe(uglify())
      .pipe(concat('js.min.js'))
      .pipe(gulp.dest('content/bundles/'))
      .pipe(gzip(gzip_options))
      .pipe(gulp.dest('content/bundles/'));
});

我需要做的是替换字符串:

What I need to do is to replace the string:

dataServer: "http://localhost:3048",

dataServer: "http://example.com",

在文件'temp/js/app/appConstant.js'中,

In the file 'temp/js/app/appConstant.js',

我正在寻找一些建议.例如,也许我应该复制 appConstant.js 文件,更改它(不确定如何)并将 appConstantEdited.js 包含在 js.src 中?

I'm looking for some suggestions. For example perhaps I should make a copy of the appConstant.js file, change that (not sure how) and include appConstantEdited.js in the js.src?

但我不确定 gulp 如何复制文件并替换文件中的字符串.

But I am not sure with gulp how to make a copy of a file and replace a string inside a file.

您提供的任何帮助将不胜感激.

Any help you give would be much appreciated.

推荐答案

Gulp 流式输入,执行所有转换,然后流式输出.使用 Gulp 时,在两者之间保存临时文件是 AFAIK 非惯用的.

Gulp streams input, does all transformations, and then streams output. Saving temporary files in between is AFAIK non-idiomatic when using Gulp.

相反,您正在寻找的是一种替换内容的流式传输方式.自己写东西会比较容易,或者你可以使用现有的插件.对我来说,gulp-replace 效果很好.

Instead, what you're looking for, is a streaming-way of replacing content. It would be moderately easy to write something yourself, or you could use an existing plugin. For me, gulp-replace has worked quite well.

如果您想在所有文件中进行替换,您可以像这样轻松更改任务:

If you want to do the replacement in all files it's easy to change your task like this:

var replace = require('gulp-replace');

gulp.task('scripts', ['clean-js'], function () {
    return gulp.src(js.src)
      .pipe(replace(/http://localhost:d+/g, 'http://example.com'))
      .pipe(uglify())
      .pipe(concat('js.min.js'))
      .pipe(gulp.dest('content/bundles/'))
      .pipe(gzip(gzip_options))
      .pipe(gulp.dest('content/bundles/'));
});

你也可以只对你期望模式所在的文件执行 gulp.src,并通过 gulp-replace 将它们单独流式传输,将其与 gulp.src 之后所有其他文件的流.

You could also do gulp.src just on the files you expect the pattern to be in, and stream them seperately through gulp-replace, merging it with a gulp.src stream of all the other files afterwards.

这篇关于如何使用 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 在一年的第一天返回前一年)