Useful Gulp Task

By: Ryan Wong at

Here are some very useful gulp task.

SASS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');

gulp.task('sass', function(done) {
gulp.src(paths.sassSrc)
.pipe(sass())
.on('error', sass.logError)
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(gulp.dest(paths.sassDest))
.on('end', done);
});

gulp.task('sass:dev', function(done) {
gulp.src(paths.sassSrc)
.pipe(sass())
.on('error', sass.logError)
.pipe(gulp.dest(paths.sassDest))
.on('end', done);
});

gulp.task('sass:prod', function(done) {
gulp.src(paths.sassSrc)
.pipe(sass())
.on('error', sass.logError)
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest(paths.sassProdDest))
.on('end', done);
});

Templating:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var templateCache = require('gulp-angular-templatecache');

gulp.task('template:dev', function () {
return gulp.src(paths.templateSrc)
.pipe(templateCache({
standalone: true,
module: 'videa.templates'
}))
.pipe(gulp.dest(paths.bundleDest));
});

gulp.task('template:prod', function(){
return gulp.src(paths.templateSrc)
.pipe(templateCache({
standalone: true,
module: 'videa.templates'
}))
.pipe(gulp.dest(paths.jsProdDest));
});

Packaging all vendor bower components to one file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var ngAnnotate = require('browserify-ngannotate');
var ngAnnotateAngular = require('gulp-ng-annotate');
var mainBowerFiles = require('main-bower-files');
var print = require('gulp-print');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var filter = require('gulp-filter');

gulp.task('package-bower-js:dev', function(){

//Package Vendor JS Files
gulp.src(mainBowerFiles({
overrides: {
jquery: {
ignore: true
},
bootstrap: {
ignore: true
}
}
}))
.pipe(filter(['**/*.js','*.js']))
//.pipe(print())
.pipe(concat('vendor.js'))
//.pipe(plugins.uglify())
.pipe(gulp.dest(paths.bundleDest));
});

gulp.task('package-bower-css:dev', function(){

//Package Vendor CSS Files
gulp.src(mainBowerFiles({
overrides: {
bootstrap: {
main: [
'./dist/css/bootstrap.min.css'
]
}
}
}))
.pipe(filter(['*.css', '*.scss','**/*.css']))
.pipe(concat('vendor.css'))
.pipe(gulp.dest(paths.cssDest));
});

gulp.task('package-bower-fonts:dev', function(){

//Package Vendor Font Files
gulp.src(mainBowerFiles({
overrides: {
bootstrap: {
main: [
'./fonts/*'
]
}
}
}))

.pipe(filter(['**/*.eot', '**/*.svg', '**/*.ttf', '**/*.woff', '**/*.woff2',
'./*.eot', '*.svg', '*.ttf', '*.woff', '*.woff2']))
.pipe(gulp.dest(paths.fontDest));
});

gulp.task('package-bower-js:prod', function(){

//Package Vendor JS Files
gulp.src(mainBowerFiles({
overrides: {
jquery: {
ignore: true
},
bootstrap: {
ignore: true
}
}
}))
.pipe(filter(['**/*.js','*.js']))
.pipe(concat(paths.vendorProdSrc))
.pipe(ngAnnotateAngular())
.pipe(streamify(uglify({
output: {
comments: false
},
mangle: true
})))
.pipe(gulp.dest(paths.jsProdDest));
});

gulp.task('package-bower-css:prod', function(){

//Package Vendor CSS Files
gulp.src(mainBowerFiles({
overrides: {
bootstrap: {
main: [
'./dist/css/bootstrap.min.css'
]
}
}
}))
.pipe(filter(['*.css', '*.scss','**/*.css']))
// .pipe(print())
.pipe(concat(paths.vendorCSSProdSrc))
.pipe(minify())
.pipe(gulp.dest(paths.sassProdDest));
});

