|
| 1 | +# gulp-renew |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/gulp-renew) |
| 4 | +[](https://codecov.io/gh/Meqn/pipflow) |
| 5 | +[](https://github.com/Meqn/pipflow/releases) |
| 6 | +[](https://nodejs.org/en/about/releases/) |
| 7 | +[](https://github.com/Meqn/pipflow) |
| 8 | + |
| 9 | +A plugin that supports batch string replacement for gulp. |
| 10 | +Replacement rules support regular expressions and function replacements. |
| 11 | + |
| 12 | +> Based on [gulp-replace](https://www.npmjs.com/package/gulp-replace) |
| 13 | +
|
| 14 | +## Installation |
| 15 | + |
| 16 | +``` |
| 17 | +npm install --save-dev gulp-renew |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +```js |
| 23 | +const gulp = require('gulp'); |
| 24 | +const renew = require('gulp-renew'); |
| 25 | + |
| 26 | +gulp.task('default', function() { |
| 27 | + return gulp.src('src/**/*.js') |
| 28 | + .pipe(renew([ |
| 29 | + { search: 'oldText', replacement: 'newText' }, |
| 30 | + { search: /oldPattern/g, replacement: 'newPattern' } |
| 31 | + ])) |
| 32 | + .pipe(gulp.dest('dist')); |
| 33 | +}); |
| 34 | + |
| 35 | +gulp.task('html', function() { |
| 36 | + return gulp.src('src/**/*.html') |
| 37 | + .pipe(renew([ |
| 38 | + [‘foo1’, 'bar1'], |
| 39 | + [/foo2/g, 'bar2'], |
| 40 | + [‘foo3’, 'bar3'] |
| 41 | + ])) |
| 42 | + .pipe(gulp.dest('dist')); |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +## API |
| 47 | + |
| 48 | +```js |
| 49 | +renew(replacements[, options]) |
| 50 | +``` |
| 51 | + |
| 52 | +### replacements |
| 53 | + |
| 54 | +Contains the rules for replacement. Each rule is an object containing the search and replacement properties. |
| 55 | +`type`: `Array<{ search: string | RegExp, replacement: string | Function }>` |
| 56 | + |
| 57 | +- `search`:The string or regular expression to search for. |
| 58 | +- `replacement`:The string or function to replace with. |
| 59 | + |
| 60 | +### options |
| 61 | + |
| 62 | +Options. |
| 63 | +`Type`: `Object` |
| 64 | + |
| 65 | +#### `options.skipBinary` |
| 66 | + |
| 67 | +Whether to skip binary files. |
| 68 | +`Type`: `boolean` |
| 69 | +`Default`: `true` |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +**示例:** |
| 74 | + |
| 75 | +- String replacement: |
| 76 | + |
| 77 | +```js |
| 78 | +# Writing style 1: |
| 79 | +renew([ |
| 80 | + { search: 'foo', replacement: 'bar' } |
| 81 | +]) |
| 82 | + |
| 83 | +# Write style 2: |
| 84 | +renew([ |
| 85 | + ['foo', 'bar'] |
| 86 | +]) |
| 87 | + |
| 88 | +# Write style 3 : |
| 89 | +renew({ 'foo1': 'bar1', 'foo2': 'bar2' }) |
| 90 | +``` |
| 91 | + |
| 92 | +- Regular expression replacement: |
| 93 | + |
| 94 | +```js |
| 95 | +renew({ search: /oldPattern/g, replacement: 'newPattern' }) |
| 96 | +``` |
| 97 | + |
| 98 | +- Function replacement: |
| 99 | + |
| 100 | +```js |
| 101 | +renew([ |
| 102 | + { |
| 103 | + search: 'oldText', |
| 104 | + replacement: function (match) { |
| 105 | + return match.toUpperCase() |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + search: /foo(.{3})/g, |
| 110 | + replacement: function (match, p1, offset, string) { |
| 111 | + return 'bar' + p1 |
| 112 | + }, |
| 113 | + }, |
| 114 | +]) |
| 115 | +``` |
0 commit comments