【问题标题】:Grunt compiling Jade filesGrunt 编译 Jade 文件
【发布时间】:2013-07-21 19:47:14
【问题描述】:

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

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

然后我希望grunt jade 输出:

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

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

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");
};

但是,当我运行翡翠命令时,我收到以下错误:

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

我做错了什么?

【问题讨论】:

  • 试试files: {"source/templates/out.html: ['source/templates/*.jade']}
  • 我想将它们编译成多个文件,而不是单个文件。
  • 哦,我明白了...通过查看文档,扩展名似乎是这样添加的ext: '.html',带有点。看不出问题出在哪里...您是否尝试过不使用cwd 只是为了使用完整路径进行测试?
  • 好的,我认为这有帮助。现在我收到错误消息:Warning: Unable to write "build/templates/" file (Error code: EISDIR). Use --force to continue.
  • 我看起来像是将 dest 视为一个文件。

标签: javascript node.js gruntjs


【解决方案1】:

完成以上答案

    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。是一模一样的,不过他们得把玉改名哈巴狗!

【讨论】:

  • files: 部分有点混乱。 Grunt 是否扩展了它,或者它们是 grunt 玉插件的选项?我问是因为我希望能够使用 /views/ 目录中的 layout.jade 文件作为包含,但我不想编译 layout.jade 文件。 -I 是否有一种巧妙的方法在 files 字段中包含单个文件。我想将它保存在 /views/ 中,因为它被 Express 渲染引擎使用。谢谢。
【解决方案2】:

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

我正在使用 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"
            } ]
        }
    }

【讨论】:

    【解决方案3】:

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

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

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

    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.
    

    test.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"
    }]
    

    运行 'grunt' 后,输出应该是:

    build/cats.html
    build/dogs.html
    

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2016-09-04
      • 2016-03-13
      • 2015-09-30
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      相关资源
      最近更新 更多