我如何使用gulp替换文件中的字符串? [英] How can I use gulp to replace a string in a file?
问题描述
我使用gulp uglify并准备好用于生产的javascript文件。我有这样的代码:
$ b $ pre $ 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:[
//这里有更多文件
'temp / js / app / appConfig.js',
'temp /js/app/appConstant.js',
//这里有更多文件
],
gulp.task('scripts',['clean-js'],函数(){
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 /'));
});
我需要做的是替换字符串:
dataServer:http:// localhost:3048,
with
dataServer:http://example.com,
$ c
在文件'temp / js / app / appConstant.js'中,
我在寻找一些建议。例如,也许我应该制作appConstant.js文件的副本,更改(不知道如何)并在js.src中包含appConstantEdited.js?
但是我我不知道如何制作文件的副本并替换文件中的字符串。
任何帮助你都会非常感激。
解决方案 Gulp流输入,做所有转换,然后流输出。在使用Gulp时,保存临时文件是AFAIK非惯用。
相反,您要找的是替换内容的流式方式。自己写一些东西会比较容易,或者你可以使用现有的插件。对我而言, gulp-replace
如果你想在所有文件中进行替换,很容易改变你的任务:
var replace = require('gulp-replace');
$ b $ 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 (gzip_options))
.pipe(gulp.dest('content / bundles /'))
.pipe(gzip_options) content / bundles /'));
});
您也可以执行 gulp.src
对你期望该模式的文件进行流式处理,并通过 gulp-replace
将它们单独流式处理,并将它与 gulp.src $ c合并$ c>之后的所有其他文件的流。
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",
with
dataServer: "http://example.com",
In the file 'temp/js/app/appConstant.js',
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?
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 streams input, does all transformations, and then streams output. Saving temporary files in between is AFAIK non-idiomatic when using Gulp.
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/'));
});
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替换文件中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!