Jason Dunbar Jason Dunbar - 1 month ago 13
TypeScript Question

gulp-typescript is compiling all .ts files not just those from specific location

I've a gulp task that's responsible for converting .ts files in a given location and spitting out .js in another given location.

The issue I'm having is that it's not only taking .ts and converting to .js from the given location, before sending to the destination, but it's also doing the same for everything in the parent and sibling folders.

My folder structure is as follows:

root
|__dist
|
|__source
| |__bot
| |__logon


Here's a simplified version of my gulpfile.js:

var gulp = require("gulp"),
ts = require("gulp-typescript");

var paths = {
botScripts: 'source/bot/*.ts',
releaseBot: 'dist/bot'
};

// Create typescript project
var tsBotProject = ts.createProject('tsconfig.json');

// Create the tasks
gulp.task('botscripts', function() {

var tsResult = tsBotProject.src(paths.botScripts)
.pipe(tsBotProject());

return tsResult.js.pipe(gulp.dest(paths.releaseBot));
});




Here's my tsconfig.json:

{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
},
"exclude": [
"node_modules"
]
}




As I said, it does transpile to .js just fine, but what I end up with is all of the typscript files, regardless of location, transpiled to .js and copied into the same folder structure under dist:

root
|__dist
| |__bot
| | |__*.js
| |__logon
| |__*.js
|
|__source
| |__bot
| |__logon


What am I doing wrong here?

Answer

tsBotProject.src() doesn't take any arguments. You're trying to pass it paths.botScripts, but that argument is simply ignored. Instead tsBotProject.src() emits exactly those files that you specified in your tsconfig.json. In your case that is everything except node_modules.

You have two options:

Option 1: You can use tsBotProject.src() in your gulpfile.js like this:

var tsResult = tsBotProject.src()
  .pipe(tsBotProject());

In order for this to work you also have to add an include property to your tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  },
  "include": [
      "source/bot/*.ts"
  ],
  "exclude": [
      "node_modules"
  ]
}

Option 2: Use gulp.src() instead of tsBotProject.src() in your gulpfile.js:

var tsResult = gulp.src(paths.botScripts)
  .pipe(tsBotProject());
Comments