Inject variable into html via Gulp Task

冷暖自知 提交于 2019-12-11 13:59:16

问题


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



来源:https://stackoverflow.com/questions/30703688/inject-variable-into-html-via-gulp-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!