Javascript 使用gulp useref如何添加额外文件?
我喜欢这样的html文件:Javascript 使用gulp useref如何添加额外文件?,javascript,angularjs,node.js,gulp,gulp-useref,Javascript,Angularjs,Node.js,Gulp,Gulp Useref,我喜欢这样的html文件: ... <!-- build:js build/js/vendor.js --> <script src="dep/angular/angular.js"></script> <!-- endbuild --> <script src="build/js/vendor.xxxxxxxx.js"></script> <!-- build:js build/js/templates.js -
...
<!-- build:js build/js/vendor.js -->
<script src="dep/angular/angular.js"></script>
<!-- endbuild -->
<script src="build/js/vendor.xxxxxxxx.js"></script>
<!-- build:js build/js/templates.js -->
<script src="build/js/templates.js"></script>
<!-- endbuild -->
。。。
像这样构建文件:
...
<!-- build:js build/js/vendor.js -->
<script src="dep/angular/angular.js"></script>
<!-- endbuild -->
<script src="build/js/vendor.xxxxxxxx.js"></script>
<!-- build:js build/js/templates.js -->
<script src="build/js/templates.js"></script>
<!-- endbuild -->
现在我用gulp angular templatecache
打包所有的视图文件,生成一个template.js,我想把这个文档添加到编译好的html文件里面,怎么做
我在
additionalStreams
设置选项中找到了gulp useref
文档,但我曾经找到了不希望实现该功能的内容。我用另一种方法解决了它
首先在页面上写下:
<!-- build:templates --><!-- 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'));
var indexHtmlFilter=$.filter(['***/','!***/index_dev.html']{
恢复:正确
});
var fontlyphiconsmatch=/^\.\.\/font\/glyphicons/;
var fontawesomeMatch=/^\.\.\/fonts\/fontawesomeMatch-/;
变量imgMatch=/^\.\.\/img\/;
var matchUrlType=函数(url){
if(fontglyphiconsMatch.test(url))
返回url.replace(/^\.\./,'/dep/bootstrap css only');
if(fontawesomeMatch.test(url))
返回url.replace(/^\.\./,'/dep/font-awesome');
if(imgMatch.test(url))
返回url.replace(/^\.\./,'');
};
gulp.src('app/index_dev.html'))
.pipe($.htmlReplace({
模板:`
`
}))
.pipe($.useref())
.pipe($.if('*.js',$.uglify({
输出:{
仅ascii_:正确
}
})))
.pipe($.if('*.css',$.modifyCssUrls({
修改:matchUrlType
})))
.pipe($.if('*.css',$.cleanCss()))
.管道(indexHtmlFilter)
.pipe($.rev())
.pipe($.revFormat)({
前缀:'.'
}))
.pipe(indexHtmlFilter.restore)
.pipe($.revReplace())
.pipe($.if('*.html',$.htmlmin({
collapseWhitespace:true
})))
.pipe($.if('index_dev.html',$.rename('index.html'))
.管道(吞咽目的地('./应用');