Gulp useref连接两次js文件

div*_*ero 1 angularjs gulp

我有这样的gulp任务

 gulp.task('html', [], function() {
    var partialsInjectFile = gulp.src(options.tmp + '/partials/templateCacheHtml.js', {read: false});
    var partialsInjectOptions = {
        starttag: '<!-- inject:partials -->',
        ignorePath: options.tmp + '/partials',
        addRootSlash: false
    };

    var htmlFilter = $.filter('*.html');
    var jsFilter = $.filter('**/*.js');
    var cssFilter = $.filter('**/*.css');
    var assets;

    return gulp.src(options.tmp + '/serve/*.html')
        .pipe($.inject(partialsInjectFile, partialsInjectOptions))
        .pipe(assets = $.useref.assets())
        .pipe($.rev())
        .pipe(jsFilter)
        .pipe($.ngAnnotate())
        .pipe($.uglify({preserveComments: $.uglifySaveLicense})).on('error', options.errorHandler('Uglify'))
        .pipe(jsFilter.restore())
        .pipe(cssFilter)
        .pipe($.csso())
        .pipe(cssFilter.restore())
        .pipe(assets.restore())
        .pipe($.useref())
        .pipe($.revReplace())
        .pipe(htmlFilter)
        .pipe($.minifyHtml({
            empty: true,
            spare: true,
            quotes: true,
            conditionals: true
        }))
        .pipe(htmlFilter.restore())
        .pipe(gulp.dest(options.dist + '/'))
        .pipe($.size({title: options.dist + '/', showFiles: true}));
});
Run Code Online (Sandbox Code Playgroud)

由gulp angular yeoman生成,但我做了一些修改,将其与其他任务隔离开来,并将问题本地化.

问题是,在所有连接后产生app - %%%.js文件包含dublicate代码:原始代码,例如缩小的代码

 function() {
    "use strict";
    function e(e, t) {
        return e(t + "/something", {}, {get: {method: "get", isArray: !0}})
     }

    angular.module("app.mappings").factory("Something", e), e.$inject = ["$resource", "API_ROOT"]
    }(),

 angular.module("app.mappings").factory("Something", Something), Something.$inject = ["$resource", "API_ROOT"] 
Run Code Online (Sandbox Code Playgroud)

这打破了我的申请.

有人可以帮助理解这个构建任务有什么问题吗?

PS似乎用html注入的脚本看起来像

<script src="app/mappings/mappings.module.js"></script>
Run Code Online (Sandbox Code Playgroud)

并且.tmp/serve /目录和src /目录都具有相同的结构.Useref搜索两者并重复发生

And*_*Gis 10

我意识到问题有点陈旧,但我刚遇到同样的问题.

重复的原因是index.html中的行:

<!-- build:js({.tmp/serve,.tmp/partials,src}) scripts/app.js -->
...
<!-- endbuild -->
Run Code Online (Sandbox Code Playgroud)

Gulp-useref查找这些行并将其转换为:

从:

<html>
<body>
    <!-- build:js scripts/combined.js -->
    <script type="text/javascript" src="scripts/one.js"></script>
    <script type="text/javascript" src="scripts/two.js"></script>
    <!-- endbuild -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

至:

<html>
<body>
    <script src="scripts/combined.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

它根据注释在路径中查找脚本文件

建立:JS({.TMP /服务,的.tmp /分音,SRC})

所以在这种情况下有3个地点:

  • .TMP /服务
  • .TMP /谐音
  • SRC

如果您的构建脚本从复制的文件src,以.tmp/serve在此期间,useref发现在这两个位置的文件,所以它们粘贴2次结果文件.因此修复是要么修改构建脚本而不是复制文件或将index.html中的那一行更改为:

<!-- build:js(.tmp/serve) scripts/app.js -->
Run Code Online (Sandbox Code Playgroud)

我想这可能不会是你5年后的帮助,但希望其他人会发现它有用.