Replaces strings in files by using string or regex patterns. Works with Gulp 3!
Usage
npm install gulp-string-replace --save-dev
Regex Replace
var replace = require('gulp-string-replace'); gulp.task('replace_1', function() { gulp.src(["./config.js"]) // Every file allown. .pipe(replace(new RegExp('@env@', 'g'), 'production')) .pipe(gulp.dest('./build/config.js')) }); gulp.task('replace_2', function() { gulp.src(["./index.html"]) // Every file allown. .pipe(replace(/version(={1})/g, '$1v0.2.2')) .pipe(gulp.dest('./build/index.html')) }); gulp.task('replace_3', function() { gulp.src(["./config.js"]) // Every file allown. .pipe(replace(/foo/g, function () { return 'bar'; })) .pipe(gulp.dest('./build/config.js')) });
String Replace
gulp.task('replace_1', function() { gulp.src(["./config.js"]) // Every file allown. .pipe(replace('environment', 'production')) .pipe(gulp.dest('./build/config.js')) });
Function Replace
gulp.task('replace_1', function() { gulp.src(["./config.js"]) // Every file allown. .pipe(replace('environment', function () { return 'production'; })) .pipe(gulp.dest('./build/config.js')) }); gulp.task('replace_2', function() { gulp.src(["./config.js"]) // Every file allown. .pipe(replace('environment', function (replacement) { return replacement + '_mocked'; })) .pipe(gulp.dest('./build/config.js')) });
API
replace(string, replacement)
string
Type: String
The string to search for.
replacement
Type: String or Function
The replacement string or function. Called once for each match.
replace(regex, replacement)
regex
Type: RegExp
More details here: MDN documentation for RegExp.
replacement
Type: String or Function
More details here: MDN documentation for String.replace.
Release History
- 2016-03-09 v0.1.0 Initial version of plugin.
Task submitted by Tomasz Czechowski