Search code examples
gruntjstypescriptgrunt-contrib-watchgrunt-ts

Running grunt task only on changed file


I have a grunt ts task that transpiles typescript files to javascript. On gunt serve I'm transpiling all the .ts files I have. Then I'd like to configure grunt to watch the .ts files but transpile ONLY the .ts file that has been changed.

After searching the web I found the following option:

Grunt ts task:

ts: {
        all: {
            src: ['scripts/**/*.ts'],
            reference: 'scripts/_references.ts'
        }
    },

This is the grunt task, I'm then listening to the watch event:

grunt.event.on('watch', function (action, filepath, target) {
    switch (target) {
        case 'ts':
        {
            grunt.config(['ts.all.src'], filepath);
            grunt.config('watch.ts.tasks', 'ts');
            break;
        }
    }

The thing is that the task runs, but the files I'm updating to the ts.all.sec variable stays the same as on grunt serve and the task transpiles again all the files.

Thanks!


Solution

  • but transpile ONLY the .ts file that has been changed.

    grunt-ts can do this if you don't use a reference.ts (Because the typescript compiler would walk up the tree otherwise and compile all the referenced files irrespective) , and don't use --out (you are good here ... you are not using it) and use external modules instead. More : (https://github.com/grunt-ts/grunt-ts#fast-compile)