ngHtml2Js удаляет определения нескольких модулей из результата
У меня есть следующее задание глотка:
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, но для Gulp. Я уже пытался добавить singleModule: true
в моих вариантах задачи gulp для ngHtml2Js. Не сработало
1 ответ
Решение
Я сделал это, переопределив стандартный шаблон 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));
});