forked from krux/postscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
158 lines (137 loc) · 4.08 KB
/
gulpfile.babel.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
/* eslint-env node */
import gulp from 'gulp';
import path from 'path';
import del from 'del';
import pkg from './package.json';
import webpackConfig from './webpack.config.babel.js';
import webpack from 'webpack';
import webpackStream from 'webpack-stream';
import WebpackDevServer from 'webpack-dev-server';
import {Server as Karma} from 'karma';
import gutil from 'gulp-util';
import rename from 'gulp-rename';
import uglify from 'gulp-uglify';
import filter from 'gulp-filter';
import stripDebug from 'gulp-strip-debug';
import eslint from 'gulp-eslint';
import jscs from 'gulp-jscs';
import header from 'gulp-header';
import esdoc from 'gulp-esdoc';
const DIST = 'dist';
const BUILD = 'build';
const BANNER = [
'/**',
` * @file ${pkg.name}`,
` * @description ${pkg.description}`,
` * @version v${pkg.version}`,
` * @see {@link ${pkg.homepage}}`,
` * @license ${pkg.license}`,
` * @author ${pkg.author.name}`,
` * @copyright ${new Date().getFullYear()} Krux Digital, Inc`,
' */',
''
].join('\n');
const LINTABLE_PATTERN = [
'*.js',
'src/**/*.js',
'test/random.js',
'test/helpers.js',
'test/generate_expected.phantom.js',
'test/unit/**.js'
];
function build(config) {
const basename = path.basename(config.output.filename, path.extname(config.output.filename));
const jsOnly = filter(['**/*.js'], {restore: true});
const mapOnly = filter(['**/*.map']);
return () => webpackStream(config)
.pipe(jsOnly)
.pipe(header(BANNER))
.pipe(rename({basename: basename, extname: '.max.js'}))
.pipe(gulp.dest(DIST))
.pipe(stripDebug())
.pipe(rename({basename: basename, extname: '.js'}))
.pipe(gulp.dest(DIST))
.pipe(uglify({compress: {properties: false}, output: {'quote_keys': true}}))
.pipe(header(BANNER))
.pipe(rename({basename: basename, extname: '.min.js'}))
.pipe(gulp.dest(DIST))
.pipe(jsOnly.restore)
.pipe(mapOnly)
.pipe(gulp.dest(DIST));
}
function test(configName, failOnError = true, karmaOptions = {}) {
return done => new Karma(Object.assign({
configFile: path.resolve(`test/karma-${configName}.config.babel.js`)
}, karmaOptions), err => {
if (err) {
gutil.log('[test]', 'Tests failed');
if (failOnError) {
done(new gutil.PluginError('karma', 'Tests failed'));
} else {
done();
}
} else {
done();
}
}).start();
}
gulp.task('default', ['lint', 'build', 'doc', 'test']);
gulp.task('clean', () => {
return del.sync([`${DIST}/**`, `${BUILD}/**`]);
});
gulp.task('build', build(webpackConfig));
gulp.task('doc', () => {
return gulp.src('./src')
.pipe(esdoc({
destination: './doc'
}));
});
gulp.task('lint', ['lint:eslint', 'lint:jscs']);
gulp.task('lint:eslint', () => {
return gulp.src(LINTABLE_PATTERN)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('lint:jscs', () => {
return gulp.src(LINTABLE_PATTERN)
.pipe(jscs())
.pipe(jscs.reporter());
});
gulp.task('serve', ['build'], done => {
const devConfig = Object.create(webpackConfig);
devConfig.devtool = 'eval';
devConfig.debug = true;
gutil.log('[serve]', `http://localhost:${pkg.config.ports.cdn}/`);
new WebpackDevServer(webpack(devConfig), {
contentBase: DIST,
hot: true,
quiet: false,
stats: {
colors: true
}
}).listen(pkg.config.ports.cdn, 'localhost', (err, stats) => {
if (err) {
done(new gutil.PluginError('webpack', err));
} else {
if (stats) {
gutil.log('[serve]', stats.toString({
colors: true
}));
}
done();
}
});
});
gulp.task('test', test('coverage'));
gulp.task('test:ci', test('ci'));
gulp.task('test:coverage', test('coverage', false));
gulp.task('test:cross-browser', test('sauce'));
gulp.task('test:debug', test('nocoverage', true, {singleRun: false}));
gulp.task('test:nocoverage', test('nocoverage', false));
gulp.task('tdd', ['test:nocoverage'], () => {
gulp.watch(['src/**', 'test/**'], ['test:nocoverage']);
});
gulp.task('tdd:coverage', ['test:coverage'], () => {
gulp.watch(['src/**', 'test/**'], ['test:coverage']);
});