Inject variable into html via Gulp Task

2019-08-03 03:22发布

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条回答
迷人小祖宗
2楼-- · 2019-08-03 04:14

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

查看更多
登录 后发表回答