JC Ford
JC Ford

Reputation: 7066

How can I in Gulp exclude javascript files compiled from Typescript?

We have an existing Angular 1 project and we want to gradually introduce Angular 2 components and eventually convert to Angular 2. The project was not written using Typescript or a module loader and converting the whole thing would be too much right now. But the Angular 2 components will be written in Typescript and will employ a module loader (SystemJS).

The legacy javascript code is bundled through an existing gulp script. I want to make minimal modifications to that gulp script so that it ignores javascript modules that were compiled from typescript. My idea is to try to exclude .js files that have a corresponding .ts file in the same folder with the same base name. I just can't figure out how to do that in gulp.

Upvotes: 0

Views: 130

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30564

The following uses fs.statSync to check if the corresponding .ts file exists and gulp-filter to exclude those files were that is the case:

var gulp = require('gulp');
var filter = require('gulp-filter');

var fs = require('fs');

gulp.task('default', function() {
  return gulp.src('src/**/*.js')
    .pipe(filter(function(file) {
      try {
        return !fs.statSync(file.path.replace(/\.js$/, '.ts')).isFile();
      } catch (e) {
        return true;
      }
    }))
    .pipe(gulp.dest('dist'));
});

Upvotes: 1

Related Questions