forked from exodus4d/pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
275 lines (234 loc) · 6.15 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
/* GULP itself */
var gulp = require('gulp-param')(require('gulp'), process.argv);
var jshint = require('gulp-jshint');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var gzip = require('gulp-gzip');
var gulpif = require('gulp-if');
var clean = require('gulp-clean');
var critical = require('critical');
var runSequence = require('run-sequence');
var exec = require('child_process').exec;
var path = require('path');
var stylish = require('jshint-stylish');
/*******************************************/
// Source and destination file paths
var _src = {
GULP: './gulpfile.js',
ICON: './public/img/notifications/logo.png',
JS_SRC: './js/**/*',
JS_LIBS: './js/lib/**/*',
JS_BUILD: './build_js',
JS_DIST: './public/js',
CSS_SRC: './public/css/*.css',
CSS_DIST: './public/css',
PACKAGE: './package.json',
CACHE: './tmp/**/*.*'
};
// Gulp plumber error handler
var onError = function(err) {
'use strict';
console.log(err);
};
/*******************************************/
// Build Configuration
var isProductionBuild = false;
/**
* Version nr (e.g. v0.0.4)
* required for "production" task
* @type {null}
*/
var tagVersion = null;
/**
* RequireJS build task using the r.js optimizer.
*/
gulp.task('requirejs', ['jshint'], function() {
'use strict';
var rjsPath = path.resolve(__dirname, './node_modules/requirejs/bin/r.js');
var oPath = path.resolve(__dirname, './build.js');
exec('node ' + rjsPath + ' -o ' + oPath, function(error, stdout, stderr) {
var success = true;
console.log('[RequireJS]', stderr);
if (error !== null) {
console.log('[RequireJS]', error);
success = false;
}
runSequence(
'copyJSBuildFiles',
'removeBuildFiles',
'gzipJS'
);
});
});
/*******************************************/
/**
* JSHint JavaScript and JSON
* see .jshintrc for configuration
* http://jshint.com/docs/
* http://jshint.com/docs/options/
*/
gulp.task('jshint', function(){
'use strict';
return gulp.src([
_src.JS_SRC,
'!' + _src.JS_LIBS
])
.pipe(plumber({
errorHandler: onError
}))
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.pipe(notify({
icon: path.resolve(__dirname, _src.ICON),
title: 'JSHint',
message: 'Task complete',
onLast: true
}));
// .pipe(jshint.reporter('fail')); // uncomment this line to stop build on error
});
/**
* Copy optimized/uglyfied js files from "js_build" folder to "public/js/x.x.x/*" folder
* for release deployment (cache busting)
*/
gulp.task('copyJSBuildFiles', ['removeDistFiles'], function () {
'use strict';
// raw files
var source = _src.JS_SRC;
if(isProductionBuild){
// build files
source = _src.JS_BUILD + '/**/*';
}
return gulp
.src( source )
.pipe(
gulpif(
tagVersion !== null,
gulp.dest( _src.JS_DIST + '/' + tagVersion )
)
).pipe(notify({
icon: path.resolve(__dirname, _src.ICON),
title: 'Copy JS to dist',
message: 'Task complete',
onLast: true
}));
});
/**
* task removes temp build js files
*/
gulp.task('removeBuildFiles', function () {
'use strict';
return gulp.src( _src.JS_BUILD ).pipe( clean( _src.JS_BUILD ) );
});
/**
* task removes "dist" js files
*/
gulp.task('removeDistFiles', function () {
'use strict';
var dist = _src.JS_DIST + '/' + tagVersion;
return gulp.src(dist).pipe( clean(dist) );
});
/**
* create *.gz version from minimized *.css
*/
gulp.task('gzipCSS', function() {
'use strict';
return gulp.src(_src.CSS_SRC)
.pipe(gzip({
gzipOptions: { level: 8 }
}))
.pipe(gulp.dest(_src.CSS_DIST));
});
/**
* create *.gz version from minimized *.js
*/
gulp.task('gzipJS', function() {
'use strict';
return gulp.src(_src.JS_DIST + '/' + tagVersion + '/**/*.js')
.pipe(gzip({
gzipOptions: { level: 8 }
}))
.pipe(gulp.dest(_src.JS_DIST + '/' + tagVersion));
});
/*******************************************/
// Watch
// execute only during continuous development!
gulp.task('watchJSFiles', function(tag) {
'use strict';
if(tag){
tagVersion = tag;
}
gulp.watch([
_src.JS_SRC,
'!' + _src.JS_LIBS,
], ['jshint', 'copyJSBuildFiles']);
});
gulp.task('watchCSSFiles', function(tag) {
'use strict';
if(tag){
tagVersion = tag;
}
gulp.watch([
_src.CSS_SRC,
], ['gzipCSS']);
});
/*******************************************/
// Default Tasks
/**
* Production task for deployment.
* $ gulp production --tag v0.0.9
* WARNING: DO NOT REMOVE THIS TASK!!!
*/
gulp.task('production', function(tag) {
'use strict';
if(tag !== null){
tagVersion = tag;
isProductionBuild = true;
// use run-sequence until gulp v4.0 is released
runSequence(
'gzipCSS',
'requirejs'
);
}
});
/**
* Default task for continuous development.
* $ gulp default --tag v0.0.9
* WARNING: DO NOT REMOVE THIS TASK!!!
*/
gulp.task('default', function(tag) {
'use strict';
if(tag){
tagVersion = tag;
}
runSequence(
'gzipCSS',
'jshint',
'copyJSBuildFiles',
'watchJSFiles',
'watchCSSFiles'
);
});
/*
// This removes all CSS styles "above the fold" from *.css and inlines them
// -> to improve pagespeed. The remaining (main) css file will be lazy loaded afterwards...
// https://github.com/addyosmani/critical
gulp.task('critical', function (cb) {
critical.generate({
inline: true,
base: './',
src: './public/templates/view/index.html',
dest: './public/templates/view/index-critical.html',
extract: true,
minify: true,
width: 2560,
height: 1440
});
});
*/
/**
* clear all backend (fat free framework) cache files
*/
gulp.task('clearCache', function() {
'use strict';
return gulp.src( _src.CACHE ).pipe( clean() );
});