• Stars
    star
    496
  • Rank 88,807 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A string replace plugin for gulp

gulp-replace NPM version Build status

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/'));
};

// or replace multiple strings
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'])
      // 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

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) {
      // 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;

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() {
         // 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.

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.

More Repositories

1

iNoBounce

Stop your iOS webapp from bouncing around when scrolling
JavaScript
1,334
star
2

mdn.io

The "I'm feeling lucky" URL shortener
JavaScript
215
star
3

gulp-handlebars

Handlebars plugin for gulp
JavaScript
171
star
4

gulp-karma

Karma plugin for gulp
JavaScript
75
star
5

gulp-csslint

CSSLint plugin for gulp
JavaScript
74
star
6

PseudoClass

An OOP framework for Node.js and the browser
JavaScript
66
star
7

DOMly

The fast template system that creates and clones DOM nodes
JavaScript
53
star
8

wellTested

A simple todo app tested with Karma, Jasmine, CasperJS, and Istanbul
JavaScript
45
star
9

scopedQuerySelectorShim

querySelector/querySelectorAll shims that enable the use of :scope
JavaScript
41
star
10

iOCSS

Simple, resolution independent, image-free CSS for mobile web applications
CSS
36
star
11

F

F, a Backbone component framework that encourages code re-use
JavaScript
23
star
12

Pinky

A straight-forward Promises/A+ 1.1 implementation
JavaScript
22
star
13

VoodooI2CGoodix

VoodooI2C satellite driver for Goodix touchscreens in C++
C++
20
star
14

gulp-declare

Safely declare namespaces and set their properties
JavaScript
17
star
15

quadtag

An Arduino-based laser tag system for quadcopters
Arduino
15
star
16

DuneBuggy

A 3D multiplayer dune buggy battle game written entirely in JavaScript
JavaScript
10
star
17

flexless

LESS mixins for the CSS Flexible Box Layout Module 2012 Editor's Draft with optional fallbacks for the 2009 W3C Working Draft.
9
star
18

karma-benchmark-reporter

A jsPerf-style reporter for karma-benchmark
JavaScript
5
star
19

TankGame

A 3D multiplayer tank battle game written entirely in JavaScript
JavaScript
5
star
20

nsdeclare

Safely declare a namespace using dot notation
JavaScript
5
star
21

stompinator

Elephant activity tracking (upstairs neighbor stomping) device for M5Stack Core2
C++
5
star
22

coronaglobe

Heatmap representation of COVID-19 on WebGl 3D world map
JavaScript
5
star
23

wolf4sdl-controller-mappings

A selection of controller mappings for wolf4sdl
4
star
24

fui

A flat UI library for the web
JavaScript
4
star
25

karma-benchmark-example

A karma-benchmark example
JavaScript
4
star
26

platformer

A platformer experiment
JavaScript
3
star
27

piControl

A client-server system for controlling the Raspberry Pi
JavaScript
3
star
28

gulp-karma-test

A repository dedicated to figuring out how to use Karma and gulp together without wanting to off one's self with a rusty spoon
JavaScript
3
star
29

coronadatascraper-cache

Cache for the coronadatascraper project
HTML
2
star
30

lazd.github.io

lazd's blog
HTML
2
star
31

shellcolor

ANSI colors for your Node.js app's stdout, stderror, and logs
JavaScript
2
star
32

commonesm

How to publish a module, maintain support for CommonJS, and test with jest
JavaScript
1
star
33

DOMly-cli

JavaScript
1
star
34

dilBot-rc

JavaScript
1
star
35

gulp-domly

DOMly plugin for gulp 3
JavaScript
1
star
36

coronavirus-data-sources

A list of coronavirus data sources by county
1
star
37

SimpleExpressServer

An express+Stylus server ala Python's SimpleHTTPServer
JavaScript
1
star
38

expressSeed

A Node.js + express app structure
1
star
39

grunt-domly

Grunt task to precompile DOMly templates
JavaScript
1
star
40

wolf4sdl

Wolf4SDL is an open-source port of id Software's classic first-person shooter Wolfenstein 3D to the cross-platform multimedia library SDL. It is meant to keep the original feel while taking advantage of some improvements.
C++
1
star