【问题标题】:Load an external file in a grunt-contrib-jade task在 grunt-contrib-jade 任务中加载外部文件
【发布时间】:2014-04-19 15:12:56
【问题描述】:

所以我已经有了这个运行良好的 grunt-contrib-jade 设置

当我自己将数据包含在(导出的)Gruntfile 中时,一切都很好:

module.exports = {
   website: {
     options: {
       data: {
         pageTitle: "This is my website",
         greeting: "Hello world",
       },
       debug: true,
       pretty: true
    },
    files: {
      'build/website/index.html': 'src/jade/template/index.jade'
    }
  }
};

它将我的 index.jade 与我的数据值合并,我的 index.html 输出是它应该的方式。但是当我想加载一个外部文件来定义我的数据时,它出错了:

options: {
  data: function (dest, src) {
    // Return an object of data to pass to templates
    return require('src/jade/template/locals.json');
  },
  debug: true,
  pretty: true
},
files: {
  'build/website/index.html': 'src/jade/template/index.jade'
}

需要的路径是有效的,我检查了三次。它与我的 index.jade 位于同一文件夹中。但是我仍然不断收到此错误:

Running "jade:website" (jade) task
>> Jade failed to compile "src/jade/template/index.jade".
>> Error: Cannot find module './locals.json'
>> Destination not written because compiled files were empty.

我几乎尝试了所有方法,但我没有看到。

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    当您require 时,本地模块需要在前面加上“./”。

      data: function (dest, src) {
        // Return an object of data to pass to templates
        return require('./src/jade/template/locals.json');
      }
    

    会起作用。你没有对这个函数做任何事情(至少现在),所以这也可能是

    data: require('./src/jade/template/locals.json')

    甚至

    data: grunt.file.readJSON('./src/jade/template/locals.json').

    【讨论】:

    • 我以前尝试过所有这些东西,很好的答案,因为我认为它会像这样工作。但我仍然不断收到以下错误:➜ 模板 git:(master) ✗ grunt watch Loading "Gruntfile.js" tasks...ERROR >> Error: Cannot find module './src/jade/template/locals.json'警告:找不到任务“监视”。使用 --force 继续。由于警告而中止。
    • 使用--verbose 标志运行 grunt 时的输出是什么?你的watch 任务是什么样的?
    • 太好了,你的问题让我走上了正确的“道路”(如果你原谅双关语的话)。我使用基于 github.com/chriscoyier/My-Grunt-Boilerplate 的 grunt 模块化设置。事实证明,路径是相对于文件本身的,但不知何故,我的大脑仍然认为路径是相对于加载 task.js 文件的 Gruntfile 的。我所要做的就是弄清楚现在是否可以使用 ../../ 之类的东西。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 2014-04-16
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多