首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gulp useref如何添加额外的文件?

使用gulp useref如何添加额外的文件?
EN

Stack Overflow用户
提问于 2017-02-21 18:27:51
回答 1查看 277关注 0票数 1

我的html文件如下:

代码语言:javascript
复制
...
<!-- build:js build/js/vendor.js -->
<script src="dep/angular/angular.js"></script>
<!-- endbuild -->

构建文件如下所示:

代码语言:javascript
复制
<script src="build/js/vendor.xxxxxxxx.js"></script>

现在我用gulp-angular-templatecache打包所有的视图文件,生成一个template.js,我想把这个文件添加到编译好的html文件里面,该怎么办呢?

我在additionalStreams的设置选项中找到了gulp-useref文件,但是我用来查找哪些是不想实现的功能。

EN

回答 1

Stack Overflow用户

发布于 2017-03-09 17:45:45

我用另一种方法解决了这个问题。

先在页面上写字:

代码语言:javascript
复制
<!-- build:templates --><!-- endbuild -->

在打包之前,使用gulp-replace替换为以下内容:

代码语言:javascript
复制
<!-- build:js build/js/templates.js -->
    <script src="build/js/templates.js"></script>
<!-- endbuild -->

最后,统一编译。

代码:

代码语言:javascript
复制
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'));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42364753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档