What is gulp-replace?
The gulp-replace npm package is a plugin for Gulp, a streaming build system, that allows you to search and replace text in files. It is useful for tasks such as modifying configuration files, updating version numbers, or replacing placeholders in templates.
What are gulp-replace's main functionalities?
Basic String Replacement
This feature allows you to replace a specific string with another string in the files matched by the gulp.src() method. In this example, all occurrences of 'foo' in text files within the 'src' directory are replaced with 'bar' and the modified files are saved to the 'dist' directory.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-text', function() {
return gulp.src('src/*.txt')
.pipe(replace('foo', 'bar'))
.pipe(gulp.dest('dist'));
});
Regular Expression Replacement
This feature allows you to use regular expressions for more complex search and replace operations. In this example, any string matching the pattern 'foo' followed by one or more digits is replaced with 'bar'.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-regex', function() {
return gulp.src('src/*.txt')
.pipe(replace(/foo\d+/g, 'bar'))
.pipe(gulp.dest('dist'));
});
Replacement with a Function
This feature allows you to use a function to determine the replacement value. The function receives the matched string and can return a modified version of it. In this example, all occurrences of 'foo' are replaced with 'FOO'.
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('replace-function', function() {
return gulp.src('src/*.txt')
.pipe(replace('foo', function(match) {
return match.toUpperCase();
}))
.pipe(gulp.dest('dist'));
});
Other packages similar to gulp-replace
gulp-string-replace
gulp-string-replace is another Gulp plugin that provides similar functionality for string replacement. It allows for both simple string replacements and replacements using regular expressions. Compared to gulp-replace, it offers a more straightforward API but lacks some of the advanced features like replacement with a function.
gulp-replace-task
gulp-replace-task is a Gulp plugin designed for replacing text in files using a configuration object. It supports both string and regular expression replacements and allows for more complex replacement tasks through the use of configuration files. It is more suitable for projects that require extensive and configurable replacement tasks compared to gulp-replace.
gulp-batch-replace
gulp-batch-replace is a Gulp plugin that allows for batch replacements in files. It supports multiple replacements in a single pass and can handle both string and regular expression replacements. This package is useful for projects that need to perform multiple replacements efficiently, whereas gulp-replace is more focused on individual replacement tasks.
gulp-replace
A string replace plugin for gulp 3
Usage
First, install gulp-replace
as a development dependency:
npm install --save-dev gulp-replace
Then, add it to your gulpfile.js
:
var replace = require('gulp-replace');
gulp.task('templates', function(){
gulp.src(['file.txt'])
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(gulp.dest('build/file.txt'));
});
API
gulp-replace can be called with a string or regex.
replace(string, replacement[, options])
string
Type: String
The string to search for.
replacement
Type: String
or Function
The replacement string or function. If replacement
is a function, it will be called once for each match and will be passed the string that is to be replaced.
replace(regex, replacement[, options])
Note: gulp-replace cannot perform regex replacement on streams.
regex
Type: RegExp
The regex pattern to search for. See the MDN documentation for RegExp for details.
replacement
Type: String
or Function
The replacement string or function. See the MDN documentation for String.replace for details.
gulp-replace options
An optional third argument, options
, can be passed.
options
Type: Object
options.skipBinary
Type: boolean
Default: false
Skip binary files