1

我的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文件里面,怎么做呢?

gulp-useref在settings options的文件里找到了additionalStreams,但是我用的时候找到了没有什么想要实现的功能。

4

1 回答 1

1

我用另一种方式解决了它。

先在页面上写:

<!-- 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'));
于 2017-03-09T09:45:44.760 回答