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
Read me for gulp 3
Usage
First, install gulp-replace
as a development dependency:
npm install --save-dev gulp-replace
# or
yarn add --dev gulp-replace
Then, add it to your gulpfile.js
:
Simple string replace
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('bar', 'foo'))
.pipe(dest('build/'));
};
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('bar', 'foo'))
.pipe(replace('baz', 'fuz'))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
Simple regex replace
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
String replace with function callback
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('foo', function handleReplace(match){ return match.reverse(); })
.pipe(dest('build/'))
};
exports.replaceTemplate = replaceTemplate;
Regex replace with function callback
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace(/foo(.{3})/g, function handleReplace(match, p1, offset, string) {
console.log('Found ' + match + ' with param ' + p1 + ' at ' + offset + ' inside of ' + string);
return 'bar' + p1;
}))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
Function callback with file object
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('filename', function handleReplace() {
return this.file.relative;
}))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
API
gulp-replace
can be called with a string or regex.
replace(string, replacement[, options])
CAUTION: replacement
could NOT be arrow function, because arrow function could not bind this
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.
The value of this.file
will be equal to the vinyl instance for the file being processed.
replace(regex, replacement[, options])
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 on special replacement string patterns and arguments to the replacement function.
The value of this.file
will be equal to the vinyl instance for the file being processed.
gulp-replace options
An optional third argument, options
, can be passed.
options
Type: Object
options.skipBinary
Type: boolean
Default: true
Skip binary files. This option is true
by default. If you want to replace content in binary files, you must explicitly set it to false
.