Gulp and TS compilation

2019-03-04 08:26发布

问题:

I have defined task in my gulp file:

gulp.task('dev:build:scripts', function () {
    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject));
});

Which takes all of the scripts, and creates source maps to the static/dist. My tsProject:

var tsProject = ts.createProject('tsconfig.json', {
    outDir: paths.dist +'/app'
});

And my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "static/dist/app"
  },
  "exclude": [
    "node_modules"
  ]
}

However, after the gulp runs the project, it doesnt compile the JS and doesnt create sourcemaps - it only does so, when I make a change to any ts file. How can I fix this?

回答1:

I think you forgot to actually write your results. Try this one:

gulp.task('dev:build:scripts', () => 
{
    var tsProject = ts.createProject('tsconfig.json');

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(ts(tsProject));  

    return tsResult.js.pipe(sourcemaps.write()).pipe(gulp.dest(paths.dist +'/app'));        
});