-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgulpfile.js
398 lines (344 loc) · 12.1 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
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
$.ngAnnotate = require('gulp-ng-annotate');
var del = require('del');
var log = require('fancy-log');
var fs = require('graceful-fs');
var packageJSON = require('./package.json');
var mkdirp = require('mkdirp');
var randomString = require('random-string');
var eslint = require('gulp-eslint');
var format = require('string-format');
format.extend(String.prototype);
var env = {
api: '/api',
auth: '/api/status',
base: '/',
port: 8080,
fake_auth: true
};
var production = false;
// var production = true;
if (process.env.NODE_ENV === 'production') {
production = true;
}
log('Environment', production ? 'Production' : 'Development');
// <paths>
var paths = {
'src': './src',
'ddest': './dist',
js: {},
css: {},
html: {}
};
paths.css.deps = './css';
paths.css.dest = paths.ddest + '/css/lib';
paths.js.src = paths.src + '/app/**/*';
paths.js.deps = './js';
paths.js.dest = paths.ddest + '/js/lib';
paths.vendor = paths.ddest + '/libs';
paths.html.src = paths.src + '/index.html';
paths.html.dest = paths.dest + '/index.html';
// </paths>
gulp.task('logs', function() {
var cl = require('conventional-changelog');
var myFile = fs.createWriteStream('CHANGELOG.md');
return cl({
repository: packageJSON.repository,
version: packageJSON.version
}).pipe(myFile);
});
gulp.task('clean', function() {
return del(['.tmp/**', 'dist/**'], {force: true});
});
// This is a function that is used to format the output of the use of the
// gulp-sizediff module. Which, in this gulpfile, is invoked with $.sizediff()
// because of the use of the gulp-load-plugins module, which puts everything
// under $.
var sizeDiffFormat = function(data) {
return ': Before: ' + data.startSize + ', After: ' + data.endSize +
' (' + Math.round(data.diffPercent * 100) + '% of original)' +
'. Total bytes saved: ' + data.diff;
};
// Optimize images
function images() {
return new Promise(function(resolve, reject) {
gulp.src('app/images/**/*')
.pipe($.sizediff.start())
// Removes the viewBox from SVG regardless of parameter passed
// Removed for NeMO (header logo)
/*.pipe($.imagemin({
progressive: true,
interlaced: true
}))*/
.pipe($.sizediff.stop({title: 'images', formatFn: sizeDiffFormat}))
.pipe(gulp.dest('dist/images'))
.on('end', resolve)
.on('error', reject);
});
}
function fonts() {
return new Promise(function(resolve, reject) {
gulp.src(['app/fonts/**'])
.pipe($.size({title: 'fonts'}))
.pipe(gulp.dest('dist/fonts'))
.on('end', resolve)
.on('error', reject);
});
}
// Copy external non-yarn managed libraries to dist
function vendor() {
return new Promise(function(resolve, reject) {
gulp.src(['app/vendor/**'])
.pipe($.size({title: 'vendor'}))
.pipe(gulp.dest('dist/libs'))
.on('end', resolve)
.on('error', reject);
});
}
// Compile and Automatically Prefix Stylesheets
//gulp.task('styles', function() {
function styles() {
return new Promise(function(resolve, reject) {
var filename = production ? 'styles.min.css' : 'styles.css';
gulp.src('app/styles/app.less')
.pipe($.changed('styles', {extension: '.less'}))
.pipe($.less().on('error', log))
.pipe($.autoprefixer({
cascade: false
})
)
.pipe($.sizediff.start())
// Concatenate And Minify Styles
.pipe($.concat(filename))
.pipe($.if(production, $.csso()))
.pipe($.sizediff.stop({title: 'styles', formatFn: sizeDiffFormat}))
.pipe(gulp.dest('dist/css'))
.on('end', resolve)
.on('error', reject);
});
}
// NOTE: When replacing this task, the replacement will instead take assets from
// js/components, css/components and fonts/components, and do essentially the
// same thing into dist/libs
gulp.task('frontend_js', function() {
var stream = gulp.src(paths.js.deps + '/components/**/*');
return stream
.pipe(gulp.dest(paths.js.dest));
});
gulp.task('frontend_css', function() {
var stream = gulp.src(paths.css.deps + '/components/**/*');
var f = $.filter(
['**', '!**/fonts', '!**/fonts/**'],
{restore: true, passthrough: false}
);
stream
// Filter the non-font files
.pipe(f)
.pipe(gulp.dest('./dist/css/lib'));
// Use the filtered files (which are fonts) as a gulp file source
f.restore
.pipe(gulp.dest('./dist/css'));
return stream;
});
var frontend = gulp.series('frontend_js', 'frontend_css');
gulp.task('frontend', frontend);
// <ng-templates>
gulp.task('ng_templates', function() {
return ng_templates();
});
var ng_templates = function() {
var f = production ? 'templates.min.js' : 'templates.js';
return gulp.src('app/scripts/**/templates/*.html')
.pipe($.htmlmin({
collapseWhitespace: true,
customAttrCollapse: /data-|d/,
removeRedundantAttributes: true,
removeComments: true }
))
.pipe($.ngHtml2js({
moduleName: function(file) {
var path = file.path.split('/');
var folder = path[path.length - 2];
return folder.replace(/-[a-z]/g, function(match) {
return match.substr(1).toUpperCase() + 'templates';
});
}
}))
.pipe($.concat(f))
.pipe($.if(production, $.uglify()))
.pipe(gulp.dest('dist/js'))
.pipe($.size({title: 'ng_templates'}));
};
// </ng-templates>
var html_replacement = function() {
var minified = production ? '.min' : '';
return gulp.src('app/index.html')
.pipe($.replace('__BASE__', env.base))
.pipe($.replace('__MINIFIED__', minified))
.pipe(gulp.dest('dist'))
.pipe($.size({title: 'html'}));
};
var html = gulp.series(html_replacement, 'frontend', 'ng_templates');
gulp.task('html', html);
// Scan Your HTML For Assets & Optimize Them
gulp.task('rev', gulp.series('html', function() {
var stream = gulp.src('dist/index.html');
if (production) {
var manifest = paths.vendor + '/rev-manifest.json';
var vendorFiles = fs.existsSync(manifest) ? require(manifest) : [];
//var assets = $.useref.assets({searchPath: 'dist'});
for (var file in vendorFiles) {
if (vendorFiles.hasOwnProperty(file)) {
stream = stream.pipe($.replace(file, vendorFiles[file]));
}
}
return stream
//.pipe($.rev())
// .pipe($.useref())
//.pipe($.revReplace())
//.pipe(gulp.dest('dist'))
.pipe($.gzip())
.pipe(gulp.dest('dist'));
} else {
return stream
.pipe(gulp.dest('dist'))
.pipe($.size({title: 'rev'}));
}
}));
// <typescript>
var tsProject = $.typescript.createProject('app/scripts/tsconfig.json');
var ts_compile = function () {
var f = production ? 'app.min.js' : 'app.js';
var api_base = production ? '/api' : 'http://localhost:5000';
var tsResult = gulp.src('app/**/*.ts')
.pipe(tsProject());
tsResult.dts.pipe(gulp.dest('dist/dts'));
tsResult.js.pipe(gulp.dest('.tmp'));
return tsResult.js
.pipe($.if(! production, $.sourcemaps.init()))
.pipe($.concat(f))
.pipe($.replace('__API_BASE__', api_base))
.pipe($.ngAnnotate())
.pipe($.if(production, $.uglify()))
.pipe($.wrap({src: './iife.txt'}))
.pipe($.if(! production, $.sourcemaps.write()))
.pipe(gulp.dest('dist/js'))
.pipe($.size({title: 'typescript'}));
};
gulp.task('ts_compile', function() {
return ts_compile();
});
// </typescript>
gulp.task('test', gulp.series('clean', 'ts_compile', 'ng_templates'), function(done) {
console.log('TO BE IMPLEMENTED');
done();
});
gulp.task('server', function() {
var express = require('express');
var path = require('path');
var app = express();
var port = env.port;
var localpath = 'dist';
// Watch for changes, and relaunch gulp tasks as necesary...
if (! production) {
devel_watcher();
}
console.log('Configured Access URLs:');
console.log('-------------------------------------');
console.log('UI: http://localhost:{}{}'.format(port, env.base));
console.log('-------------------------------------');
console.log('API {}:'.format(env.api));
console.log('-------------------------------------');
console.log('Serving files at {} from dir: {}\n\n'.format(env.base, localpath));
app.use('/', express.static(path.join(__dirname, localpath)));
// Needed for browser refresh to work
app.get('*', function(request, response, next) {
response.sendfile(__dirname + '/dist/index.html');
});
app.listen(port);
});
function devel_watcher() {
gulp.watch(['app/**/*.html'], function(event) {
console.log('Detected an HTML change. ' +
'Re-running the "html" task and reloading.');
html();
});
gulp.watch(['app/**/*.{less,css}'], function(event) {
console.log('Detected a less or css change. ' +
'Re-running the "styles" task and reloading.');
styles();
});
gulp.watch(['app/scripts/**/*.ts'], function(event) {
console.log('Detected a typescript change. ' +
'Re-running the "ts_compile" task and reloading.');
ts_compile();
});
gulp.watch(['app/scripts/**/*.html'], function(event) {
console.log('Detected an angular template change. ' +
'Re-running the "ng_templates" task and reloading.');
ng_templates();
});
gulp.watch(['app/images/**/*'], function(event) {
console.log('Detected an images change. ' +
'Re-running the "images" task and reloading.');
images();
});
}
function pegjs() {
var PEG = require('pegjs');
var input = fs.readFileSync('gql.pegjs', {encoding: 'utf8'});
var parser = PEG.generate(input, {output: 'source'});
return new Promise(function(resolve, reject) {
var out = production ? 'gql.min.js' : 'gql.js';
fs.writeFileSync(out, 'window.gql = ' + parser);
gulp.src(out)
// Minify the gql javascript file if we're in 'production'. Also,
// if we DO minify, then produce the stats of how much savings
// we got. Therefore, we have 'if' conditions around the sizediff
// invocations.
.pipe($.if(production, $.sizediff.start()))
.pipe($.if(production, $.uglify()))
.pipe($.if(production, $.sizediff.stop({title: 'pegjs', formatFn: sizeDiffFormat})))
.pipe(gulp.dest('dist/js/'))
.on('end', resolve)
.on('error', reject);
});
}
// Run the eslint tool on javascript assets (that are not 3rd party or
// already minified) and report results.
gulp.task('eslint', function() {
var stream = gulp.src([
'**/*.js',
'!dist/**',
'!**/*.min.js',
'!js/components/**',
'!node_modules/**'
]);
return stream
.pipe(eslint())
.pipe(eslint.result(function(result) {
// Called for each ESLint result.
if (result.messages.length !== 0) {
console.log('ESLint result: ' + result.filePath);
console.log('# Messages: ' + result.messages.length);
console.log('# Warnings: ' + result.warningCount);
console.log('# Errors: ' + result.errorCount);
}
}));
});
gulp.task('build', gulp.series('clean', styles,
gulp.series(
gulp.parallel(
'rev', fonts, vendor,
pegjs, 'ts_compile', images
),
function parallelDone (done) {
done();
}
),
function allDone (done) {
done();
}
));
gulp.task('default', gulp.series('build'));