Gathering detailed insights and metrics for gulp-typescript
Gathering detailed insights and metrics for gulp-typescript
Gathering detailed insights and metrics for gulp-typescript
Gathering detailed insights and metrics for gulp-typescript
A TypeScript compiler for gulp with incremental compilation support.
npm install gulp-typescript
v5.0.1 - Resolve @types in node_modules above current directory
Published on 13 Mar 2019
v5.0.0 - Custom transforms & various fixes
Published on 02 Dec 2018
5.0.0-alpha.3 - Fix fullReporter
Published on 03 Jul 2018
5.0.0-alpha.2 - Project references
Published on 18 Jun 2018
v5.0.0-alpha.1 - Gulp 4 & TS 2.9
Published on 11 Jun 2018
v4.0.1 - Fix missing dependency
Published on 08 Feb 2018
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
838 Stars
883 Commits
130 Forks
13 Watching
22 Branches
55 Contributors
Updated on 11 Nov 2024
JavaScript (56.67%)
TypeScript (43.33%)
Cumulative downloads
Total Downloads
Last day
-1.7%
43,655
Compared to previous day
Last week
7.8%
283,409
Compared to previous week
Last month
45.5%
951,670
Compared to previous month
Last year
4.8%
7,582,581
Compared to previous year
A gulp plugin for handling TypeScript compilation workflow. The plugin exposes TypeScript's compiler options to gulp using TypeScript API.
This plugin works best with gulp 4. If you cannot update to this version, please see the section "Gulp 3" below.
Updating from version 2? See the breaking changes in version 3.
1npm install --global gulp-cli
1npm install gulp@4
1npm install gulp-typescript typescript
Almost all options from TypeScript are supported.
outFile
(string) - Generate one javascript and one definition file. Only works when no module system is used.outDir
(string) - Move output to a different (virtual) directory. Note that you still need gulp.dest
to write output to disk.noImplicitAny
(boolean) - Warn on expressions and declarations with an implied 'any' type.suppressImplicitAnyIndexErrors
(boolean) - Suppress --noImplicitAny
errors for indexing objects lacking index signatures.noLib
(boolean) - Don't include the default lib (with definitions for - Array, Date etc)lib
(string[]) - List of library files to be included in the compilation.target
(string) - Specify ECMAScript target version: 'ES3' (default), 'ES5' or 'ES6'.module
(string) - Specify module code generation: 'commonjs', 'amd', 'umd' or 'system'.jsx
(string) - Specify jsx code generation: 'react' or 'preserve' (TS1.6+).declaration
(boolean) - Generates corresponding .d.ts files. You need to pipe the dts
streams to save these files.removeComments
(boolean) - Do not emit comments to output.emitDecoratorMetadata
(boolean) - Emit design-time metadate for decorated declarations in source.experimentalAsyncFunctions
(boolean) - Support for ES7-proposed asynchronous functions using the async
/await
keywords (TS1.6+).experimentalDecorators
(boolean) - Enables experimental support for ES7 decorators.moduleResolution
(string) - Determine how modules get resolved. Either 'node' for Node.js/io.js style resolution, or 'classic' (default) (TS1.6+).noEmitOnError
(boolean) - Do not emit outputs if any type checking errors were reported.noEmitHelpers
(boolean) - Do not generate custom helper functions like __extends in compiled output.preserveConstEnums
(boolean) - Do not erase const enum declarations in generated code.isolatedModules
(boolean) - Compiles files seperately and doesn't check types, which causes a big speed increase. You have to use gulp-plumber and TypeScript 1.5+.allowJs
(boolean) - Allow JavaScript files to be compiled.rootDir
- Specifies the root directory of input files. Only use to control the output directory structure with outDir
.See the TypeScript wiki for a complete list. These options are not supported:
sourceMap
, inlineSourceMap
, inlineSources
, sourceRoot
, declarationMap
) - Use gulp-sourcemaps instead.watch
- Use gulp.watch
instead. See the paragraph "Incremental compilation".project
- See "Using tsconfig.json
".help
, version
gulp-typescript can be imported using const ts = require('gulp-typescript');
. It provides the following functions:
ts(options?)
- Returns a gulp stream that compiles TypeScript files using the specified options.ts.createProject(options?)
, ts.createProject(tsconfig filename, options?)
- Returns a project. The intended usage is to create a project outside of a task with const tsProject = ts.createProject(..);
. Within a task, tsProject()
can be used to compile a stream of TypeScript files.tsProject.src()
- Returns a stream containing the source files (.ts) from a tsconfig file. It can only be used if you create a project with a tsconfig.json
file. It is a replacement for gulp.src(..)
.Both ts(..)
and tsProject()
provide sub-streams that only contain the JavaScript or declaration files. An example is shown later in the readme.
Below is a minimal gulpfile.js
which will compile all TypeScript file in folder src
and emit a single output file called output.js
in built/local
. To invoke, simple run gulp
.
1var gulp = require('gulp'); 2var ts = require('gulp-typescript'); 3 4gulp.task('default', function () { 5 return gulp.src('src/**/*.ts') 6 .pipe(ts({ 7 noImplicitAny: true, 8 outFile: 'output.js' 9 })) 10 .pipe(gulp.dest('built/local')); 11});
Another example of gulpfile.js
. Instead of creating the default task, the file specifies custom named task. To invoke, run gulp scripts
instead of gulp
. As a result, the task will generate both JavaScript files and TypeScript definition files (.d.ts
).
1var gulp = require('gulp'); 2var ts = require('gulp-typescript'); 3var merge = require('merge2'); // Requires separate installation 4 5gulp.task('scripts', function() { 6 var tsResult = gulp.src('lib/**/*.ts') 7 .pipe(ts({ 8 declaration: true 9 })); 10 11 return merge([ 12 tsResult.dts.pipe(gulp.dest('release/definitions')), 13 tsResult.js.pipe(gulp.dest('release/js')) 14 ]); 15});
tsResult
is a stream containing the generated JavaScript and definition files.
In many situations, some plugins need to be executed on the JavaScript files.
For these situations, the stream has sub-streams, namely a JavaScript stream (tsResult.js
) and a definition file stream (tsResult.dts
).
You need to set the declaration
option to generate definition files.
If you don't need the definition files, you can use a configuration as seen in the first example, and you don't need to store the result into a variable as tsResult
.
Instead of calling ts(options)
, you can create a project first outside of the task. Inside the task, you should then use tsProject()
. An example:
1var gulp = require('gulp'); 2var ts = require('gulp-typescript'); 3var merge = require('merge2'); 4 5var tsProject = ts.createProject({ 6 declaration: true 7}); 8 9gulp.task('scripts', function() { 10 return gulp.src('lib/*.ts') 11 .pipe(tsProject()) 12 .pipe(gulp.dest('dist')); 13}); 14 15gulp.task('watch', ['scripts'], function() { 16 gulp.watch('lib/*.ts', ['scripts']); 17});
When you run gulp watch
, the source will be compiled as usual. Then, when you make a change and save the file, your TypeScript files will be compiled in about half the time.
You must create the project outside of the task. You can't use the same project in multiple tasks. Instead, create multiple projects or use a single task to compile your sources. Usually it is not worth to create different tasks for the client side, backend or tests.
tsconfig.json
To use tsconfig.json
, you have to use ts.createProject
:
1var tsProject = ts.createProject('tsconfig.json');
If you want to add or overwrite certain settings in the tsconfig.json
file, you can use:
1var tsProject = ts.createProject('tsconfig.json', { noImplicitAny: true });
The task will look like:
1gulp.task('scripts', function() { 2 var tsResult = gulp.src("lib/**/*.ts") // or tsProject.src() 3 .pipe(tsProject()); 4 5 return tsResult.js.pipe(gulp.dest('release')); 6});
You can replace gulp.src(...)
with tsProject.src()
to load files based on the tsconfig file (based on files
, excludes
and includes
).
gulp-typescript isn't restricted to a single TypeScript version.
You can install the latest stable version using npm install typescript --save-dev
or a nightly npm install typescript@next --save-dev
.
You can also use a fork of TypeScript, if it is based on TypeScript 2.x. You can configure this in your gulpfile:
1[...].pipe(ts({ 2 typescript: require('my-fork-of-typescript') 3}));
Or in combination with a tsconfig
file:
1var tsProject = ts.createProject('tsconfig.json', { 2 typescript: require('my-form-of-typescript') 3});
gulp-typescript supports source maps by the usage of the gulp-sourcemaps plugin. It works for both JavaScript and definition (.d.ts
) files. You don't have to set sourceMap
or declarationMap
in your configuration. When you use gulp-sourcemaps, they will be generated automatically.
Configuring the paths of source maps can be hard. The easiest way to get working source maps is to inline the sources of your TypeScript files in the source maps. This will of course increase the size of the source maps. The following example demonstrates this approach:
1var gulp = require('gulp') 2var ts = require('gulp-typescript'); 3var sourcemaps = require('gulp-sourcemaps'); 4 5gulp.task('scripts', function() { 6 return gulp.src('lib/*.ts') 7 .pipe(sourcemaps.init()) // This means sourcemaps will be generated 8 .pipe(ts({ 9 // ... 10 })) 11 .pipe( ... ) // You can use other plugins that also support gulp-sourcemaps 12 .pipe(sourcemaps.write()) // Now the sourcemaps are added to the .js file 13 .pipe(gulp.dest('dist')); 14});
When you are not inlining the source content, you should specify the sourceRoot
property. It can be configured with the following rule:
outDir
option to TypeScript, the sourceRoot
option of gulp-sourcemaps should be the relative path from the gulp.dest
path to the source directory (from gulp.src
)outDir
option to the same value as the directory in gulp.dest
, you should set the sourceRoot
to ./
.outDir
option to a different value, there is no easy rule to configure gulp-sourcemaps. I'd advise to change the value of outDir if possible.Furthermore you should set includeContent: false
. Here's an example where outDir
isn't set:
1gulp.task('scripts', function() { 2 return gulp.src('lib/*.ts') 3 .pipe(sourcemaps.init()) 4 .pipe(ts({ 5 // ... 6 })) 7 .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../lib' })) 8 .pipe(gulp.dest('dist')); 9});
Some examples can be found in ivogabe/gulp-typescript-sourcemaps-demo.
For more information, see gulp-sourcemaps.
You can pass aditional transforms to the compiler pipeline. We aligned with the interface of awesome-typescript-loader. You can specify transforms by setting the getCustomTransformers
option.
The option expects a string, pointing at a module that exposes the transforms, or a function that returns the transforms. Its type is getCustomTransformers: (string | ((program: ts.Program) => ts.CustomTransformers | undefined))
.
1const styledComponentsTransformer = require('typescript-plugin-styled-components').default; 2 3const project = ts.createProject('test/customTransformers/tsconfig.json', { 4 getCustomTransformers: () => ({ 5 before: [ 6 styledComponentsTransformer(), 7 ] 8 }); 9});
By default, errors are logged to the console and the build crashes on compiler errors. In watch mode, the build does not throw, meaning that consequent builds are still ran. Note that gulp 4 is required for this behaviour. If you are still using gulp 3, see the section "Gulp 3" below.
If you want to change the way that messages are logged to the console (or some other output), you can provide a reporter. You can specify a custom reporter as the second argument of the main function, or as the only argument when using a tsProject
:
1ts(options, reporter); 2tsProject(reporter);
Available reporters are:
ts.reporter.nullReporter()
) - Don't report errorsts.reporter.defaultReporter()
) - Report basic errors to the consolets.reporter.longReporter()
) - Extended version of default reporter, intelliJ link functionality + file watcher error highlighting should work using this onets.reporter.fullReporter(showFullFilename?: boolean)
) - Show full error messages, with source.If you want to build a custom reporter, you take a look at lib/reporter.ts
, that file declares an interface which a reporter should implement.
This plugin works best with gulp 4. If you cannot update to this version, you may experience problems when using incremental compilations with a watcher. A compilation error will namely crash the process, which is desired in a CI environment. Gulp 4 prevents that the process crashes in watch mode. This does not happen in gulp 3, so you will need to handle that manually.
You should attach an error handler to catch those compilation errors.
1gulp.src(..) 2 .pipe(ts(..)) 3 .on('error', () => { /* Ignore compiler errors */}) 4 .pipe(gulp.dest(..))
npm install
git submodule update --init
to pull down the TypeScript compiler/services versions used in the test suite.npm install -g gulp-cli
).gulp
.The plugin uses itself to compile. There are 2 build directories, release
and release-2
. release
must always contain a working build. release-2
contains the last build. When you run gulp compile
, the build will be saved in the release-2
directory. gulp test
will compile the source to release-2
, and then it will run some tests. If these tests give no errors, you can run gulp release
. The contents from release-2
will be copied to release
.
gulp-typescript is licensed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/14 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
26 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More