【问题标题】:Passing gulp-front-matter value to gulp-wrap将 gulp-front-matter 值传递给 gulp-wrap
【发布时间】:2015-06-29 13:06:53
【问题描述】:

我希望做这样的事情:

gulp.src('src/*.html')
  .pipe(fm ({
    property: 'fm'
  }))
  .pipe(wrap({
    src: fm['template']
  }))

其中template 是front-matter 中的一个值。我可以创建一个单独的函数,在其中使用 gulp-tap 传递值,但我希望保留它 “简单的”。我不认为gulp-wrap 支持访问前端变量。

【问题讨论】:

    标签: gulp


    【解决方案1】:

    这似乎是一种解决方案,但随着我添加更多复杂性(例如使用 gulp-swig),它开始失败。

    源文件包含:

    ---
    template: full
    ---
    
    Text content is here.
    

    和脚本:

    var frontMatter   = require('gulp-front-matter'),
        gulp          = require('gulp'),
        tap           = require('gulp-tap'),
        wrap          = require('gulp-wrap')
    
    gulp.task('default', function() {
      gulp.src('src/*.html')
        .pipe(frontMatter())
        .pipe(tap(function(file) {
          gulp.src(file.path)
            .pipe(frontMatter ({
              remove: true
            }))
            .pipe(wrap({
              src: file.frontMatter['template'] + '.tpl' // ==> full.tpl
            }))
            .pipe(gulp.dest('build/'))
        }))
    });
    

    【讨论】:

      【解决方案2】:

      你需要传递一个函数作为gulp-wrap的第一个参数。

      gulp-wrap的文档说

      当一个函数时,该函数将被调用并且应该返回 模板内容。该函数首先获取数据对象 参数。

      类似下面的东西应该可以工作。

      var gulp = require('gulp')
      var fm = require('gulp-front-matter')
      var wrap = require('gulp-wrap')
      
      var fs = require('fs')
      
      gulp.src('src/*.html')
        .pipe(fm({property: 'fm'}))
        .pipe(wrap(function (data) {
          return fs.readFileSync('path/to/layout_dir/' + data.file.fm.template + '.tpl').toString()
        }))
        .pipe(gulp.dest('build/'))
      

      另请参阅: Gulp Front Matter +Markdown through Nunjucks

      【讨论】:

        猜你喜欢
        • 2015-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多