Grunt watch:只编译一个文件而不是全部 [英] Grunt watch: compile only one file not all

查看:151
本文介绍了Grunt watch:只编译一个文件而不是全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有grunt设置编译所有的我的咖啡文件到javascript和维护所有的文件夹结构使用dynamic_mappings,工作伟大。

 咖啡:{
dynamic_mappings:{
files:[{
expand:true,
cwd:'assets / scripts / src /',
src:'** / *。coffee',
dest:'assets / scripts / dest /',
:'.js'
}]
}
}



<我想做的是,然后使用手表来编译任何更改的咖啡文件,仍然保持文件夹结构。此工作使用上述任务与此观察任务:

 手表:{
coffeescript:{
文件:'assets / scripts / src / ** / *。coffee',
tasks:['coffee:dynamic_mappings']
}
}



问题是,当一个文件更改时,它会将整个咖啡目录重新编译为Javascript,如果只编译单个咖啡文件那就变成了Javascript。这是自然可能在Grunt或者这是一个自定义功能。这里的关键是它必须保持文件夹结构,否则很容易。



我们有自定义的手表脚本在工作,我想卖他们在Grunt,但会需要这个功能来做。

解决方案

您可以使用类似以下Gruntfile。每当CoffeeScript文件更改时,它会更新 coffee:dynamic_mappings 的配置,以仅将修改的文件用作 src



此示例是

希望它有帮助!

  var path = require(path); 
var srcDir ='assets / scripts / src /';
var destDir ='assets / scripts / dest /';

module.exports = function(grunt){
grunt.initConfig({
coffee:{
dynamic_mappings:{
files:[{
expand:true,
cwd:srcDir,
src:'** / *。coffee',
dest:destDir,
ext:'.js'
}]
}
},
手表:{
coffeescript:{
files:'assets / scripts / src / ** / *。coffee',
tasks:coffee:dynamic_mappings,
options:{
spawn:false,//重要,以便任务在同一上下文中运行
}
}
}
});

grunt.event.on('watch',function(action,filepath,target){
var coffeeConfig = grunt.config(coffee);
// Update file.src是修改的文件的路径(相对于srcDir)
coffeeConfig.dynamic_mappings.files [0] .src = path.relative(srcDir,filepath);
grunt.config coffee,coffeeConfig);
});

grunt.loadNpmTasks(grunt-contrib-coffee);
grunt.loadNpmTasks(grunt-contrib-watch);

grunt.registerTask(default,[coffee:dynamic_mappings,watch:coffeescript]);
};


I have grunt setup to compile all of my coffee files into javascript and maintain all folder structures using dynamic_mappings which works great.

coffee: {
  dynamic_mappings: {
    files: [{
      expand: true,
      cwd: 'assets/scripts/src/',
      src: '**/*.coffee',
      dest: 'assets/scripts/dest/',
      ext: '.js'
    }]
  }
}

What I would like to do is then use watch to compile any changed coffee file and still maintain folder structure. This works using the above task with this watch task:

watch: {
  coffeescript: {
    files: 'assets/scripts/src/**/*.coffee',
    tasks: ['coffee:dynamic_mappings']
  }
}

The problem is that when one file changes it compiles the entire directory of coffee into Javascript again, it would be great if it would only compile the single coffee file that was changed into Javascript. Is this naturally possible in Grunt or is this a custom feature. The key here is it must maintain the folder structure otherwise it would be easy.

We have custom watch scripts at work and I'm trying to sell them on Grunt but will need this feature to do it.

解决方案

You can use something like the following Gruntfile. Whenever a CoffeeScript file changes, it updates the configuration for coffee:dynamic_mappings to only use the modified file as the src.

This example is a slightly modified version of the example in the grunt-contrib-watch readme.

Hope it helps!

var path = require("path");
var srcDir = 'assets/scripts/src/';
var destDir = 'assets/scripts/dest/';

module.exports = function( grunt ) {
    grunt.initConfig( {
        coffee: {
            dynamic_mappings: {
                files: [{
                    expand: true,
                    cwd: srcDir,
                    src: '**/*.coffee',
                    dest: destDir,
                    ext: '.js'
                }]
            }
        },
        watch : {
            coffeescript : {
                files: 'assets/scripts/src/**/*.coffee',
                tasks: "coffee:dynamic_mappings",
                options: {
                    spawn: false, //important so that the task runs in the same context
                }
            }
        }
    } );

    grunt.event.on('watch', function(action, filepath, target) {
        var coffeeConfig = grunt.config( "coffee" );
        // Update the files.src to be the path to the modified file (relative to srcDir).
        coffeeConfig.dynamic_mappings.files[0].src = path.relative(srcDir, filepath);
        grunt.config("coffee", coffeeConfig);
    } );

    grunt.loadNpmTasks("grunt-contrib-coffee");
    grunt.loadNpmTasks("grunt-contrib-watch");

    grunt.registerTask("default", [ "coffee:dynamic_mappings", "watch:coffeescript"]);
};

这篇关于Grunt watch:只编译一个文件而不是全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