简体   繁体   中英

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.

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'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM