我的html文件如下:
...
<!-- build:js build/js/vendor.js -->
<script src="dep/angular/angular.js"></script>
<!-- endbuild -->构建文件如下所示:
<script src="build/js/vendor.xxxxxxxx.js"></script>现在我用gulp-angular-templatecache打包所有的视图文件,生成一个template.js,我想把这个文件添加到编译好的html文件里面,该怎么办呢?
我在additionalStreams的设置选项中找到了gulp-useref文件,但是我用来查找哪些是不想实现的功能。
发布于 2017-03-09 17:45:45
我用另一种方法解决了这个问题。
先在页面上写字:
<!-- build:templates --><!-- endbuild -->在打包之前,使用gulp-replace替换为以下内容:
<!-- build:js build/js/templates.js -->
<script src="build/js/templates.js"></script>
<!-- endbuild -->最后,统一编译。
代码:
var indexHtmlFilter = $.filter(['**/*', '!**/index_dev.html'], {
restore: true
});
var fontglyphiconsMatch = /^\.\.\/fonts\/glyphicons/;
var fontawesomeMatch = /^\.\.\/fonts\/fontawesome-/;
var imgMatch = /^\.\.\/img\//;
var matchUrlType = function(url){
if(fontglyphiconsMatch.test(url))
return url.replace(/^\.\./, '/dep/bootstrap-css-only');
if(fontawesomeMatch.test(url))
return url.replace(/^\.\./, '/dep/font-awesome');
if(imgMatch.test(url))
return url.replace(/^\.\./, '');
};
gulp.src('app/index_dev.html')
.pipe($.htmlReplace({
templates: `
<!-- build:js build/js/templates.js -->
<script src="build/js/templates.js"></script>
<!-- endbuild -->
`
}))
.pipe($.useref())
.pipe($.if('*.js', $.uglify({
output: {
ascii_only: true
}
})))
.pipe($.if('*.css', $.modifyCssUrls({
modify: matchUrlType
})))
.pipe($.if('*.css', $.cleanCss()))
.pipe(indexHtmlFilter)
.pipe($.rev())
.pipe($.revFormat({
prefix: '.'
}))
.pipe(indexHtmlFilter.restore)
.pipe($.revReplace())
.pipe($.if('*.html', $.htmlmin({
collapseWhitespace: true
})))
.pipe($.if('index_dev.html', $.rename('index.html')))
.pipe(gulp.dest('./app'));https://stackoverflow.com/questions/42364753
复制相似问题