Inject variable into html via Gulp Task

2019-08-03 04:02发布

问题:

I'm using Gulp and a number of plugins to template my HTML and minify my HTML i.e. "gulp-file-include", "gulp-inject", "gulp-minify-html" none seem to offer a way of injecting a constant at build time i.e something like this:

.pipe('*.html', $.gulpVar({urlpath: 'www.myurl.com'})))

then in my HTML a reference to the variable

Contact Us

The closest I can find to something similar is gulp-ng-constant although this injects it into the JS and uses Angular, but I'm not using Angular and wish to inject it into the HTML. Is there a gulp plugin that has that feature?

回答1:

you can use gulp-replace

gulp.task('templates', function(){
  gulp.src('template.html')
    .pipe(replace(/$$myvar$$/g, 'release'))
    .pipe(gulp.dest('build/file.txt'));
});

above task is replacing $$myvar$$ by release in template.html