PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/gulp-typescript/readme.md

https://gitlab.com/minhaj_mimo/weatherApp
Markdown | 290 lines | 245 code | 45 blank | 0 comment | 0 complexity | 8ca20a0bb96ebeb4dad0672abea9a008 MD5 | raw file
  1. gulp-typescript
  2. ===============
  3. A gulp plugin for handling TypeScript compilation workflow. The plugin exposes TypeScript's compiler options to gulp using TypeScript API.
  4. [![Build Status](https://travis-ci.org/ivogabe/gulp-typescript.svg?branch=master)](https://travis-ci.org/ivogabe/gulp-typescript)
  5. Features
  6. --------
  7. - Incremental compilation (so faster builds)
  8. - Error reporting
  9. - Different output streams for .js, .d.ts files.
  10. - Support for sourcemaps using gulp-sourcemaps
  11. - Compile once, and filter different targets
  12. How to install
  13. --------------
  14. ##### 1. Install gulp
  15. ```shell
  16. npm install --global gulp
  17. ```
  18. ##### 2. Install gulp in the project dependency
  19. ```shell
  20. npm install gulp
  21. ```
  22. ##### 3. Install gulp-typescript
  23. ```shell
  24. npm install gulp-typescript
  25. ```
  26. Options
  27. -------
  28. Allmost all options from TypeScript are supported.
  29. - `out` (TS1.5-), `outFile` (TS1.6+) (string) - Generate one javascript and one definition file. Only works when no module system is used.
  30. - `outDir` (string) - Move output to a different (virtual) directory. Note that you still need `gulp.dest` to write output to disk.
  31. - `noImplicitAny` (boolean) - Warn on expressions and declarations with an implied 'any' type.
  32. - `suppressImplicitAnyIndexErrors` (boolean) - Suppress --noImplicitAny errors for indexing objects lacking index signatures.
  33. - `noLib` (boolean) - Don't include the default lib (with definitions for - Array, Date etc)
  34. - `target` (string) - Specify ECMAScript target version: 'ES3' (default), 'ES5' or 'ES6'.
  35. - `module` (string) - Specify module code generation: 'commonjs', 'amd', 'umd' or 'system'.
  36. - `jsx` (string) - Specify jsx code generation: 'react' or 'preserve' (TS1.6+).
  37. - `declaration` (boolean) - Generates corresponding .d.ts files. You need to pipe the `dts` streams to save these files.
  38. - `removeComments` (boolean) - Do not emit comments to output.
  39. - `emitDecoratorMetadata` (boolean) - Emit design-time metadate for decorated declarations in source.
  40. - `experimentalAsyncFunctions` (boolean) - Support for ES7-proposed asynchronous functions using the `async`/`await` keywords (TS1.6+).
  41. - `experimentalDecorators` (boolean) - Enables experimental support for ES7 decorators.
  42. - `moduleResolution` (string) - Determine how modules get resolved. Either 'node' for Node.js/io.js style resolution, or 'classic' (default) (TS1.6+).
  43. - `noEmitOnError` (boolean) - Do not emit outputs if any type checking errors were reported.
  44. - `noEmitHelpers` (boolean) - Do not generate custom helper functions like __extends in compiled output.
  45. - `preserveConstEnums` (boolean) - Do not erase const enum declarations in generated code.
  46. - `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+.
  47. - `allowJs` (boolean) - Allow JavaScript files to be compiled.
  48. - `rootDir` - Specifies the root directory of input files. Only use to control the output directory structure with `outDir`.
  49. See the [TypeScript wiki](http://www.typescriptlang.org/docs/handbook/compiler-options.html) for a complete list.
  50. These options are not supported:
  51. - Sourcemap options (`sourceMap`, `inlineSourceMap`, `inlineSources`, `sourceRoot`) - Use [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) instead.
  52. - `watch` - Use `gulp.watch` instead. See the paragraph "Incremental compilation".
  53. - `project` - See "Using `tsconfig.json`".
  54. - Obvious: `help`, `version`
  55. ## Unofficial options
  56. Besides the official options options, gulp-typescript supports the following options:
  57. - ```noExternalResolve``` (boolean) - Do not resolve files that are not in the input. Explanation below.
  58. - ```sortOutput``` (boolean) - Sort output files. Useful if you want to concatenate files (see below).
  59. - ```typescript``` (object) - Use a different version / fork of TypeScript (see below). Use it like: `typescript: require('typescript')` or `typescript: require('my-fork-of-typescript')`
  60. Basic Usage
  61. ----------
  62. 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`.
  63. ```javascript
  64. var gulp = require('gulp');
  65. var ts = require('gulp-typescript');
  66. gulp.task('default', function () {
  67. return gulp.src('src/**/*.ts')
  68. .pipe(ts({
  69. noImplicitAny: true,
  70. out: 'output.js'
  71. }))
  72. .pipe(gulp.dest('built/local'));
  73. });
  74. ```
  75. Another example of `gulpfile.js`. Instead of creating 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).
  76. ```javascript
  77. var gulp = require('gulp');
  78. var ts = require('gulp-typescript');
  79. var merge = require('merge2'); // Require separate installation
  80. gulp.task('scripts', function() {
  81. var tsResult = gulp.src('lib/**/*.ts')
  82. .pipe(ts({
  83. declaration: true,
  84. noExternalResolve: true
  85. }));
  86. return merge([
  87. tsResult.dts.pipe(gulp.dest('release/definitions')),
  88. tsResult.js.pipe(gulp.dest('release/js'))
  89. ]);
  90. });
  91. ```
  92. `tsResult` is a object that has a JavaScript stream (`.js`) and a definition file stream (`.dts`).
  93. You need to set the `declaration` option to get definition files in the `dts` stream.
  94. If you don't need the definition files, you can use a configuration as seen in the first example.
  95. Incremental compilation
  96. -----------------------
  97. Instead of calling ```ts(options)```, you can create a project first, and then call ```ts(project)```. An example:
  98. ```javascript
  99. var gulp = require('gulp');
  100. var ts = require('gulp-typescript');
  101. var merge = require('merge2');
  102. var tsProject = ts.createProject({
  103. declaration: true,
  104. noExternalResolve: true
  105. });
  106. gulp.task('scripts', function() {
  107. var tsResult = gulp.src('lib/*.ts')
  108. .pipe(ts(tsProject));
  109. return merge([ // Merge the two output streams, so this task is finished when the IO of both operations are done.
  110. tsResult.dts.pipe(gulp.dest('release/definitions')),
  111. tsResult.js.pipe(gulp.dest('release/js'))
  112. ]);
  113. });
  114. gulp.task('watch', ['scripts'], function() {
  115. gulp.watch('lib/*.ts', ['scripts']);
  116. });
  117. ```
  118. 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.
  119. You must create the project outside of the task. You can't use the same project in multiple tasks.
  120. Instead, create multiple projects or use a single task to compile your sources.
  121. Using `tsconfig.json`
  122. -------------
  123. To use `tsconfig.json`, you have to use `ts.createProject`:
  124. ```javascript
  125. var tsProject = ts.createProject('tsconfig.json');
  126. ```
  127. If you want to add or overwrite certain settings in the `tsconfig.json` file, you can use:
  128. ```javascript
  129. var tsProject = ts.createProject('tsconfig.json', { sortOutput: true });
  130. ```
  131. The task will look like:
  132. ```javascript
  133. gulp.task('scripts', function() {
  134. var tsResult = tsProject.src() // instead of gulp.src(...)
  135. .pipe(ts(tsProject));
  136. return tsResult.js.pipe(gulp.dest('release'));
  137. });
  138. ```
  139. TypeScript version
  140. ------------------
  141. gulp-typescript uses TypeScript 1.8 by default. You can also use 1.4+ or a nighty version of TypeScript instead.
  142. You should add the version you want (1.4+) to your package.json file as a devDependency. You can use the a nightly build to get the latest features:
  143. ```
  144. npm install typescript@next
  145. ```
  146. And add this to your gulpfile:
  147. ```javascript
  148. [...].pipe(ts({
  149. typescript: require('typescript')
  150. }));
  151. ```
  152. Or in combination with a `tsconfig` file:
  153. ```javascript
  154. var tsProject = ts.createProject('tsconfig.json', {
  155. typescript: require('typescript')
  156. });
  157. ```
  158. It's also possible to use a fork of TypeScript. Add an extra option to the options object like this:
  159. ```javascript
  160. [...].pipe(ts({
  161. typescript: require('my-fork-of-typescript')
  162. }));
  163. ```
  164. Filters
  165. -------
  166. There are two ways to filter files:
  167. ```javascript
  168. gulp.task('scripts', function() {
  169. var tsResult = gulp.src('lib/*.ts')
  170. .pipe(ts(tsProject, filterSettings));
  171. ...
  172. });
  173. ```
  174. And
  175. ```javascript
  176. gulp.task('scripts', function() {
  177. var tsResult = gulp.src('lib/*.ts')
  178. .pipe(ts(tsProject));
  179. tsResult.pipe(ts.filter(tsProject, filterSettings));
  180. });
  181. ```
  182. The first example doesn't add files (that don't pass the filter) to the compiler, the second one does add them to the compiler,
  183. but removes them later from the stream.
  184. You can put as much pipes between compilation and filtering as you want, as long as the filename doesn't change.
  185. At the moment there is only one filter available:
  186. - ```referencedFrom``` (string[]) Only files that are referenced (using ```/// <reference path="..." />```) by the files in this array pass this filter.
  187. Resolving files
  188. ---------------
  189. By default, gulp-typescript will try to resolve the files you require and reference. These files are parsed, but not emitted (so you will not see them in the output stream).
  190. If you set the option ```noExternalResolve``` to true, gulp-typescript will not resolve all the requires and references. It assumes that all the necessary files are in the input stream. For example, if you have your ```.ts``` files in the ```lib``` folder, and the ```.d.ts``` files in the ```definitions``` folder, you must use ```gulp.src(['lib/**.ts', 'definitions/**.ts'])``` instead of ```gulp.src(['lib/**.ts'])``` in your gulpfile if you use the option ```noExternalResolve```.
  191. Advantage of ```noExternalResolve```: faster compilation.
  192. Disadvantage of ```noExternalResolve```: won't work when you forgot some input files.
  193. Advice: turn it on, and make sure you list all the input files.
  194. Files that are resolved when ```noExternalResolve``` is off, won't be pushed to the output stream, unless you are using the `out` option.
  195. Concatenate files
  196. ------------
  197. The ```tsc``` command has the ability to concatenate using the ```--out``` parameter. There are two approaches to do that in ```gulp-typescript```.
  198. You can use the `out` option. This is fine for small projects, but for big projects it's not always sufficient.
  199. The other option is to use `gulp-concat`. The ```tsc``` command sorts the files using the ```<reference>``` tags. ```gulp-typescript``` does this when you enable the ```sortOutput``` option. You can use the ```referencedFrom``` filter to only include files that are referenced from certain files.
  200. Source maps
  201. ----------
  202. Example of ```gulpfile.js``` which will compile typescript to javascript as well as generate
  203. associated sourcemap.
  204. ```javascript
  205. var gulp = require('gulp')
  206. var ts = require('gulp-typescript');
  207. var concat = require('gulp-concat');
  208. var sourcemaps = require('gulp-sourcemaps');
  209. gulp.task('scripts', function() {
  210. var tsResult = gulp.src('lib/*.ts')
  211. .pipe(sourcemaps.init()) // This means sourcemaps will be generated
  212. .pipe(ts({
  213. sortOutput: true,
  214. // ...
  215. }));
  216. return tsResult.js
  217. .pipe(concat('output.js')) // You can use other plugins that also support gulp-sourcemaps
  218. .pipe(sourcemaps.write()) // Now the sourcemaps are added to the .js file
  219. .pipe(gulp.dest('release/js'));
  220. });
  221. ```
  222. For more information, see [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps).
  223. Reporters
  224. ---------
  225. You can specify a custom reporter as the 3rd argument of the main function:
  226. ```javascript
  227. ts(optionsOrProject, filters, reporter);
  228. ```
  229. You can set options, project or filter to `undefined` if you don't want to set them. Available reporters are:
  230. - nullReporter (`ts.reporter.nullReporter()`) - Don't report errors
  231. - defaultReporter (`ts.reporter.defaultReporter()`) - Report basic errors to the console
  232. - longReporter (`ts.reporter.longReporter()`) - Extended version of default reporter, intelliJ link functionality + file watcher error highlighting should work using this one
  233. - fullReporter (`ts.reporter.fullReporter(showFullFilename?: boolean)`) - Show full error messages, with source.
  234. If you want to build a custom reporter, you take a look at `lib/reporter.ts`, in that file is an interface which a reporter should implement.
  235. Build gulp-typescript
  236. ------------
  237. 1. Clone this repo
  238. 2. Execute `npm install`
  239. 3. Execute `git submodule update --init` to pull down the TypeScript compiler/services versions used in the test suite.
  240. 4. Ensure the gulp CLI is globally installed (`npm install -g gulp`).
  241. 5. Execute the tests: `gulp`.
  242. 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```.
  243. License
  244. -------
  245. gulp-typescript is licensed under the [MIT license](http://opensource.org/licenses/MIT).