forked from goel4ever/usage-gulp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
286 lines (255 loc) · 7.86 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
/**
* Created by agoel on 5/4/15.
*/
'use strict';
/**
* ===================================================
* Workflow Management::
* ===================================================
*
* Start by:
* a. Clean existing distribution folder
* b. Preen bower packages based on need and configuration in bower json
* c. Find all files in bower components and create one each js/css by concatenating only (call it vendor.js and vendor.css)
* d. Find all the files being used from source
* e. Minify the files
* f. Concatenate the files into modularized files
* g. Save the final files
* h. Replace the references in template
* i. Add header to generated files
* j. Copy remaining files to the dist folder, for e.g., images, fonts
*
* Tasks that a build manager would be required to do:
* 1. Lint JS
* 2. Concat JS (into modular js files in temp folder or something)
* 3. Minify JS (files in temp folder)
* 4. Uglify JS (files in temp folder)
* 5. Have one task doing all of this
*
* 6. Lint HTML
* 7. Remove comments etc from HTML
*
* 8. Convert Sass and Less into CSS
* 9. Lint CSS after or before #8
* 10.Minify CSS
* 11.Uglify CSS
*
* 12. Cache management
* 13. Package management - dev-test-prod
* 14. Create a server
*
* 15. Can we Multi-thread few tasks that are unrelated???
* 16. Build failure management?? Exit on error and not proceed further.
* 17. How about adding Git hooks?
*
*/
/**
* [START] - Find all the files =====================================
*/
var allSources = 'components';
var allDestination = 'dist';
var jsAppSources = [
'components/**/*.js'
];
var jsVendorSources = [
'bower_components/**/*.js'
];
var jsDestination = 'dist/scripts';
var cssAppSources = [
'components/project/'
];
var cssVendorSources = [
'bower_components/**/*.min.css',
];
var cssDestination = 'dist/styles';
var htmlAppSources = [
'components/**/*.html'
];
var htmlDestination = 'dist/html';
/**
* [END] - Find all the files =======================================
*/
// require - Node method that tells that gulp is required by the code
var gulp = require('gulp'),
runSequence = require('run-sequence'),
del = require('del'),
clean = require('gulp-contrib-clean'),
copy = require('gulp-contrib-copy'),
preen = require('preen'),
add_header = require('gulp-header'),
// Add more 'src' files at any point in the pipeline
addSrc = require('gulp-add-src'),
// Replace etc with etc
replace = require('gulp-replace'),
csslint = require('gulp-csslint'),
less = require('gulp-less'),
compass = require('gulp-compass'),
// Minify CSS files
minify_css = require('gulp-minify-css'),
htmlhint = require('gulp-htmlhint'),
minify_html = require('gulp-minify-html'),
jshint = require('gulp-jshint'),
jshint_stylish = require('jshint-stylish'),
// Concatenate files
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
//
usemin = require('gulp-usemin'),
gutil = require('gulp-util'),
watch = require('gulp-watch');
/**
* [START] - Define executable tasks ================================
*/
/**
* Task [util:clean] - Meant to delete files and folders in a given path
* 'dist/development/js/project/scripts/*'
* 'dist/development/js/project/scripts/**'
* 'dist/development/js/project/scripts/** /*'
* 'dist/development/js/project/** /*'
* 'dist/development/js/project/scripts'
* 'dist/development/js/project/scripts'
*/
gulp.task('util:clean', function() {
gulp.src(allDestination)
.pipe(clean());
});
// Clean Distribution folder before executing any task
gulp.task('util:clean:dist', function(cb) {
del([
// here we use a globbing pattern to match everything inside the `dist` folder
'dist/**/*',
// Delete the folder itself
'dist',
// TODO: Negation does not work as of now. Figure out an alternative.
// we don't want to clean this file though so we negate the pattern
// TODO: remove script.js file once solution identified
'!dist/development/js/script.js',
'!dist/mobile/deploy.json'
], cb);
});
// Clean Temporary folder after executing all tasks
gulp.task('util:clean:tmp', function(cb) {
del([
// here we use a globbing pattern to match everything inside the `dist` folder
'tmp'
], cb);
});
/**
* Task [Util:Clean:Preen] - Meant to delete files that are not required in the package when building or working.
* Configuration is based on definition in bower.json
*/
gulp.task('util:clean:preen', function(cb) {
preen.preen({}, cb);
});
/**
* Task [Util:Copy] - Meant to provide a way to copy files or folder from source to destination
*/
gulp.task('util:copy', function() {
gulp.src(jsAppSources)
.pipe(copy()
.on('error', gutil.log))
.pipe(gulp.dest(jsDestination));
});
// Add header to each file generated
gulp.task('util:add:header', function() {
gulp.src(jsDestination + '/**/*.js')
.pipe(add_header("/* This file is auto-generated — do not edit by hand! */\n"))
.pipe(gulp.dest(jsDestination));
gulp.src(cssDestination + '/**/*.css')
.pipe(add_header("/* This file is auto-generated — do not edit by hand! */\n"))
.pipe(gulp.dest(cssDestination));
gulp.src(htmlDestination + '/**/*.html')
.pipe(add_header("<!-- This file is auto-generated — do not edit by hand! -->\n"))
.pipe(gulp.dest(htmlDestination));
});
/**
* [END] - Define executable tasks ==================================
*/
/**
* [START] - Tasks built to handle individual components =========================
*/
gulp.task('build:js', function() {
gulp.src(jsAppSources)
.pipe(jshint()
.on('error', gutil.log))
.pipe(jshint.reporter("default"))
.pipe(uglify()
.on('error', gutil.log))
.pipe(concat('script.min.js')
.on('error', gutil.log))
.pipe(gulp.dest(jsDestination));
gulp.src(jsVendorSources)
.pipe(concat('vendor.min.js')
.on('error', gutil.log))
.pipe(gulp.dest(jsDestination));
});
gulp.task('build:css', function() {
gulp.src([
cssAppSources + '**/*.less',
cssAppSources + '**/*.css'
])
.pipe(csslint())
.pipe(csslint.reporter())
.pipe(less()
.on('error', gutil.log))
.pipe(concat('main.min.css')
.on('error', gutil.log))
.pipe(minify_css()
.on('error', gutil.log))
.pipe(gulp.dest(cssDestination));
gulp.src(cssVendorSources)
.pipe(concat('vendor.min.css')
.on('error', gutil.log))
.pipe(gulp.dest(cssDestination));
});
gulp.task('build:html', function() {
gulp.src(htmlAppSources)
.pipe(htmlhint()
.on('error', gutil.log))
.pipe(minify_html({empty: true})
.on('error', gutil.log))
.pipe(gulp.dest(htmlDestination));
});
gulp.task('server', ['util:clean:dist', 'util:clean:tmp']);
gulp.task('watch', function() {
gulp.watch("components/**/*.js", ['lint:js']);
});
/**
* [END] - Tasks built to handle individual components ===========================
*/
/**
* [START] - Tasks to be exported for consumption ================================
*/
gulp.task('serve:build', function(cb) {
runSequence(
['util:clean:dist', 'util:clean:preen'],
['build:js', 'build:css', 'build:html'],
'util:add:header',
//'server',
//'watch',
cb);
console.log('Default Task completed');
});
gulp.task('serve:debug', function(cb) {
runSequence(
['util:clean:dist', 'util:clean:preen'],
['build:js', 'build:css', 'build:html'],
'util:add:header',
//'server',
//'watch',
cb);
console.log('Debug Task completed');
});
gulp.task('serve:release', function(cb) {
runSequence(
['util:clean:dist', 'util:clean:preen'],
['build:js', 'build:css', 'build:html'],
'util:add:header',
//'server',
//'watch',
cb);
console.log('Release Task completed');
});
/**
* [END] - Tasks to be exported for consumption ==================================
*/