-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
460 lines (390 loc) · 11.3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
'use strict';
const gulp = require('gulp');
// --------------------------------------------------------------------
// Plugins
// --------------------------------------------------------------------
const env = process.env.NODE_ENV || 'development';
const path = require('path');
const fs = require('fs');
const es = require('event-stream');
const processEnv = require('gulp-env');
const debug = require('gulp-debug');
const watch = require('gulp-watch');
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');
const changed = require('gulp-changed');
const loopback = require('gulp-loopback-sdk-angular');
// Run sass alongside burbon (fastest way of sass compiling)
const sass = require('gulp-sass');
const neat = require('node-neat').includePaths;
// JS/CSS Injection Related Files
const inject = require('gulp-inject');
const bowerFiles = require('main-bower-files');
const angularSort = require('gulp-angular-filesort');
// Live Reload Enabled
const livereload = require('gulp-livereload');
// --------------------------------------------------------------------
// BUILD PLUGINS
// --------------------------------------------------------------------
const concat = require('gulp-concat');
const del = require('del');
//JS Modules
const uglify = require('gulp-uglify');
const cssmin = require('gulp-cssmin');
const sourcemaps = require('gulp-sourcemaps');
const ngAnnotate = require('gulp-ng-annotate');
const babel = require('gulp-babel');
//HTML Modules
const htmlmin = require('gulp-htmlmin');
const rename = require('gulp-rename');
// --------------------------------------------------------------------
// Error Handler
// --------------------------------------------------------------------
//The title and icon that will be used for the gulp notifications
const onError = function(err) {
console.log(err);
};
// --------------------------------------------------------------------
// Gulp config
// --------------------------------------------------------------------
const sourcePath = __dirname + '/client/src/';
const vendorPath = `${sourcePath}vendor/`;
const bowerOptions = {
paths: {
bowerDirectory: vendorPath,
bowerrc: __dirname + '/.bowerrc',
bowerJson: __dirname + '/bower.json'
},
overrides: {
'angular-ui-router-anim-in-out': {
main: 'anim-in-out.js'
},
'clappr': {
main: 'dist/clappr.min.js'
},
'raven-js': {
main: [
'dist/raven.js',
'dist/plugins/angular.js'
]
},
'trackpad-scroll-emulator': {
main: [
'css/trackpad-scroll-emulator.css',
'jquery.trackpad-scroll-emulator.min.js'
]
}
}
};
const config = {
inject: {
target: sourcePath + 'index.html',
sources: {
app: {
css: [
sourcePath + 'css/**/*.css',
sourcePath + 'app/**/*.css'
],
js: [
sourcePath + 'app/**/*.js'
]
}
}
},
sass: {
target: sourcePath + 'css',
partials: sourcePath + 'css/scss/partials/*.scss',
source: [
sourcePath + 'css/scss/*.scss',
sourcePath + 'css/*.scss',
sourcePath + 'css/scss/partials/*.scss',
sourcePath + 'app/**/*.scss',
sourcePath + 'app/**/**/*.scss',
sourcePath + 'app/**/**/**/*.scss'
]
}
};
// --------------------------------------------------------------------
// BUILD Options
// --------------------------------------------------------------------
const destPath = __dirname + '/client/dist/';
const destIndex = destPath + 'index.html';
const buildDate = (new Date()).getTime();
let buildId = parseInt(buildDate/10000);
buildId = '' + buildId;
buildId = buildId.substring(2);
buildId = env.substring(0, 4) + '-' + buildId;
const build = {
css: [
sourcePath + 'css/**/*.css',
sourcePath + 'app/**/*.css'
],
js: [
sourcePath + 'app/**/*.js'
],
html: [
sourcePath + 'app/**/*.html'
],
images: [
sourcePath + 'images/**/*.*'
],
assets: [
sourcePath + 'fonts/**/*.*'
]
};
const dest = {
imagesPath: destPath + 'images/',
assetsPath: destPath + 'fonts/',
cssPath: destPath + 'css/',
cssVendorFile: 'vendor.min.css',
cssFile: 'app.min.css',
jsPath: destPath + 'app/',
jsVendorFile: 'vendor.min.js',
jsFile: 'app.min.js',
htmlPath: destPath + 'app',
mapPath: destPath + 'maps/'
};
const htmlOpts = {
collapseWhitespace: true
};
const jsOpts = {
mangle: false,
preserveComments: 'all'
};
const cacheOpts = {
type: 'timestamp'
};
let buildJSOptions = JSON.parse(JSON.stringify(bowerOptions));
let buildCSSOptions = JSON.parse(JSON.stringify(bowerOptions));
buildJSOptions.filter = /\.js$/i;
buildCSSOptions.filter = /\.css$/i;
let bowerJS = bowerFiles(buildJSOptions);
let bowerCSS = bowerFiles(buildCSSOptions);
// --------------------------------------------------------------------
// BUILD Tasks
// --------------------------------------------------------------------
gulp.task('build', function() {
//if (env === 'development')
//return true;
return gulp.start('build:inject')
.on('end', () => {
setTimeout(() => {
process.exit();
}, 2000);
});
});
gulp.task('build:inject', ['build:clean', 'move:index', 'build:js', 'build:css'], function(){
return gulp.src(destIndex)
.pipe(inject(
gulp.src([dest.cssPath + 'app.min.*', dest.jsPath + 'app.min.*'], {read: false}),
{relative: true}
))
.pipe(inject(
gulp.src([dest.cssPath + 'vendor.min.*', dest.jsPath + 'vendor.min.*'], {read: false}),
{name: 'bower', relative: true}
))
.pipe(gulp.dest(destPath));
});
gulp.task('build:clean', function() {
return del([
dest.cssPath + '*.css',
dest.jsPath + '*.js',
dest.mapPath + '*.map'
]);
});
gulp.task('build:css', ['build:css:vendor', 'build:css:app']);
gulp.task('build:css:vendor', function(){
return gulp.src(bowerCSS)
.pipe(plumber(onError))
.pipe(concat(dest.cssVendorFile))
.pipe(cssmin())
.pipe(rename(function (path) {
path.basename += '.' + buildId;
}))
.pipe(gulp.dest(dest.cssPath));
});
gulp.task('build:css:app', function() {
return gulp.src(config.sass.source)
.pipe(plumber(onError))
.pipe(sass(
{ includePaths: ['styles'].concat(neat) }
))
.pipe(concat(dest.cssFile))
.pipe(cssmin())
.pipe(rename(function (path) {
path.basename += '.' + buildId;
}))
.pipe(gulp.dest(dest.cssPath));
});
gulp.task('build:js', ['build:js:vendor', 'build:js:app']);
gulp.task('build:js:vendor', function() {
var vendorJs = bowerJS.filter(function(file) {
return file.indexOf('.js') > -1;
});
return gulp.src(vendorJs)
.pipe(plumber(onError))
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(concat(dest.jsVendorFile))
.pipe(rename(function (path) {
path.basename += '.' + buildId;
}))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest(dest.jsPath));
});
gulp.task('build:js:app', function(){
return gulp.src(build.js)
.pipe(plumber(onError))
.pipe(sourcemaps.init())
.pipe(babel({presets: ['es2015']}))
.pipe(angularSort())
.pipe(ngAnnotate())
//.pipe(uglify(jsOpts))
.pipe(concat(dest.jsFile))
.pipe(rename(function (path) {
path.basename += '.' + buildId;
}))
.pipe(sourcemaps.write('../maps'))
.pipe(gulp.dest(dest.jsPath));
});
gulp.task('build:js:lbservices', function() {
const envs = processEnv.set({
DEEPSTREAM_OFF: 'true'
});
return gulp.src('./server/server.js')
.pipe(envs)
.pipe(loopback())
.pipe(envs.reset)
.pipe(rename('lb-services.js'))
.pipe(gulp.dest('./client/src/app/core'));
});
gulp.task('lbservices', ['build:js:lbservices'], function(cb) {
setTimeout(() => {
cb();
process.exit();
}, 2000);
});
gulp.task('move:index', ['move:angular', 'move:images', 'move:fonts'], function(){
return gulp.src(config.inject.target)
.pipe(gulp.dest(destPath));
});
gulp.task('move:images', function(){
return gulp.src(build.images)
.pipe(gulp.dest(dest.imagesPath));
});
gulp.task('move:fonts', function() {
return gulp.src(build.assets)
.pipe(gulp.dest(dest.assetsPath));
});
gulp.task('move:angular', function(){
return gulp.src(build.html)
.pipe(htmlmin(htmlOpts))
.pipe(gulp.dest(dest.htmlPath));
});
// --------------------------------------------------------------------
// Development Tasks
// --------------------------------------------------------------------
//Default gulp task for dev purposes
gulp.task('default', ['watch', 'inject', 'sass']);
//Injects all css/js files into our index.html src file
gulp.task('inject', function () {
return gulp.src(config.inject.target)
.pipe(plumber(onError))
.pipe(inject(
gulp.src(bowerFiles(bowerOptions), {read: false}),
{name: 'bower', relative: true}
))
.pipe(inject(
es.merge(
gulp.src(config.inject.sources.app.css),
gulp.src(config.inject.sources.app.js)
.pipe(babel({presets: ['es2015']}))
.pipe(angularSort())
),
{relative: true}
))
.pipe(gulp.dest(sourcePath));
});
//Builds our sass with burbon/neat
gulp.task('sass', function() {
return gulp.src(config.sass.source)
.pipe(plumber(onError))
.pipe(sass(
{ includePaths: ['styles'].concat(neat) }
))
.pipe(gulp.dest(config.sass.target))
.on('error', function (err) {
// Allows sass to continue watch tasks even after an error
this.emit('end');
});
});
//Watches for changes in files that should be streamed/compiled to browser
gulp.task('watch', function() {
livereload.listen();
//We only want to inject files when files are added/deleted, not changed.
var options = {events: ['add', 'unlink']};
var injectFn = function() {
return gulp.start('inject');
};
watch(
config.sass.source,
function(){
return gulp.start('sass');
}
);
//Watch for changes in app related JS files and live reload them
watch(
build.js,
function(){
return gulp.start('reload-js');
}
);
// Watch for changes in html files and reload them
watch(
build.html,
function(){
return gulp.start('reload-html');
}
);
// Watch for changes in html files and reload them
watch(
build.css,
function(){
return gulp.start('reload-css');
}
);
//Watch for changes in app related files and inject new ones
watch(
config.inject.sources.app.js.concat(config.inject.sources.app.css),
options,
injectFn
);
//Watch for changes in bower related files and inject new ones
watch(
'bower.json',
injectFn
);
});
gulp.task('i', ['inject']);
gulp.task('reload-html', function(){
return gulp.src(build.html)
.pipe(plumber(onError))
.pipe(changed(sourcePath + 'reload/'))
.pipe(gulp.dest(sourcePath + 'reload/'))
.pipe(livereload());
});
gulp.task('reload-js', function(){
return gulp.src(build.js)
.pipe(babel({presets: ['es2015']}))
.pipe(plumber(onError))
.pipe(changed(sourcePath + 'reload/'))
.pipe(gulp.dest(sourcePath + 'reload/'))
.pipe(livereload());
});
gulp.task('reload-css', function(){
return gulp.src(build.css)
.pipe(plumber(onError))
.pipe(changed(sourcePath + 'reload/', {hasChanged: changed.compareSha1Digest}))
.pipe(gulp.dest(sourcePath + 'reload/'))
.pipe(livereload());
});