Replace String From A JSON File.
npm install @yodasws/gulp-json-replace``bash`
yarn add --dev @yodasws/gulp-json-replace
`bash`
npm install @yodasws/gulp-json-replace --save-dev
html
%%title
%%title
%%author
`config.json
`javascript
{
"title": "JSON Replace for Gulp",
"author": "Sam Grundman"
}
`gulpfile.js:
`javascript
const replace = require('gulp-json-replace');gulp.task('json-replace', function() {
return gulp.src('src/*/.html')
.pipe(replace({
src: './config.json',
}))
.pipe(gulp.dest('dist/'));
});
`Result:
`html
JSON Replace for Gulp
JSON Replace for Gulp
Sam Grundman
`$3
index.html
`html
{title}
{openParagraph}
`gulpfile.js:
`javascript
gulp.task('json-replace', function() {
return gulp.src('src/*/.html')
.pipe(replace({
src: {
title: 'Great New Story!',
},
keepNoMatch: true,
prefix: '{',
suffix: '}',
}))
.pipe(gulp.dest('dist/'));
});
`Result:
`html
Great New Story!
{openParagraph}
`Options
$3
Type: String or Object
Accepts either the path of the JSON content file or a JavaScript object.$3
Type: String
Default: %%
The string to open the search field.$3
Type: String
Default:
The string to close the search field.$3
Type: Boolean
Default: false`