【问题标题】:Grunt: Watch file changes and compile parent directoryGrunt:观察文件变化并编译父目录
【发布时间】:2015-01-19 22:32:03
【问题描述】:

我正在使用 grunt 开发一个项目,我之前没有使用过 grunt,目前这是用于监视文件的设置,当文件更改时重新编译所有文件(包含数百个文件的多个子目录)使用将车把转换为 html,这非常慢。我想通过只编译需要的东西来改进它以加快进程。

用 grunt newer 观察文件并没有真正起作用,因为目录中存在依赖关系,因此仅重新编译更改的文件不会产生有效的页面。

我基本上需要重新编译已更改文件的整个父目录,但我不太确定如何配置类似的东西。

我应该在哪里查看任何提示?

组装本身是这样配置的:

 var _ = require('lodash');
 var path = require('path');
 // expand the data files and loop over each filepath
 var pages = _.flatten(_.map(grunt.file.expand('./src/**/*.json'), function(filepath) {
    // read in the data file
    var data = grunt.file.readJSON(filepath);
    var dest=path.dirname(filepath)+ '/' +path.basename(filepath, path.extname(filepath));
    dest=dest.replace("src/","");
    var hbs;
    if (data.hbs){
        hbs=grunt.file.read(path.dirname(filepath)+ '/' + data.hbs)
    }

    // create a 'page' object to add to the 'pages' collection
    return {
    // the filename will determine how the page is named later
    filename: dest,
    // the data from the json file
    data: data,
    // add the recipe template as the page content
    content:hbs 
    };
 }));

return {
    options: {
        /*postprocess: require('pretty'),*/
        marked: {sanitize: false},
        data: '<%= options.src %>/**/*.json',
        helpers: '<%= options.src %>/helpers/helper-*.js',
        layoutdir: '<%= options.src %>/templates',
        partials: ['<%= options.src %>/components/**/*.hbs']
    },
    build: {
        options: {
            layout: 'base.hbs',
            assets: '<%= options.build %>',
            pages: pages
        },
        files: [
            {
                cwd: '<%= options.src %>', 
                dest: '<%= options.build %>',
                src: '!*'
            }
        ]
    },
}

因此,每次加载时,所有页面都会像 /src/sites/abc/xyz/foo.json 一样被扫描并编译,但我只想更改文件。 Watch 确实检测到更改的文件,但所有文件都会再次编译,我不确定如何让 watch 在配置中识别出的更改文件只处理部分文件。

【问题讨论】:

  • 你能告诉我们与问题相关的配置吗?
  • 当然,我已经添加了我理解为组装过程的基本代码。

标签: node.js gruntjs


【解决方案1】:

我认为你需要的东西已经在手表里了。

检查 grunt 文档中的Using the watch event

把这里的内容抄下来满足SO MODS/GODS

当监视的文件被修改时,此任务将发出监视事件。如果您希望在编辑文件时收到简单的通知,或者如果您将此任务与另一个任务结合使用,这将非常有用。下面是一个使用 watch 事件的简单示例:

grunt.initConfig({
  watch: {
    scripts: {
      files: ['lib/*.js'],
    },
  },
});
grunt.event.on('watch', function(action, filepath, target) {
  grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
});

watch 事件并非旨在替换用于配置和运行任务的标准 Grunt API。如果您尝试从监视事件中运行任务,那么您很可能做错了。请阅读配置任务。

根据需要编译文件

一个非常常见的要求是只根据需要编译文件。这是一个仅使用 jshint 任务对更改的文件进行 lint 的示例:

grunt.initConfig({
  watch: {
    scripts: {
      files: ['lib/*.js'],
      tasks: ['jshint'],
      options: {
        spawn: false,
      },
    },
  },
  jshint: {
    all: {
      src: ['lib/*.js'],
    },
  },
});

// on watch events configure jshint:all to only run on changed file
grunt.event.on('watch', function(action, filepath) {
  grunt.config('jshint.all.src', filepath);
});

如果您需要动态修改配置,则必须禁用 spawn 选项以保持手表在相同的上下文中运行。

如果您同时保存多个文件,您可能会选择更可靠的方法:

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

【讨论】:

  • 谢谢,使用事件挂钩我可以扫描目录文件,然后将它们放入组装过程中。 spawn: false 对于此解决方案非常重要 - 我不想包含使组装过程在选项恢复为默认值的新线程中产生的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 2019-11-27
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多