gulp.task('package-bower-fonts:prod', function(){

//Package Vendor Font Files
gulp.src(mainBowerFiles({
overrides: {
bootstrap: {
main: [
'./fonts/*'
]
}
}
}))

.pipe(filter(['**/*.eot', '**/*.svg', '**/*.ttf', '**/*.woff', '**/*.woff2',
'./*.eot', '*.svg', '*.ttf', '*.woff', '*.woff2']))
.pipe(gulp.dest(paths.fontProdDest));
});
```

Our bower.js
```js

{
"name": "hahaha",
"version": "1.1.38",
"description": "hahaha",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"font-awesome": "~4.3.0",
"animate.css": "~3.3.0",
"underscore": "~1.8.3",
"angular": "~1.4.10",
"angular-animate": "~1.4.9",
"angular-breadcrumb": "~0.4.0",
"angular-cookies": "~1.4.4",
"angular-ui-router": "~0.2.14",
"angular-permission": "~0.3.1",
"ngstorage": "~0.3.3",
"angular-bootstrap": "~1.1.2",
"angular-ui-utils": "~3.0.0",
"ng-file-upload": "~5.0.9",
"angular-loader": "~1.4.4",
"angular-messages": "~1.4.4",
"angular-resource": "~1.4.4",
"angular-route": "~1.4.4",
"angular-sanitize": "~1.4.4",
"angular-touch": "~1.4.4",
"angular-loading": "~0.1.4",
"angularjs-slider": "~2.1.0",
"tinycolor": "~1.3.0",
"ut-angular-toast": "*",
"ui-select": "angular-ui-select#~0.13.2",
"bootstrap": "3.3.0",
"angular-color-picker": "angularjs-color-picker#~0.8.1",
"angular-ui-ace": "bower",
"html5-sortable-angularjs": "*"
},
"resolutions": {
"angular": "1.4.10"
}
}

Browserify:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var browserify = require('browserify');
var vinylSource = require('vinyl-source-stream');
var ngAnnotate = require('browserify-ngannotate');
var ngAnnotateAngular = require('gulp-ng-annotate');
var streamify = require('gulp-streamify');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');

gulp.task('browserify:dev', function() {
return browserify(paths.entry, {
transform: [ngAnnotate],
debug: true,
cache: {}, packageCache: {}, fullPaths: true,
paths: [paths.browserifyAliasPath]
})
.bundle()
.on('error', function(message){
console.log(message);
})
.pipe(vinylSource(paths.bundleSrc))
.pipe(gulp.dest(paths.bundleDest));
});

gulp.task('browserify:prod', function () {
var bundleStream = browserify(paths.entry, {
transform: [ngAnnotate],
paths: [paths.browserifyAliasPath],
debug: true,
cache: {}, packageCache: {}, fullPaths: true
})
.bundle();

bundleStream
.pipe(vinylSource(paths.bundleSrc))
.pipe(streamify(uglify({
output: {
comments: false
},
mangle: false
})))
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest(paths.jsProdDest));
});

Starting server

1
2
3
4
5
6
var gls = require('gulp-live-server');

gulp.task('server', function() {
var server = gls('bin/www', {env: {NODE_ENV: 'development'}}, false);
server.start();
});

Compress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var zip = require('gulp-zip');

Date.prototype.YYYYMMDDHHMMSS = function () {
var yyyy = this.getFullYear().toString();
var MM = pad(this.getMonth() + 1,2);
var dd = pad(this.getDate(), 2);
var hh = pad(this.getHours(), 2);
var mm = pad(this.getMinutes(), 2)
var ss = pad(this.getSeconds(), 2)
return yyyy + MM + dd+ hh + mm + ss;
};
gulp.task('compress', function () {
return gulp.src([
'package.json',
'README.txt'], {base: "."})
.pipe(zip('V' + new Date().YYYYMMDDHHMMSS() + '.zip'))
.pipe(gulp.dest('dist'));
});

Typescript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');

gulp.task('typescript:backend', function() {
return gulp.src(paths.typescriptBackendSrc, { base: "." })
.pipe(sourcemaps.init())
.pipe(ts({
noExternalResolve: false,
noImplicitAny: false,
noLib: false,
module: 'commonjs',
target: 'ES5',
removeComments: true,
moduleResolution : "classic"
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('.'));
});