Gulp TypeScript Plugin

6.0.0-alpha.1 · active · verified Tue Apr 21

gulp-typescript is a Gulp plugin designed for integrating TypeScript compilation into Gulp build workflows, offering features like incremental compilation and project reference support. The package is currently in an active development phase, with v5.0.1 being the latest stable release and v6.0.0-alpha.1 actively being developed. New releases, including alphas and minor fixes, are frequent. It serves as a key tool for developers who leverage Gulp for task automation and need to compile TypeScript, providing a direct interface to TypeScript's compiler options and supporting `tsconfig.json` configurations. Its main differentiators include tight integration with Gulp's streaming API, efficient incremental compilation, and support for advanced TypeScript features like custom transformers introduced in v5.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to compile TypeScript files using `gulp-typescript` with sourcemaps and `tsconfig.json` support, outputting both JavaScript and declaration files. It also includes a basic watch task for development.

import gulp from 'gulp';
import ts from 'gulp-typescript';
import sourcemaps from 'gulp-sourcemaps';

// Create a TypeScript project instance, optionally reading a tsconfig.json
const tsProject = ts.createProject('tsconfig.json', {
  // Override options from tsconfig.json or define new ones
  // For example, force ES2017 target if tsconfig is older
  target: 'ES2017',
  declaration: true // Generate .d.ts files
});

function compileTypeScript() {
  return gulp.src('src/**/*.ts') // Source TypeScript files
    .pipe(sourcemaps.init()) // Initialize sourcemaps
    .pipe(tsProject()) // Compile TypeScript
    .js // Get JavaScript stream
    .pipe(sourcemaps.write('.')) // Write sourcemaps to same directory
    .pipe(gulp.dest('dist')) // Output JavaScript files
    .dts // Get Declaration stream
    .pipe(gulp.dest('dist')); // Output Declaration files
}

// Define a watch task for continuous compilation during development
function watchTypeScript() {
  gulp.watch('src/**/*.ts', compileTypeScript);
}

export const build = gulp.series(compileTypeScript);
export const watch = gulp.series(compileTypeScript, watchTypeScript);
export default build;

view raw JSON →