Thorsten Westheider
Thorsten Westheider

Reputation: 10932

Gulp task to only compile files that have changed

I need to write a gulp task that will only compile those Typescript files that have actually changed and came up with this:

var gulp = require('gulp');
var print = require('gulp-print');
var newer = require('gulp-newer');
var ts = require('gulp-typescript');

gulp.task('compile:ts', function () {
   return gulp.src([
         'typings/browser.d.ts',
         'app/**/*.ts'
      ])
      .pipe(newer('app'))
      .pipe(print(function (filepath) {
         return 'Compiling ' + filepath + '...';
      }))
      .pipe(ts({
         target: 'es5',
         module: 'commonjs',
         moduleResolution: 'node',
         sourceMap: true,
         emitDecoratorMetadata: true,
         experimentalDecorators: true,
         removeComments: false,
         noImplicitAny: false
      }))
      .pipe(gulp.dest('app'));
});

However, this task doesn't find any modified files although there are .ts files with more recent timestamps than their .js counterpart.

Where did I go wrong?

Upvotes: 0

Views: 733

Answers (2)

Sven Schoenung
Sven Schoenung

Reputation: 30574

However, this task doesn't find any modified files although there are .ts files with more recent timestamps than their .js counterpart.

That's because you're not telling gulp-newer to compare .ts files with .js files. You're comparing .ts files with themselves, so there is no change to be detected.

You need to tell gulp-newer to compare each .ts file with its .js counterpart:

.pipe(newer({dest:'app',ext:'.js'}))

Upvotes: 2

Kylar
Kylar

Reputation: 424

There is an easier method of compiling files when they change. Using gulp watch, you can run the function once and whenever you save a change, it'll run the compiling function.

gulp.task('watch', function() {
     gulp.watch(['ts/filepaths','more/ts/filepaths'],
     ['compile:ts','otherFunctionsIfNeeded']);
});

If you rewrite compile:ts to only compile, using the function above, whenever you save a ts file, it will compile it for you

Upvotes: 0

Related Questions