This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
122 lines (111 loc) · 3.39 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
const Promise = require('bluebird');
const child_process = require('child_process');
const fs = require('fs');
const glob = require('glob');
const gulp = require('gulp');
const babel = require('gulp-babel');
const changed = require('gulp-changed');
const minifyCSS = require('gulp-csso');
const gzip = require('gulp-gzip');
const gulpif = require('gulp-if');
const less = require('gulp-less');
const uglify = require('gulp-uglify');
const lazypipe = require('lazypipe');
const merge = require('merge-stream');
const path = require('path');
const pump = require('pump');
const rollup = require('rollup-stream');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
Promise.promisifyAll(child_process);
Promise.promisifyAll(fs);
const isProd = process.env.NODE_ENV === 'production';
gulp.task('bucklescript', (cb) => {
child_process.exec('./node_modules/.bin/bsb -make-world', (err, stdout) => {
if (err != null) {
return cb(new Error(stdout.toString()));
}
cb();
});
});
const minifyJS = lazypipe()
.pipe(buffer)
.pipe(babel)
.pipe(uglify);
gulp.task('client', [ 'bucklescript' ], (cb) => {
const clientFiles = merge(glob.sync('client/*.js').map(input => {
return rollup({
input,
format: 'iife',
plugins: [
resolve(),
commonjs(),
],
})
.pipe(source(path.resolve(input), path.resolve('client')));
}));
pump([
clientFiles,
// TODO see if we can used changed() here
gulpif(isProd, minifyJS()),
gulp.dest('dist'),
gzip(),
gulp.dest('dist'),
], cb);
});
gulp.task('css', (cb) => {
pump([
gulp.src('client/less/*.less'),
changed('dist', { extension: '.css' }),
less(),
gulpif(isProd, minifyCSS()),
gulp.dest('dist'),
gzip(),
gulp.dest('dist'),
], cb);
});
gulp.task('public', (cb) => {
pump([
gulp.src('public/*'),
changed('dist', { transformPath: p => p + '.gz' }),
gzip(),
gulp.dest('dist'),
], cb);
});
gulp.task('server', (cb) => {
pump([
gulp.src('server/*.js'),
changed('bin'),
babel(),
gulp.dest('bin'),
], cb);
});
gulp.task('default', [ 'client', 'css', 'public', 'server' ]);
gulp.task('dist', [ 'default' ], (cb) => {
const version = require('./package.json').version;
const filename = 'bcc32.com-v' + version + '.tar.gz';
const basename = path.basename(__dirname) + '-v' + version;
fs.symlinkAsync('.', basename)
.then(() => child_process.execAsync('git ls-files -z'))
.then(stdout => {
const files = stdout.split('\0').filter(x => x !== '');
files.push('bin');
files.push('dist');
return files;
})
.then(files => {
return ['-czf', filename].concat(files.map(x => path.join(basename, x)));
})
.then(args => child_process.execFileAsync('tar', args))
.finally(() => fs.unlinkAsync(basename))
.asCallback(cb);
});
gulp.task('watch', [ 'default' ], () => {
gulp.watch('client/bs/**/*', [ 'client' ]);
gulp.watch('client/*.js', [ 'client' ]);
gulp.watch('client/*.less', [ 'css' ]);
gulp.watch('public/*', [ 'public' ]);
gulp.watch('server/*.js', [ 'server' ]);
});