Grunt:查看多个文件,仅编译已更改 [英] Grunt: Watch multiple files, Compile only Changed

查看:32
本文介绍了Grunt:查看多个文件,仅编译已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Grunt 的新手,到目前为止我非常喜欢它.我希望 Grunt 在运行 grunt watch

时只编译更改的文件

在我的 Grunfile.coffee 中,我目前有(相关部分).
注意:assets/javascript/app.coffee 和 assets/javascript/app.js 是目录

 咖啡:默认:展开:真cwd:资产/javascript/app.coffee"src: ["*.coffee"]目标:资产/javascript/app.js"分机:.js"丑化:开发:选项:美化:真压缩:假破坏:假保留评论:'全部'文件:js/app.js":资产/javascript/app.js/*.js"js/libs.js":资产/javascript/libs/*.js"手表:咖啡脚本:文件:'assets/javascript/**/*.coffee'任务:[咖啡"]javascript:文件:资产/**/*.js"任务:["uglify:dev"]实时加载:文件:[Gruntfile.coffee"、js/*.js"、*.php"、css/*.css"、images/**/*.{png,jpg,jpeg,gif,webp,svg}", "js/*.js", ]选项:重载:真

可能有更短的方法,但我首先将 app.coffee 编译为 app.js,以便在我分发我的工作之后,不熟悉 Coffeescript 的人可以以某种合理的方式浏览代码.

所有这一切的问题在于,现在我保存了一个 Coffeescript 文件,我得到了太多的步骤(我认为):

>>文件assets/javascript/app.coffee/browse.coffee"已更改.运行coffee:default"(咖啡)任务文件 assets/javascript/app.js/browse.js 创建.文件 assets/javascript/app.js/filters.js 创建.完成,没有错误.2013 年 5 月 28 日星期二 12:30:18 GMT+0300 (EEST) 在 0.837 秒内完成 - 等待中...行>>文件assets/javascript/app.js/browse.js"已更改.>>文件assets/javascript/app.js/filters.js"已更改.运行uglify:dev"(uglify)任务创建文件js/app.js".创建文件js/libs.js".完成,没有错误.2013 年 5 月 28 日星期二 12:30:19 GMT+0300 (EEST) 在 0.831 秒内完成 - 等待中...行>>文件js/app.js"已更改.>>文件js/libs.js"已更改.2013 年 5 月 28 日星期二 12:30:19 GMT+0300 (EEST) 在 0.000 秒内完成 - 等待中...

目前我只是设置我的项目,但我会有更多的 Coffeescript 文件,而且我不希望 Coffeescript 在每次文件更改时重新编译所有文件.

此外,libs.js 根本不参与这一切,但我猜它仍然是编译的,因为它也匹配assets/*/.js"模式.p>

有没有办法让 Grunt 只编译已更改的文件?

解决方案

我终于找到了真正的解决方案!而且也超级简单!

npm install grunt-newer --save-dev

然后在你的 Gruntfile 中(在 grunt 中加载任务之后):

观看:咖啡脚本:文件:'assets/javascript/**/*.coffee'任务:[较新:咖啡"]

就是这样!真棒grunt-newer太棒了!

I'm new to Grunt, and so far I'm enjoying it very much. I want Grunt to compile only the changed files when running grunt watch

In my Grunfile.coffee I currently have (relevant parts).
Note: assets/javascript/app.coffee and assets/javascript/app.js are directories

    coffee:
        default:
            expand: true
            cwd: "assets/javascript/app.coffee"
            src: ["*.coffee"]
            dest: "assets/javascript/app.js"
            ext: ".js"

    uglify:
        dev:
            options:
                beautify: true
                compress: false
                mangle: false
                preserveComments: 'all'

            files: 
                "js/app.js": "assets/javascript/app.js/*.js"
                "js/libs.js": "assets/javascript/libs/*.js"

    watch:
        coffeescript:
            files: 'assets/javascript/**/*.coffee'
            tasks: ["coffee"]

        javascript:
            files: "assets/**/*.js"
            tasks: ["uglify:dev"]
        livereload:
            files: ["Gruntfile.coffee", "js/*.js", "*.php", "css/*.css", "images/**/*.{png,jpg,jpeg,gif,webp,svg}", "js/*.js", ]
            options:
                livereload: true

There is probably a shorter way around, but I'm compiling app.coffee to app.js first, so that after I distribute my work, people who aren't comfortable with Coffeescript can browse the code in somewhat reasonable manner.

The problem with all this is that now that I save a Coffeescript file, I get too many steps ( I think ):

>> File "assets/javascript/app.coffee/browse.coffee" changed.

Running "coffee:default" (coffee) task
File assets/javascript/app.js/browse.js created.
File assets/javascript/app.js/filters.js created.

Done, without errors.
Completed in 0.837s at Tue May 28 2013 12:30:18 GMT+0300 (EEST) - Waiting...
OK
>> File "assets/javascript/app.js/browse.js" changed.
>> File "assets/javascript/app.js/filters.js" changed.

Running "uglify:dev" (uglify) task
File "js/app.js" created.
File "js/libs.js" created.

Done, without errors.
Completed in 0.831s at Tue May 28 2013 12:30:19 GMT+0300 (EEST) - Waiting...
OK
>> File "js/app.js" changed.
>> File "js/libs.js" changed.

Completed in 0.000s at Tue May 28 2013 12:30:19 GMT+0300 (EEST) - Waiting...

Currently I'm just setting up my project, but I will have a lot more Coffeescript files, and I don't want Coffeescript to recompile all of the files, on each file change.

Furthermore, libs.js has no part in all of this at all, but I guess it is still compiled, because it also matches the "assets/*/.js" pattern.

Is there a way to make Grunt compile only the files that have changed ?

解决方案

I've finally found a real solution! And it's super simple too!

npm install grunt-newer --save-dev

Then in your Gruntfile (after loading the task in grunt):

watch:
    coffeescript:
        files: 'assets/javascript/**/*.coffee'
        tasks: ["newer:coffee"]

And that's it! The Awesome grunt-newer is awesome!

这篇关于Grunt:查看多个文件,仅编译已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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