简体   繁体   English

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

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

Using a version of what grunt-contrib-watch recommends for compiling only changed files in here : https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed 使用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();
});

This works fine (again with a variation I wrote for it here: https://gist.github.com/pgilad/6897875 ) 效果很好(同样,我在这里为它写了一个变体: https : //gist.github.com/pgilad/6897875

The problem is when using include inside Jade templates, meaning you are including other Jade templates in order to build the complete html file. 问题是在Jade模板中使用include时,这意味着要包括其他Jade模板以构建完整的html文件。

Using the singular solution for compile doesn't work because if a .jade file you are working on is embeded using include current_working_jade.jade - the including file won't get recompiled. 使用单数解决方案无法编译,因为如果正在使用include current_working_jade.jade嵌入了您正在处理的.jade文件,则include current_working_jade.jade 包含文件将不会重新编译。

Are there any workarounds for this besides compiling all of your jade files from scratch? 除了从头开始编译所有jade文件之外,还有其他解决方法吗? This causes a problem when you have around ~60 large jade files to compile every time. 每次要编译约60个大型玉文件时,这会引起问题。

The only possible solution I can think of is either mapping jade templates dependencies either externally or with directories, but I don't know any tools/plugins which do that... 我能想到的唯一可能的解决方案是在外部或通过目录映射Jade模板依赖关系,但是我不知道有哪些工具/插件可以这样做。

After already starting to work on a scaffold that will generate a sortof jade sourcemap I found this great project, that already solves this issue: 在已经开始研究会生成某种玉sourcemap的脚手架之后,我发现了这个很棒的项目,已经解决了这个问题:

Jade Inheritance 翡翠传承

Usage is as follows: 用法如下:

  1. Install package using: npm install jade-inheritance --save-dev 使用以下命令安装软件包: npm install jade-inheritance --save-dev
  2. Where you want to get a list of dependent files from a jade: 您想要从玉器获取相关文件列表的位置:

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

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

  3. Then when you want to get the file: 然后,当您要获取文件时:

    depenedentFiles = inheritance.files;

  4. The project also demonstrates how to apply the concept with grunt.watch in order to compile only changed jade files with their dependents, exactly what I needed: 该项目还演示了如何使用grunt.watch来应用该概念,以便仅与他们的依赖者一起编译更改的jade文件,这正是我所需要的:

Using jade-inheritance with grunt watch 与玉石继承人一起使用手表

I imagine something like checking all jade files and if they include your changed file then recompile that as well. 我想像检查所有玉器文件,如果它们包括您更改的文件,然后重新编译它。 Shouldn't be too hard. 不应该太难。 Pseudo code: 伪代码:

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

Now add these files to the compile task. 现在将这些文件添加到编译任务。

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

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