gpt4 book ai didi

javascript - 基于html属性为gulp构建过程添加条件

转载 作者:可可西里 更新时间:2023-11-01 13:48:18 25 4
gpt4 key购买 nike

是否可以添加一个条件,使我项目中所有 html 文件中的每个 img 标记都应该具有另一个属性,如 my-custom-directive

我有一个 Angular Directive(指令),它修改我的 web 应用程序中相对 img src URL 的基本 URL,我想防止我的团队成员在他们编写的 html 模板中丢失此属性。

最佳答案

你可以使用 gulp-replace更改每个标签...

var replace = require('gulp-replace');

gulp.task('pages', function(){
gulp.src(['*.html'])
.pipe(replace(/<img/g, '<img my-custom-directive'))
.pipe(gulp.dest('build'));
});

这会将属性添加到每个图像。如果你想要更多的控制,你可以使用 gulp-tap获取整个文件的内容并对其进行处理。

var tap = require('gulp-tap');

gulp.task('pages', function(){
gulp.src(['*.html'])
.pipe(tap(function(file) {

// get current contents
let contents = file.contents.toString();

// do your conditional processing
// eg deal with each tag in a loop & only change those without the attribute
contents = contents.replace(/<img/g, '<img my-custom-directive');

// set new contents to flow through the gulp stream
file.contents = new Buffer(contents);
}))
.pipe(gulp.dest('build'));
});

关于javascript - 基于html属性为gulp构建过程添加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com