gulp-replace
Advanced tools
Comparing version 1.0.0 to 1.1.0
64
index.js
'use strict'; | ||
var Transform = require('readable-stream/transform'); | ||
var rs = require('replacestream'); | ||
var istextorbinary = require('istextorbinary'); | ||
const Transform = require('stream').Transform; | ||
const rs = require('replacestream'); | ||
const istextorbinary = require('istextorbinary'); | ||
const Vinyl = require('vinyl') | ||
const defaultOptions = { | ||
skipBinary: true | ||
} | ||
module.exports = function(search, _replacement, options) { | ||
if (!options) { | ||
options = {}; | ||
module.exports = function(search, _replacement, options = {}) { | ||
// merge options | ||
options = { | ||
...defaultOptions, | ||
...options | ||
} | ||
if (options.skipBinary === undefined) { | ||
options.skipBinary = true; | ||
} | ||
return new Transform({ | ||
objectMode: true, | ||
transform: function(file, enc, callback) { | ||
/** | ||
* transformation | ||
* @param {Vinyl} file | ||
* @param {BufferEncoding} enc | ||
* @param {(error?: Error | null, data?: any) => void} callback | ||
*/ | ||
transform(file, enc, callback) { | ||
if (file.isNull()) { | ||
@@ -23,3 +31,3 @@ return callback(null, file); | ||
var replacement = _replacement; | ||
let replacement = _replacement; | ||
if (typeof _replacement === 'function') { | ||
@@ -38,8 +46,7 @@ // Pass the vinyl file object as this.file | ||
if (search instanceof RegExp) { | ||
file.contents = new Buffer(String(file.contents).replace(search, replacement)); | ||
} | ||
else { | ||
var chunks = String(file.contents).split(search); | ||
file.contents = Buffer.from(String(file.contents).replace(search, replacement)); | ||
} else { | ||
const chunks = String(file.contents).split(search); | ||
var result; | ||
let result; | ||
if (typeof replacement === 'function') { | ||
@@ -52,3 +59,3 @@ // Start with the first chunk already in the result | ||
// The replacement function should be called once for each match | ||
for (var i = 1; i < chunks.length; i++) { | ||
for (let i = 1; i < chunks.length; i++) { | ||
// Add the replacement value | ||
@@ -67,3 +74,3 @@ result.push(replacement(search)); | ||
file.contents = new Buffer(result); | ||
file.contents = Buffer.from(result); | ||
} | ||
@@ -76,15 +83,8 @@ return callback(null, file); | ||
if (options && options.skipBinary) { | ||
istextorbinary.isText(file.path, file.contents, function(err, result) { | ||
if (err) { | ||
return callback(err, file); | ||
} | ||
if (!result) { | ||
callback(null, file); | ||
} else { | ||
doReplace(); | ||
} | ||
}); | ||
if (options.skipBinary) { | ||
if (!istextorbinary.isText(file.path, file.contents)) { | ||
callback(null, file); | ||
} else { | ||
doReplace(); | ||
} | ||
return; | ||
@@ -91,0 +91,0 @@ } |
{ | ||
"name": "gulp-replace", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A string replace plugin for gulp", | ||
"dependencies": { | ||
"istextorbinary": "2.2.1", | ||
"readable-stream": "^2.0.1", | ||
"replacestream": "^4.0.0" | ||
"istextorbinary": "^5.12.0", | ||
"replacestream": "^4.0.3", | ||
"yargs-parser": ">=5.0.0-security.0" | ||
}, | ||
"devDependencies": { | ||
"concat-stream": "^1.5.2", | ||
"mocha": "^5.1.1", | ||
"should": "^13.2.1", | ||
"vinyl": "^2.1.0" | ||
"concat-stream": "^2.0.0", | ||
"mocha": "^8.2.1", | ||
"should": "^13.2.3", | ||
"vinyl": "^2.2.1" | ||
}, | ||
@@ -19,2 +19,4 @@ "scripts": { | ||
}, | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"repository": { | ||
@@ -34,4 +36,4 @@ "type": "git", | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=10" | ||
} | ||
} |
118
README.md
# gulp-replace [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] | ||
> A string replace plugin for gulp 3 | ||
> A string replace plugin for gulp | ||
[Read me for gulp 3](README-gulp3.md) | ||
## Usage | ||
@@ -10,2 +13,4 @@ | ||
npm install --save-dev gulp-replace | ||
# or | ||
yarn add --dev gulp-replace | ||
``` | ||
@@ -16,78 +21,97 @@ | ||
### Simple string replace | ||
```javascript | ||
var replace = require('gulp-replace'); | ||
const replace = require('gulp-replace'); | ||
const { src, dest } = require('gulp'); | ||
gulp.task('templates', function(){ | ||
gulp.src(['file.txt']) | ||
function replaceTemplate() { | ||
return src(['file.txt']) | ||
.pipe(replace('bar', 'foo')) | ||
.pipe(gulp.dest('build/')); | ||
}); | ||
.pipe(dest('build/')); | ||
}; | ||
exports.replaceTemplate = replaceTemplate; | ||
``` | ||
### Simple regex replace | ||
```javascript | ||
var replace = require('gulp-replace'); | ||
const replace = require('gulp-replace'); | ||
const { src, dest } = require('gulp'); | ||
gulp.task('templates', function(){ | ||
gulp.src(['file.txt']) | ||
// See http://mdn.io/string.replace#Specifying_a_string_as_a_parameter | ||
.pipe(replace(/foo(.{3})/g, '$1foo')) | ||
.pipe(gulp.dest('build/')); | ||
}); | ||
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; | ||
``` | ||
### String replace with function callback | ||
```javascript | ||
var replace = require('gulp-replace'); | ||
const replace = require('gulp-replace'); | ||
const { src, dest } = require('gulp'); | ||
gulp.task('templates', function(){ | ||
gulp.src(['file.txt']) | ||
.pipe(replace('foo', function(match) { | ||
// Replaces instances of "foo" with "oof" | ||
return match.reverse(); | ||
})) | ||
.pipe(gulp.dest('build/')); | ||
}); | ||
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 | ||
```javascript | ||
var replace = require('gulp-replace'); | ||
const replace = require('gulp-replace'); | ||
const { src, dest } = require('gulp'); | ||
gulp.task('templates', function(){ | ||
gulp.src(['file.txt']) | ||
.pipe(replace(/foo(.{3})/g, function(match, p1, offset, string) { | ||
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 http://mdn.io/string.replace#Specifying_a_function_as_a_parameter | ||
// 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(gulp.dest('build/')); | ||
}); | ||
.pipe(dest('build/')); | ||
}; | ||
exports.replaceTemplate = replaceTemplate; | ||
``` | ||
### Function callback with file object | ||
```javascript | ||
var replace = require('gulp-replace'); | ||
const replace = require('gulp-replace'); | ||
const { src, dest } = require('gulp'); | ||
gulp.task('templates', function(){ | ||
gulp.src(['file.txt']) | ||
.pipe(replace('filename', function() { | ||
// 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(gulp.dest('build/')); | ||
}); | ||
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; | ||
``` | ||
## API | ||
gulp-replace can be called with a string or regex. | ||
`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` | ||
@@ -98,2 +122,3 @@ | ||
#### replacement | ||
Type: `String` or `Function` | ||
@@ -108,2 +133,3 @@ | ||
#### regex | ||
Type: `RegExp` | ||
@@ -114,2 +140,3 @@ | ||
#### replacement | ||
Type: `String` or `Function` | ||
@@ -126,17 +153,18 @@ | ||
#### 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 | ||
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`. | ||
[MDN documentation for RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | ||
[MDN documentation for String.replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter | ||
[travis-url]: http://travis-ci.org/lazd/gulp-replace | ||
[travis-url]: https://travis-ci.org/lazd/gulp-replace | ||
[travis-image]: https://secure.travis-ci.org/lazd/gulp-replace.svg?branch=master | ||
[npm-url]: https://npmjs.org/package/gulp-replace | ||
[npm-image]: https://badge.fury.io/js/gulp-replace.svg |
14399
6
100
165
+ Addedbinaryextensions@4.19.0(transitive)
+ Addedistextorbinary@5.15.0(transitive)
+ Addedtextextensions@5.16.0(transitive)
+ Addedyargs-parser@21.1.1(transitive)
- Removedreadable-stream@^2.0.1
- Removedbinaryextensions@2.3.0(transitive)
- Removededitions@1.3.4(transitive)
- Removedistextorbinary@2.2.1(transitive)
- Removedtextextensions@2.6.0(transitive)
Updatedistextorbinary@^5.12.0
Updatedreplacestream@^4.0.3