2014-10-14 3 views
2

На данный момент у меня есть только две задачи с глотком, ex gulp.task('handlebars-index'), gulp.task('handlebars-about'). Ниже приведен код из документов, https://www.npmjs.org/package/gulp-compile-handlebarsGulp-compile-handlebars для нескольких html-страниц?

Я не уверен, как я могу справиться с задачей для двух файлов .handlebars.

var gulp = require('gulp'); 
var handlebars = require('gulp-compile-handlebars'); 
var rename = require('gulp-rename'); 

gulp.task('handlebars', function() { 
    var templateData = { 
     firstName: 'Kaanon' 
    }, 
    options = { 
     ignorePartials: true, //ignores the unknown footer2 partial in the handlebars template, defaults to false 
     partials : { 
      footer : '<footer>the end</footer>' 
     }, 
     batch : ['./src/partials'], 
     helpers : { 
      capitals : function(str){ 
       return str.toUpperCase(); 
      } 
     } 
    } 

    // here how do I add an index.html and say and about.html? 
    return gulp.src('src/index.handlebars') 
     .pipe(handlebars(templateData, options)) 
     .pipe(rename('index.html')) 
     .pipe(gulp.dest('dist')); 
}); 

Вы можете увидеть выше задачи в основном берет index.handlebars затем компилирует его и создает файл index.html.

Если бы я добавил массив файлов руля, как эта задача будет знать, как создать .html-версию?

return gulp.src(['src/index.handlebars','src/about.handlebars']) 
     .pipe(handlebars(templateData, options)) 
     .pipe(rename('index.html')) 
     .pipe(rename('about.html')) 
     .pipe(gulp.dest('dist')); 

Вышеуказанное не будет работать.

ответ

7

Gulp-rename также выполняет функцию, в которой вы можете изменить только часть пути.

return gulp.src('src/*.handlebars') 
    .pipe(handlebars(templateData, options)) 
    .pipe(rename(function(path) { 
     path.extname = '.html'; 
    })) 
    .pipe(gulp.dest('dist')); 

https://github.com/hparra/gulp-rename#usage

+0

Хорошо, хорошо выглядит благодаря, верные удары, имеющие 3-4 глотком задачи БЖ. Я должен был больше обратить внимание на плагин Gulp-rename, я чистил документы gulp-compile-handlebars. Спасибо, человек, которого вы качаете! –

Смежные вопросы