This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
forked from dollarshaveclub/postmate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
98 lines (87 loc) · 2.7 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
const babel = require('rollup-plugin-babel');
const connect = require('connect');
const eslint = require('gulp-eslint');
const fs = require('fs');
const gulp = require('gulp');
const header = require('gulp-header');
const http = require('http');
const minify = require('uglify-js').minify;
const mochaPhantomJS = require('gulp-mocha-phantomjs');
const path = require('path');
const rollup = require('rollup-stream');
const serveStatic = require('serve-static');
const source = require('vinyl-source-stream');
const uglify = require('rollup-plugin-uglify');
var parentServer; // eslint-disable-line no-var
var childServer; // eslint-disable-line no-var
const pkg = require('./package.json');
const banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @link <%= pkg.homepage %>',
' * @author <%= pkg.author %>',
' * @license <%= pkg.license %> */',
''].join('\n');
gulp.task('do-build', () =>
rollup({
entry: './src/postmate.js',
format: 'umd',
moduleName: 'Postmate',
plugins: [
babel({
exclude: 'node_modules/**',
}),
uglify({}, minify),
],
})
.pipe(source('postmate.min.js'))
.pipe(header(banner, { pkg }))
.pipe(gulp.dest('./build'))
);
gulp.task('update-readme', () => {
const readme = path.join(__dirname, 'README.md');
const data = fs.readFileSync(readme, 'utf-8');
const distSize = fs.statSync(path.join(__dirname, 'build', 'postmate.min.js')).size;
const updated = data.replace(/<span class="size">(.*?)<\/span>/,
`<span class="size">\`${(distSize / 1024).toFixed(1)}kb\`</span>`);
fs.writeFileSync(readme, updated);
});
gulp.task('lint', () =>
gulp.src(['**/*.js', '!node_modules/**', '!build/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
);
gulp.task('parent-test-server', done => {
parentServer = http.createServer(
connect()
.use(serveStatic('.'))
.use(serveStatic('test/fixtures'))
)
.listen(9000, done);
});
gulp.task('child-test-server', done => {
childServer = http.createServer(
connect()
.use(serveStatic('.'))
.use(serveStatic('test/fixtures'))
)
.listen(9001, done);
});
gulp.task('do-test', () => {
const stream = mochaPhantomJS({
phantomjs: {
useColors: true,
},
});
stream.write({ path: 'http://localhost:9001/test/runner.html' });
stream.end();
return stream;
});
gulp.task('test', ['parent-test-server', 'child-test-server', 'do-test'], () => {
parentServer.close();
childServer.close();
});
gulp.task('watch', () => gulp.watch('./src/postmate.js', ['build']));
gulp.task('build', ['do-build', 'update-readme']);
gulp.task('build-watch', ['build', 'watch']);