0

我有以下吞咽任务:

gulp.task('html', function () {
    // Compile templates
    return gulp.src(templateFiles)
        .pipe(htmlmin({
            removeComments: true,
            collapseWhitespace: true,
            conservativeCollapse: true,
            removeScriptTypeAttributes: true
        }))
        .pipe(ngHtml2Js({
            moduleName: 'my.tpls',
            prefix: 'tpl/'
        }))
        .pipe(concat(libName + '.tpls.js'))
        .pipe(gulp.dest(destDirectory));
});

这会在一个文件中生成几个代码块,类似于这些:

(function(module) {
try {
  module = angular.module('my.tpls');
} catch (e) {
  module = angular.module('my.tpls', []);
}
module.run(['$templateCache', function($templateCache) {
  $templateCache.put('tpl/my/templates/template1.html',
    '<some-html></<some-html>');
}]);
})();

(function(module) {
try {
  module = angular.module('my.tpls');
} catch (e) {
  module = angular.module('my.tpls', []);
}
module.run(['$templateCache', function($templateCache) {
  $templateCache.put('tpl/my/templates/template2.html',
    '<some-html></<some-html>');
}]);
})();

这似乎非常低效,并设置了许多不需要的额外字节来下载。

有没有办法调整 gulp 任务以使结果更像:

(function(module) {
try {
  module = angular.module('my.tpls');
} catch (e) {
  module = angular.module('my.tpls', []);
}
module.run(['$templateCache', function($templateCache) {
  $templateCache.put('tpl/my/templates/template1.html',
    '<some-html></<some-html>');
  $templateCache.put('tpl/my/templates/template2.html',
    '<some-html></<some-html>');
}]);
})();

澄清; 我正在寻找的是grunt-html2js singleModule option的等价物,但对于 Gulp。我已经尝试singleModule: true为 ngHtml2Js 添加我的 gulp 任务选项。没用。

4

1 回答 1

0

我通过覆盖标准 ngHtml2Js 模板来做到这一点,然后使用 gulp-tap 修改文件。奇迹般有效!:-)

gulp.task('html', function () {
    // Compile templates
    return gulp.src(templateFiles)
        .pipe(htmlmin({
            removeComments: true,
            collapseWhitespace: true,
            conservativeCollapse: true,
            removeScriptTypeAttributes: true
        }))
        .pipe(ngHtml2Js({
            template: "    $templateCache.put('<%= template.url %>',\n        '<%= template.prettyEscapedContent %>');",
            prefix: 'tpl/'
        }))
        .pipe(concat(libName + '.tpls.js'))
        .pipe(tap(function(file, t) {
            file.contents = Buffer.concat([
                new Buffer("(function(module) {\n" +
                "try {\n" +
                "  module = angular.module('my.tpls');\n" +
                "} catch (e) {\n" +
                "  module = angular.module('my.tpls', []);\n" +
                "}\n" +
                "module.run(['$templateCache', function($templateCache) {\n"),
                file.contents,
                new Buffer("\n}]);\n" +
                "})();\n")
            ]);
        }))
        .pipe(gulp.dest(destDirectory));
});
于 2015-03-18T21:32:24.410 回答