grunt未定义默认未找到

时间:2015-01-30 17:17:21

标签: gruntjs grunt-build-control

我想为html模板项目创建yoeman生成器 当我尝试启动grunt时,我发现此错误任务'默认找不到 grunt build给grunt没有定义

$> grunt
Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: grunt is not defined
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.

这是我的代码

var fs = require('fs');
var path = require('path');
var showdown = require('showdown');
var EJS = require('ejs');
var TemplateRender = function(file, destination, source, template) {
this.file = file;
this.destination = destination;
this.source = source;
this.template = template;
this.grunt = grunt;
};

TemplateRender.prototype = {
render: function() {
var file    = this._read();
var html    = this._convert(file);
var content = this._template(html);
              this._write(content);
},
_read: function() {
var filepath = path.join(this.source,this.file);
grunt.file.read(filepath);
},
_convert: function(file) {
return new showdown.convertor().makeHtml(file);
},
_template: function(html) {
    var template = this.grunt.file.read(this.template);
    return EJS.render(template,{content:html});
 },
_write: function() {
this.grunt.file.write(
  path.join(this.destination, this.file),
  page
  );
  }

  };
'use strict';
module.exports = function(grunt) {

  grunt.registerTask('build', function() {
    var template = "app/index.ejs",
    destination = path.join(process.cwd(),"dist"),
    source = path.join(process.cwd(),"posts"),
    files = fs.readdirSync(source);

  files.forEach(function(file) {
    new TemplateRender(file, destination, source, template, grunt).render();
    read();
    convert();
    template();
    write();


  });

  });

  };

我需要知道如何检测grunt和yeoman中的错误

1 个答案:

答案 0 :(得分:0)

在代码的顶部,在TemplateRender函数中,您有这一行:this.grunt = grunt;但您实际上并没有该名称的参数。试试这个:

// ... (everything the same above here)

// *** Notice the new argument to this constructor function
var TemplateRender = function(file, destination, source, template, grunt) {
  this.file = file;
  this.destination = destination;
  this.source = source;
  this.template = template;
  this.grunt = grunt;
};

TemplateRender.prototype = {
  // ...
  _read: function() {
    var filepath = path.join(this.source,this.file);

    // *** probably needs to be `this.grunt` ?
    this.grunt.file.read(filepath);
  },

  // ...
};

module.exports = function(grunt) {
  grunt.registerTask('build', function() {
    // ... (mostly the same)

    files.forEach(function(file) {
      new TemplateRender(file, destination, source, template, grunt).render();

      // *** where are these defined? Should they be: this._read(), etc?
      this._read();
      this._convert();
      this._template();
      this._write();
    });

  });

};