npm.io
1.0.1 • Published 1 year ago

gulp-renew

Licence
MIT
Version
1.0.1
Deps
3
Size
10 kB
Vulns
0
Weekly
0
Stars
1

gulp-renew

version Codecov release node.js license

[ English | 中文 ]

A plugin that supports batch string replacement for gulp.
Replacement rules support regular expressions and function replacements.

Based on gulp-replace

Installation

npm install --save-dev gulp-renew

Usage

const gulp = require('gulp');
const renew = require('gulp-renew');

gulp.task('default', function() {
  return gulp.src('src/**/*.js')
   .pipe(renew([
      { search: 'oldText', replacement: 'newText' },
      { search: /oldPattern/g, replacement: 'newPattern' }
    ]))
   .pipe(gulp.dest('dist'));
});

gulp.task('html', function() {
  return gulp.src('src/**/*.html')
   .pipe(renew([
      [‘foo1’, 'bar1'],
      [/foo2/g, 'bar2'],
      [‘foo3’, 'bar3']
    ]))
   .pipe(gulp.dest('dist'));
});

API

renew(replacements[, options])
replacements

Contains the rules for replacement. Each rule is an object containing the search and replacement properties.

type: Array<{ search: string | RegExp, replacement: string | Function }>

  • search:The string or regular expression to search for.
  • replacement:The string or function to replace with.
options

Options.

Type: Object

options.skipBinary

Whether to skip binary files.

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.

示例:

  • String replacement:
# Writing style 1renew([
  { search: 'foo', replacement: 'bar' }
])

# Write style 2renew([
  ['foo', 'bar']
])

# Write style 3renew({ 'foo1': 'bar1', 'foo2': 'bar2' })
  • Regular expression replacement:
renew({ search: /oldPattern/g, replacement: 'newPattern' })
  • Function replacement:
renew([
  {
    search: 'oldText',
    replacement: function (match) {
      return match.toUpperCase()
    },
  },
  {
    search: /foo(.{3})/g,
    replacement: function (match, p1, offset, string) {
      return 'bar' + p1
    },
  },
])

Keywords