Gulp TypeScript Plugin
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
-
TypeError: project.src is not a function
cause Attempting to use `ts.createProject()` without invoking it as a function or incorrectly accessing its properties.fixEnsure `ts.createProject()` is called to create the project instance, e.g., `const tsProject = ts.createProject('tsconfig.json');`, and then use `tsProject()` as the gulp plugin, e.g., `gulp.src('src/**/*.ts').pipe(tsProject()).js.pipe(...)`. -
Error: Not a vinyl file. Make sure that you're not using .js or .d.ts files in your gulp.src statement when using `allowJs` or `declaration`
cause `gulp-typescript` typically expects `.ts` or `.tsx` files as input unless `allowJs` is enabled. This error can occur if you pass already compiled JS/DTS files to the TypeScript stream without proper configuration.fixVerify that your `gulp.src()` glob pattern only selects TypeScript source files (`.ts`, `.tsx`). If you intend to compile JavaScript files, ensure `allowJs: true` is set in your `tsconfig.json` or `createProject` options. -
TS####: [TypeScript Compilation Error Message]
cause A TypeScript compilation error occurred, and `gulp-typescript` (since v5) is configured to crash the process on error in non-watch mode.fixCorrect the TypeScript code identified by the error message. For development and watch mode, consider using `gulp-plumber` before the `tsProject()` pipe to prevent the stream from stopping on errors, allowing continuous watch and reporting. -
Error: Input file must be a TS file!
cause Attempting to pass non-TypeScript files (e.g., JavaScript files) to the `gulp-typescript` stream without `allowJs` enabled in the compiler options.fixEither ensure `gulp.src` only selects `.ts` or `.tsx` files, or enable the `allowJs` option in your `tsconfig.json` or `ts.createProject` configuration if you intend to process JavaScript files.
Warnings
- breaking Starting with v4.0.0 and further solidified in v5.0.0, `gulp-typescript` requires Gulp 4. Older versions of Gulp are not supported. Additionally, `gulp-util` dependency was removed in v4.0.0.
- breaking In v5.0.0, the behavior of compiler errors changed. Outside of watch mode, the Gulp process will now exit when a TypeScript compiler error occurs, aligning with Gulp's new error handling. In watch mode, the process continues.
- gotcha `gulp-typescript` does not handle sourcemap options directly. Options like `sourceMap`, `inlineSourceMap`, `inlineSources` are unsupported. Instead, `gulp-sourcemaps` should be used.
- gotcha The `watch` compiler option is not supported by `gulp-typescript`. Incremental compilation is managed internally by `ts.createProject()`, and file watching should be handled by `gulp.watch()`.
- breaking v6.0.0-alpha.1 is a pre-release version. It may introduce further breaking changes not yet fully documented or stable. Use with caution in production environments.
- breaking When compiling with the `declaration: true` option, `gulp-typescript` outputs two streams: one for JavaScript (`.js`) and one for declaration files (`.dts`). Both must be piped to `gulp.dest()` separately.
Install
-
npm install gulp-typescript -
yarn add gulp-typescript -
pnpm add gulp-typescript
Imports
- ts
import ts from 'gulp-typescript';
const ts = require('gulp-typescript'); - createProject
const tsProject = require('gulp-typescript').createProject('tsconfig.json');const tsProject = ts.createProject('tsconfig.json'); - reporter
ts.reporter.full()
Quickstart
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;