繁体   English   中英

如何使用模板包括条件地编译(使用Grunt)仅更改的玉文件

[英]How to conditionally compile (using Grunt) only changed jade files with template includes

使用grunt-contrib-watch建议的版本仅在此处编译更改的文件: https : //github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed

var changedFiles = Object.create(null);

var onChange = grunt.util._.debounce(function() {
grunt.config('jshint.all.src', Object.keys(changedFiles));
   changedFiles = Object.create(null);
}, 200);

grunt.event.on('watch', function(action, filepath) {
   changedFiles[filepath] = action;
   onChange();
});

效果很好(同样,我在这里为它写了一个变体: https : //gist.github.com/pgilad/6897875

问题是在Jade模板中使用include时,这意味着要包括其他Jade模板以构建完整的html文件。

使用单数解决方案无法编译,因为如果正在使用include current_working_jade.jade嵌入了您正在处理的.jade文件,则include current_working_jade.jade 包含文件将不会重新编译。

除了从头开始编译所有jade文件之外,还有其他解决方法吗? 每次要编译约60个大型玉文件时,这会引起问题。

我能想到的唯一可能的解决方案是在外部或通过目录映射Jade模板依赖关系,但是我不知道有哪些工具/插件可以这样做。

在已经开始研究会生成某种玉sourcemap的脚手架之后,我发现了这个很棒的项目,已经解决了这个问题:

翡翠传承

用法如下:

  1. 使用以下命令安装软件包: npm install jade-inheritance --save-dev
  2. 您想要从玉器获取相关文件列表的位置:

    var JadeInheritance = require('jade-inheritance');

    var inheritance = new JadeInheritance(file, basedirname, {basedir:basedirname});

  3. 然后,当您要获取文件时:

    depenedentFiles = inheritance.files;

  4. 该项目还演示了如何使用grunt.watch来应用该概念,以便仅与他们的依赖者一起编译更改的jade文件,这正是我所需要的:

与玉石继承人一起使用手表

我想像检查所有玉器文件,如果它们包括您更改的文件,然后重新编译它。 不应该太难。 伪代码:

var allFiles = getAllJadeFileWithIncludesAndProjectPathMap();
//allFiles now contains something like this

{
  'jade/index.jade': ['jade/menu.jade', 'jade/home.jade'],
  'jade/about.jade': ['jade/menu.jade']
}


var rootFiles = [];

_.each(allFiles, function (includes, parent) {
  _.each(includes, function (includePath) {
    var parent;
    while (parent = getParentPath(includePath)) {
      //nothing needed if getParentPath returns null for files that aren't included
    }

    if (rootFiles.indexOf(parent) !== -1) {
      rootFiles.push(parent);
    }
  });
});

现在将这些文件添加到编译任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM