javascript - 使用 grunt 观察并重新编译单个模板

标签 javascript node.js gruntjs

我有一个包含一堆 jade templates 的目录, 和一个 grunt将所有这些编译成单独的 html 文件的任务。

我想要一个 watch task它会在模板更改时重新编译模板,但现在我的任务是在每个模板发生更改时重新编译每个模板。

Here is a demo要点。

是否有一种简洁的方法来编写一个任务,在模板更改时重新编译模板,而不是所有其他模板?

最佳答案

解决方案是添加一个filter文件列表的函数:

var fs = require('fs');
var join = require('path').join;
module.exports = function(grunt) {
  grunt.initConfig({
    jade: {
      files: {
        src: ['*.jade'],
        dest: './',
        expand: true,
        ext: '.html',
        filter: function(destDir, src) {
          var dest = join(destDir, src.replace(/jade$/, 'html'));
          var destMod;
          try {
              destMod = +fs.lstatSync(dest).mtime;
          } catch (e) {
              if (e.code === 'ENOENT') {
                  // there's no html file, so ensure that the
                  // jade file is compiled by returning true
                  return true;
              }
              throw e;
          }
          return fs.lstatSync(src).mtime > destMod;
        }
      }
    },
    watch: {
      files: ['*.jade'],
      tasks: ['jade']
    }
  });

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

关于javascript - 使用 grunt 观察并重新编译单个模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18195522/

相关文章:

javascript - 我如何使用事件 @select 从 Vue.js 中的段落中选择一些文本?

javascript - 是否应该使用 HTML 注释来封装 JavaScript 代码块?

javascript - 通过 JavaScript 从 CRM 2011 检索可排序电子邮件列表

javascript - 针对数据 :uri in IE8? 的 32KB 限制的任何解决方法

c# - 将 javascript 中的数字转换为 4 字节数组

javascript - dist 中的 GruntJS 配置样式和脚本路径

node.js - Docker Build 在 GCP Cloud Build 中失败 - 但在 localhost 中成功

javascript - 当与其他 View 不在同一文件夹中时,Swig 标签在索引文件中不起作用

css - Grunt 在 usemin block 中包含 bower_components

javascript - grunt 找不到 compass 命令。为什么不?