22

我正在尝试配置我的 Gruntfile 以将我的所有 Jade 文件编译为单独的 HTML 文件。例如,如果我有以下源文件夹:

source
└── templates
    ├── first.jade
    ├── second.jade
    └── third.jade

然后我希望grunt jade输出:

build
└── templates
    ├── first.html
    ├── second.html
    └── third.html

这是我的 Gruntfile 使用grunt-contrib-jade

module.exports = function(grunt) {
    grunt.initConfig({

        jade: {
            compile: {
                options: {
                    client: false,
                    pretty: true
                },
                files: [ {
                  src: "*.jade",
                  dest: "build/templates/",
                  ext: "html",
                  cwd: "source/templates/"
                } ]
            }
        },
    });

    grunt.loadNpmTasks("grunt-contrib-jade");
};

但是,当我运行 jam 命令时,出现以下错误:

Running "jade:compile" (jade) task
>> Source file "first.jade" not found.
>> Source file "second.jade" not found.
>> Source file "third.jade" not found.

我究竟做错了什么?

4

3 回答 3

50

完成以上答案

    jade: {
        compile: {
            options: {
                client: false,
                pretty: true
            },
            files: [ {
              cwd: "app/views",
              src: "**/*.jade",
              dest: "build/templates",
              expand: true,
              ext: ".html"
            } ]
        }
    }

因此,如果您的来源的结构如下:

app
└── views
    └── main.jade
    └── user
        └── signup.jade
        └── preferences.jade

grunt jade将创建以下结构

build
└── templates
    └── main.html
    └── user
        └── signup.html
        └── preferences.html

编辑:grunt-contrib-jade已弃用。你应该使用grunt-contrib-pug. 一模一样,但他们不得不将玉改名为哈巴狗!

于 2013-11-09T21:04:44.807 回答
3

以防万一有人需要。上面没有任何工作。这就是它最终对我有用的方式。

我正在使用grunt.loadNpmTasks('grunt-contrib-pug');我不知道 contrib-jade 是否已被弃用,但此解决方案对我有用。我需要第一个文件对象来处理 index.jade 和第二个来处理模板。现在,如果我不将其拆分并仅指向项目目录,那么翡翠编译器就会在我的 npm 包文件夹中丢失,因此它运行得更快。

pug: {
        compile: {
            options: {
                client: false,
                pretty: true,
                data: {
                    debug: false
                }
            },
            files: [
            {
                'dist/index.html': ['index.jade']
            },
            {
                src: "templates/*.jade",
                dest: "dist",
                expand: true,
                ext: ".html"
            } ]
        }
    }
于 2016-04-14T07:34:55.097 回答
1

我知道这是一篇旧帖子,但我在尝试解决类似问题时一直回到这里。我想使用 for 循环从单个玉模板文件输出多个 html 文件。因此需要更好地控制“文件”对象。

我遇到并最终解决的两个问题是设置输出文件名(javascript 对象文字 KEY)并确保立即运行内联 javascript 函数以便循环变量可用。

这是我带有注释的完整源代码。我希望这对其他偶然发现这篇文章的人有用。

Gruntfile.js:

module.exports = function(grunt) {

  // Create basic grunt config (e.g. watch files)
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
      grunt: { files: ['Gruntfile.js'] },
      jade: {
        files: 'src/*.jade',
        tasks: ['jade']
      }
    }
  });

  // Load json to populate jade templates and build loop
  var json = grunt.file.readJSON('test.json');

  for(var i = 0; i < json.length; i++) {
      var obj = json[i];

      // For each json item create a new jade task with a custom 'target' name.
      // Because a custom target is provided don't nest options/data/file parameters 
      // in another target like 'compile' as grunt wont't be able to find them 
      // Make sure that functions are called using immediate invocation or the variables will be lost
      // http://stackoverflow.com/questions/939386/immediate-function-invocation-syntax      
      grunt.config(['jade', obj.filename], {
        options: {
            // Pass data to the jade template
            data: (function(dest, src) {
                return {
                  myJadeName: obj.myname,
                  from: src,
                  to: dest
                };
            }()) // <-- n.b. using() for immediate invocation
        },
        // Add files using custom function
        files: (function() {
          var files = {};
          files['build/' + obj.filename + '.html'] = 'src/index.jade';
          return files;
        }()) // <-- n.b. using () for immediate invocation
      });
  }

  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Register all the jade tasks using top level 'jade' task
  // You can also run subtasks using the target name e.g. 'jade:cats'
  grunt.registerTask('default', ['jade', 'watch']);
};

src/index.jade:

doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) {
         bar(1 + 5)
      }
  body
    h1 #{myJadeName} - node template engine    
    #container.col
      p.
        Jade is a terse and simple
        templating language with a
        strong focus on performance
        and powerful features.

测试.json:

[{
    "id" : "1", 
    "filename"   : "cats",
    "tid" : "2016-01-01 23:35",
    "myname": "Cat Lady"
},
{
    "id" : "2", 
    "filename"   : "dogs",
    "tid" : "2016-01-01 23:45",
    "myname": "Dog Man"
}]

运行“咕噜”后,输出应为:

build/cats.html
build/dogs.html
于 2016-03-28T10:22:50.893 回答