/
gulpfile.js
290 lines (263 loc) · 7.08 KB
/
gulpfile.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Build Dependencies
*/
var fs = require('fs'),
merge = require('merge-stream'),
gulp = require('gulp'),
angularFilesort = require('gulp-angular-filesort'),
gutil = require('gulp-util'),
jshint = require('gulp-jshint'),
ngAnnotate = require('gulp-ng-annotate'),
uglify = require('gulp-uglifyjs'),
htmlmin = require('gulp-minify-html'),
templateCache = require('gulp-angular-templatecache'),
cssmin = require('gulp-minify-css'),
concat = require('gulp-concat'),
rimraf = require('gulp-rimraf'),
rename = require('gulp-rename'),
size = require('gulp-size'),
newer = require('gulp-newer'),
imagemin = require('gulp-imagemin'),
pngcrush = require('imagemin-pngcrush'),
flatten = require('gulp-flatten'),
taskListing = require('gulp-task-listing');
/**
* ======================================
* === PATHS ==========================
* ======================================
*/
/**
* Define root project paths
* @type {Object}
*/
var paths = {
build: 'dist/',
app: 'src/',
assets: 'src/assets/',
tmp: 'tmp/',
vendor: 'vendor/standalone/',
bower: 'vendor/bower/'
};
/**
* Define source globs
* @type {Object}
*/
var sources = {
js: [
paths.app + 'common/**/*.js',
paths.app + 'app/**/*.js'
],
// Assets can be stored within component directories as well
images: [
paths.app + '**/*.png',
paths.app + '**/*.jpg',
paths.app + '**/*.jpeg',
paths.app + '**/*.gif',
paths.app + '**/*.webp'
],
css: paths.app + '**/*.css',
tpl: paths.app + '**/*.tpl.html',
html: [paths.app + '**/*.html', '!**/*.tpl.html'],
// Will get concatenated to libs.js
vendorjs: [
// Common deps
paths.bower + 'jquery/jquery.min.js',
paths.bower + 'underscore/underscore.js',
paths.bower + 'bootstrap/dist/js/bootstrap.min.js',
paths.bower + 'angular/angular.min.js',
// Other bower deps
paths.bower + 'angular-flare/dist/angular-flare.min.js',
paths.bower + 'angular-resource/angular-resource.min.js',
paths.bower + 'angular-route/angular-route.min.js',
paths.bower + 'angular-ui/build/angular-ui.min.js',
paths.bower + 'autofill-event/src/autofill-event.js',
// Manual vendor deps
paths.vendor + '**/*.js',
]
};
/**
* Define destination paths
* @type {Object}
*/
var dests = {
build: {
root: paths.build,
js: paths.build + 'js',
images: paths.build + 'images',
css: paths.build + 'css',
jsorig: paths.build + 'js/sources'
},
filenames: {
app: 'app.js',
appmin: 'app.min.js',
vendor: 'libs.js',
vendormin: 'libs.min.js'
}
};
/**
* ======================================
* === TASKS ==========================
* ======================================
*/
gulp.task('default', taskListing);
gulp.task('build', ['css', 'html', 'images', 'js', 'vendor']);
/**
* lint
*/
gulp.task('lint', function () {
return gulp.src(sources.js)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
/**
* clean
*/
gulp.task('clean', function () {
return gulp.src([paths.build, paths.tmp], {
read: false
})
.pipe(rimraf());
});
/**
* build
*/
gulp.task('js', ['lint'], function () {
// Angular application source code
var src = gulp.src(sources.js)
.pipe(ngAnnotate());
// Angular html templates
var tpl = gulp.src(sources.tpl)
.pipe(htmlmin())
.pipe(templateCache());
// Merge the two streams to create an unminified, concatenated build file
var build = merge(src, tpl)
// Ensure that the order in which we load files allows for angular modules
// to be defined before accessing (downside to not using broserify)
.pipe(angularFilesort())
.pipe(uglify(dests.filenames.app, {
mangle: false,
output: {
beautify: true
},
enclose: {
"window": "window",
"window.angular": "angular",
"undefined": "undefined"
}
}));
// Merge the two streams to create a minified build file
var buildmini = merge(src, tpl)
.pipe(angularFilesort())
// Remove paths so that we can have the sourcemap point to a single dir
.pipe(flatten())
// Write out all raw source files into the dist dir (for sourcemaps)
.pipe(gulp.dest(dests.build.js + '/sources'))
// Minify and produce source maps
.pipe(uglify(dests.filenames.appmin, {
outSourceMap: true,
basePath: 'dist/js',
enclose: {
"window": "window",
"window.angular": "angular",
"undefined": "undefined"
}
}));
// Merged streams since we split them
// and write both minified and unminified outputs
return merge(build, buildmini)
.pipe(size({
showFiles: true
}))
.pipe(gulp.dest(dests.build.js));
});
/**
* vendor js packaging
*/
gulp.task('vendor', function () {
var vendors = gulp.src(sources.vendorjs)
// Remove paths so that we can have the sourcemap point to a single dir
.pipe(flatten())
// Write out all raw source files into the dist dir (for sourcemaps)
.pipe(gulp.dest(dests.build.jsorig))
// Ensure that any input file is newer than the output combined first
// gulp-newer, for combined files, requires up to point to the output file
.pipe(newer([
dests.build.js,
dests.filenames.vendormin
].join('/')));
var vendorraw = vendors
.pipe(concat(dests.filenames.vendor));
var vendormin = vendors
// Minify
.pipe(uglify(dests.filenames.vendormin, {
outSourceMap: true,
basePath: dests.build.js
}));
// Merge streams since we split them
// and write both minified and unminified outputs
return merge(vendorraw, vendormin)
.pipe(size({
showFiles: true
}))
.pipe(gulp.dest(dests.build.js));
});
/**
* css minification
*/
gulp.task('css', function () {
return gulp.src(sources.css)
// Ensure that any input file is newer than its output first
.pipe(flatten())
.pipe(newer(dests.build.css))
// Minify the css
.pipe(cssmin())
.pipe(size({
showFiles: true
}))
.pipe(gulp.dest(dests.build.css));
});
/**
* html minification (non-templates)
*/
gulp.task('html', function () {
return gulp.src(sources.html)
// Ensure that any input file is newer than its output first
.pipe(flatten())
.pipe(newer(dests.build.root))
// Minify the html
.pipe(htmlmin())
.pipe(size({
showFiles: true
}))
.pipe(gulp.dest(dests.build.root));
});
/**
* image minification
*/
gulp.task('images', function () {
return gulp.src(sources.images)
.pipe(flatten())
// Ensure that any input file is newer than its output first
.pipe(newer(dests.build.images))
.pipe(imagemin({
progressive: true,
use: [
pngcrush({
reduce: true
})
],
optimizationLevel: 5
}))
.pipe(gulp.dest(dests.build.images));
});
/**
* ======================================
* === WATCHES ========================
* ======================================
*/
gulp.task('watch', ['build'], function () {
gulp.watch([sources.js, sources.tpl], ['lint', 'js']);
gulp.watch(sources.css, ['css']);
gulp.watch(sources.images, ['images']);
gulp.watch(sources.html, ['html']);
});