forked from nodecg/nodecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
95 lines (85 loc) · 2.11 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
'use strict';
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const del = require('del');
const exorcist = require('exorcist');
const gulp = require('gulp');
const gutil = require('gulp-util');
const source = require('vinyl-source-stream');
const transform = require('vinyl-transform');
const buildDirectory = 'build';
/**
* Waits for the given ReadableStream
*/
function waitFor(stream) {
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
}
function clean() {
return del([
buildDirectory,
'.nyc_output',
'coverage',
'instrumented'
]);
}
function browserApi() {
return Promise.all([
waitFor(buh()),
waitFor(buh({instrument: true}))
]);
}
function buh({instrument} = {}) {
// Set up the browserify instance on a task basis
const b = browserify({
entries: './lib/api.js',
debug: true,
// Defining transforms here will avoid crashing your stream
transform: [
['babelify', {
global: true,
presets: ['@babel/preset-env', ['minify', {builtIns: false}]],
comments: false,
minified: true,
sourceMaps: true,
plugins: instrument ? ['istanbul'] : []
}],
['aliasify', {
aliases: {
'./logger': './lib/browser/logger',
'./replicant': './lib/browser/replicant',
'./config': './lib/browser/config'
},
verbose: false
}]
]
});
// Ignore some files that we don't want in the browser bundle.
[
'express',
'./lib/server/index.js',
'./lib/replicator.js',
'./lib/util/index.js'
].forEach(file => {
b.ignore(file);
});
return b.bundle()
.pipe(source('nodecg-api.min.js'))
.pipe(buffer())
.pipe(transform(() => {
return exorcist(instrument ? 'instrumented/nodecg-api.min.js.map' : 'build/src/nodecg-api.min.js.map');
}))
.on('error', gutil.log)
.pipe(gulp.dest(instrument ? 'instrumented' : 'build/src'));
}
exports.clean = clean;
exports['browser-api'] = gulp.series(
clean,
browserApi
);
exports.default = gulp.series(clean, browserApi);
process.on('unhandledRejection', r => {
console.error('UNHANDLED PROMISE REJECTION:\n', r.stack ? r.stack : r);
});