https://github.com/outatime/gulp-replace-task
Replace text patterns with applause.
https://github.com/outatime/gulp-replace-task
Last synced: 11 months ago
JSON representation
Replace text patterns with applause.
- Host: GitHub
- URL: https://github.com/outatime/gulp-replace-task
- Owner: outaTiME
- License: mit
- Created: 2014-03-20T04:19:18.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-05-04T00:17:02.000Z (about 5 years ago)
- Last Synced: 2025-07-17T12:10:43.798Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 31.3 KB
- Stars: 59
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# gulp-replace-task
[](https://github.com/outaTiME/gulp-replace-task/actions/workflows/main.yml)
[](https://www.npmjs.com/package/gulp-replace-task)

[](#)
[](http://commitizen.github.io/cz-cli/)
[](https://twitter.com/outa7iME)
> Replace text patterns with [applause](https://github.com/outaTiME/applause).
## Install
From NPM:
```shell
npm install gulp-replace-task --save-dev
```
## Usage
Assuming installation via NPM, you can use `gulp-replace-task` in your gulpfile like this:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
gulp.task('default', function () {
gulp
.src('src/index.html')
.pipe(
replace({
patterns: [
{
match: 'foo',
replacement: 'bar'
}
]
})
)
.pipe(gulp.dest('build'));
});
```
## Options
Supports all the applause [options](https://github.com/outaTiME/applause#options).
## Examples
### Basic
File `src/manifest.appcache`:
```
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
```
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
gulp.task('default', function () {
gulp
.src('src/manifest.appcache')
.pipe(
replace({
patterns: [
{
match: 'timestamp',
replacement: Date.now()
}
]
})
)
.pipe(gulp.dest('build'));
});
```
### Multiple matching
File `src/manifest.appcache`:
```
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
```
File `src/humans.txt`:
```
__ _
_ _/__ /./|,//_`
/_//_// /_|/// //_, outaTiME v.@@version
/* TEAM */
Web Developer / Graphic Designer: Ariel Oscar Falduto
Site: https://www.outa.im
Twitter: @outa7iME
Contact: afalduto at gmail dot com
From: Buenos Aires, Argentina
/* SITE */
Last update: @@timestamp
Standards: HTML5, CSS3, robotstxt.org, humanstxt.org
Components: H5BP, Modernizr, jQuery, Bootstrap, LESS, Jade, Grunt
Software: Sublime Text, Photoshop, LiveReload
```
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
var pkg = require('./package.json');
gulp.task('default', function () {
gulp
.src(['src/manifest.appcache', 'src/humans.txt'])
.pipe(
replace({
patterns: [
{
match: 'version',
replacement: pkg.version
},
{
match: 'timestamp',
replacement: Date.now()
}
]
})
)
.pipe(gulp.dest('build'));
});
```
### Cache busting
File `src/index.html`:
```html
```
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
gulp.task('default', function () {
gulp
.src('src/index.html')
.pipe(
replace({
patterns: [
{
match: 'timestamp',
replacement: Date.now()
}
]
})
)
.pipe(gulp.dest('build'));
});
```
### Include file
File `src/index.html`:
```html
@@include
```
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
var fs = require('fs');
gulp.task('default', function () {
gulp
.src('src/index.html')
.pipe(
replace({
patterns: [
{
match: 'include',
replacement: fs.readFileSync('./includes/content.html', 'utf8')
}
]
})
)
.pipe(gulp.dest('build'));
});
```
### Regular expression
File `src/username.txt`:
```
John Smith
```
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
gulp.task('default', function () {
gulp
.src('src/username.txt')
.pipe(
replace({
patterns: [
{
match: /(\w+)\s(\w+)/,
replacement: '$2, $1' // Replaces "John Smith" with "Smith, John"
}
]
})
)
.pipe(gulp.dest('build'));
});
```
### Lookup for `foo` instead of `@@foo`
Gulpfile:
```javascript
var gulp = require('gulp');
var replace = require('gulp-replace-task');
gulp.task('default', function () {
gulp
.src('src/foo.txt')
.pipe(
replace({
patterns: [
{
match: /foo/g, // Explicitly using a regexp
replacement: 'bar'
}
]
})
)
.pipe(
replace({
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
usePrefix: false // Using the option provided
})
)
.pipe(
replace({
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
prefix: '' // Removing the prefix manually
})
)
.pipe(gulp.dest('build'));
});
```
## Related
- [applause](https://github.com/outaTiME/applause) - Human-friendly replacements
## License
MIT © [outaTiME](https://outa.im)