UNPKG

3.31 kBMarkdownView Raw
1# Make a beautiful class-based gulpfiles with Typescript and Gulpclass
2
3Allows to create a gulp files in classes, each method of which can be a gulp task.
4
5## Installation
6
71. Install module:
8
9 `npm install gulpclass --save-dev`
10
112. Use [typings](https://github.com/typings/typings) to install all required definition dependencies.
12
13 `typings install`
14
15## Usage
16
171. Create a `gulpfile.ts` and describe your tasks
18
19 ```javascript
20 import {Gulpclass, Task, SequenceTask} from "gulpclass/Decorators";
21
22 let gulp = require("gulp");
23 let del = require("del");
24
25 @Gulpclass()
26 export class Gulpfile {
27
28 @Task()
29 clean(cb: Function) {
30 return del(["./dist/**"], cb);
31 }
32
33 @Task()
34 copyFiles() {
35 return gulp.src(["./README.md"])
36 .pipe(gulp.dest("./dist"));
37 }
38
39 @Task("copy-source-files") // you can specify custom task name if you need
40 copySourceFiles() {
41 return gulp.src(["./src/**.js"])
42 .pipe(gulp.dest("./dist/src"));
43 }
44
45 @SequenceTask() // this special annotation using "run-sequence" module to run returned tasks in sequence
46 build() {
47 return ["copyFiles", "copy-source-files"];
48 }
49
50 @Task()
51 default() { // because this task has "default" name it will be run as default gulp task
52 return ["build"];
53 }
54
55 }
56 ```
57
582. How to run
59
60 There is a caveat of using gulp and typescript together. The problem is that when you run your `gulp` commands
61 in console, gulp cannot read your tasks from your typescript code - it can read only from `gulpfile.js`.
62 But there is a simple workaround - you can create a gulpfile.js, compile and execute typescript on-the-fly.
63
64 create a **gulpfile.js** and put there this peace of code:
65 ```javascript
66 eval(require("typescript").transpile(require("fs").readFileSync("./gulpfile.ts").toString()));
67 ```
68 this peace of code reads your gulpfile.ts contents, and asks typescript to transpile it on-the-fly and executes transpiled result as javascript.
69
70 (you need to run `npm install typescript --save-dev` if you dont have typescript package installed)
71
72 Please note that if you are NOT using `outDir` in typescript compile options, you may have problems if your
73 gulpclass file is named `gulpfile.ts` typescript compiler will try to compile it to `gulpfile.js`, and will override
74 code you added to gulpfile.js. Solution is simple - rename your `gulpfile.ts`. You can call it as you wish,
75 for example you can call it `gulpclass.ts`.
76
77 **alternative approaches (not recommended):**
78
79 alternative approaches depend of tsconfig configuration you use. These examples assume that you are using
80 `"outDir": "build"` as a directory to where files are compiled:
81
82 * create `gulpfile.js` and put there ```require("build/gulpfile")```
83 * or run gulp in cmd with extra parameters: `gulp --gulpfile build/gulpfile.js --cwd .`
84
85## Samples
86
87This project itself using [gulpfile.ts](https://github.com/PLEEROCK/gulpclass/blob/master/gulpfile.ts).
88Take a look on it as an example.
\No newline at end of file