Install package with NPM and add it to your development dependencies:
npm install --save-dev gulp-replace-it
var replace = require('gulp-replace-it');
gulp.task('replace', function () {
return gulp.src('./template.js')
.pipe(replace({
placeholderTemplate: '/* _ */',
with: {
token: 'hello world'
}
}))
.pipe(gulp.dst('./result.js'));
});
Having the following template.js
:
console.log(/* token */);
you'll get console.log('hello world')
in your result.js
.
gulp-replace-it
also supports buffers and streams:
var fs = require('fs');
var buffer = fs.readFileSync('./license');
var stream = gulp.src('./inner.js');
gulp.src('./template.js')
.pipe(replace({
with: {
license: buffer,
code: stream
}
})
.pipe(gulp.dst('./result.js'));
license
:
/* MIT LICENSE */
inner.js
:
console.log('hello world');
template.js
:
{{ license }}
function main() {
{{ code }}
}
main();
result.js
:
/* MIT LICENSE */
function main() {
console.log('hello world');
}
main();
-
placeholderTemplate
optional A pattern for placeholder. Can't contain alphabetical characters. Examples:/* _ */
,# _ #
,// _ \\
.{{ _ }}
is default value. -
with
an object that stores token/data mapping .