javascriptnode.jsgulpglobminimatch

Include all .d.ts but exclude .ts on glob for gulp.src


Background

After I compile my Typescript files on my Angular2 project, I will have the following directory structure (simplified):

├── app
│   └── app.module.d.ts
│   └── app.module.js
│   └── app.module.ts
│   └── index.d.ts
│   └── index.js
│   └── index.ts
│   └── sample.component.d.ts
│   └── sample.component.js
│   └── sample.component.ts
│   └── sample.component.html
│   └── sample.component.css

Problem

Using gulp@3.9.1 and gulp.src(), how can I create a glob array which will include all the files under the app folder, but ignoring the .ts files corresponding to their .d.ts files?


Solution

  • Easiest way in your case, is to be explicit in what you want to include.

    Working example:

    var gulp = require('gulp');
    
    gulp.task('default', function() {
        gulp.src('app/**/*.*(js|html|css|d.ts)').pipe(gulp.dest('./out'));
    });
    

    Result:

    $ tree out
    out
    ├── app.module.d.ts
    ├── app.module.js
    ├── index.d.ts
    ├── index.js
    ├── sample.component.css
    ├── sample.component.d.ts
    ├── sample.component.html
    └── sample.component.js
    
    0 directories, 8 files
    

    See the documentation on gulp glob options here: https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options

    Which inherently uses node-gulp patterns, documented here: https://github.com/isaacs/node-glob