Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
gulp-replace
Advanced tools
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.
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'));
});
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 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 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.
A string replace plugin for gulp
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
:
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('bar', 'foo'))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
// See https://mdn.io/string.replace#Specifying_a_string_as_a_parameter
.pipe(replace(/foo(.{3})/g, '$1foo'))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
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;
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) {
// Replace foobaz with barbaz and log a ton of information
// See https://mdn.io/string.replace#Specifying_a_function_as_a_parameter
console.log('Found ' + match + ' with param ' + p1 + ' at ' + offset + ' inside of ' + string);
return 'bar' + p1;
}))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
const replace = require('gulp-replace');
const { src, dest } = require('gulp');
function replaceTemplate() {
return src(['file.txt'])
.pipe(replace('filename', function handleReplace() {
// Replaces instances of "filename" with "file.txt"
// this.file is also available for regex replace
// See https://github.com/gulpjs/vinyl#instance-properties for details on available properties
return this.file.relative;
}))
.pipe(dest('build/'));
};
exports.replaceTemplate = replaceTemplate;
gulp-replace
can be called with a string or regex.
CAUTION:
replacement
could NOT be arrow function, because arrow function could not bindthis
Type: String
The string to search for.
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.
Type: RegExp
The regex pattern to search for. See the MDN documentation for RegExp for details.
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.
An optional third argument, options
, can be passed.
Type: Object
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
.
FAQs
A string replace plugin for gulp
The npm package gulp-replace receives a total of 198,487 weekly downloads. As such, gulp-replace popularity was classified as popular.
We found that gulp-replace demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.