This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
178 lines (162 loc) · 5.47 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
/* globals require, console, __dirname */
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({
replaceString: /^gulp(-|\.)([0-9]+)?/
});
var jest = require('gulp-jest');
const fs = require('fs');
const del = require('del');
const path = require('path');
const isparta = require('isparta');
const esperanto = require('esperanto');
const browserify = require('browserify');
const runSequence = require('run-sequence');
const source = require('vinyl-source-stream');
const connect = require('gulp-connect');
require('harmonize')();
// Adjust this file to configure the build
const config = require('./config');
// Remove the built files
gulp.task('clean', function(cb) {
del([config.destinationFolder], cb);
});
// Remove our temporary files
gulp.task('clean:tmp', function(cb) {
del(['tmp'], cb);
});
// Send a notification when JSHint fails,
// so that you know your changes didn't build
function ding(file) {
return file.jshint.success ? false : 'JSHint failed';
}
// Lint our source code
gulp.task('lint:src', function() {
return gulp.src(['src/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(ding))
.pipe($.jshint.reporter('fail'));
});
// Lint our test code
gulp.task('lint:test', function() {
return gulp.src(['test/unit/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(ding))
.pipe($.jshint.reporter('fail'));
});
// Build two versions of the library
gulp.task('build', ['lint:src', 'clean'], function(done) {
esperanto.bundle({
base: 'src',
entry: config.entryFileName,
}).then(function(bundle) {
var res = bundle.toUmd({
sourceMap: true,
sourceMapSource: config.entryFileName + '.js',
sourceMapFile: config.exportFileName + '.js',
name: config.exportVarName
});
// Write the generated sourcemap
fs.mkdirSync(config.destinationFolder);
fs.writeFileSync(path.join(config.destinationFolder, config.exportFileName + '.js'), res.map.toString());
$.file(config.exportFileName + '.js', res.code, { src: true })
.pipe($.plumber())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.babel({ blacklist: ['useStrict'] }))
.pipe($.sourcemaps.write('./', {addComment: false}))
.pipe(gulp.dest(config.destinationFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(config.exportFileName + '.min.js'))
.pipe($.uglifyjs({
outSourceMap: true,
inSourceMap: config.destinationFolder + '/' + config.exportFileName + '.js.map',
}))
.pipe(gulp.dest(config.destinationFolder))
.on('end', done);
})
.catch(done);
});
// Use babel to build the library to CommonJS modules. This
// is fed to Browserify, which builds the version of the lib
// for our browser spec runner.
gulp.task('compile_browser_script', function() {
return gulp.src(['src/**/*.js'])
.pipe($.plumber())
.pipe($.babel({modules: 'common'}))
.pipe(gulp.dest('tmp'))
.pipe($.filter([config.entryFileName + '.js']))
.pipe($.rename('__entry.js'))
.pipe(gulp.dest('tmp'));
});
// Bundle our app for our unit tests
gulp.task('browserify', ['compile_browser_script'], function() {
var bundleStream = browserify(['./test/setup/browserify.js'], {debug: true}).bundle();
return bundleStream
.on('error', function(err){
console.log(err.message);
this.emit('end');
})
.pipe($.plumber())
.pipe(source('./tmp/__spec-build.js'))
.pipe(gulp.dest(''))
.pipe($.livereload());
});
function test() {
// return gulp.src(['test/setup/node.js', 'test/unit/**/*.js'], {read: false})
// .pipe($.plumber())
// .pipe($.mocha({reporter: 'dot', globals: config.mochaGlobals}));
return gulp.src('__tests__').pipe(jest({
'scriptPreprocessor': __dirname + '/node_modules/babel-jest',
'collectCoverage': true,
'collectCoverageOnlyFrom' : {
'./src/react-in-style.js' : true,
},
'testFileExtensions': [
'es6',
'js'
],
'moduleFileExtensions': [
'js',
'json',
'es6'
],
}));
}
// Use `npm coverage` instead, this may or may not work
gulp.task('coverage', function(done) {
gulp.src(['src/*.js'])
.pipe($.plumber())
.pipe($.istanbul({ instrumenter: isparta.Instrumenter }))
.pipe($.istanbul.hookRequire())
.on('finish', function() {
return test()
.pipe($.istanbul.writeReports())
.on('end', done);
});
});
// Lint and run our tests
gulp.task('test', ['lint:src', 'lint:test'], function() {
require('babel/register')({ modules: 'common' });
return test();
});
// Ensure that linting occurs before browserify runs. This prevents
// the build from breaking due to poorly formatted code.
gulp.task('build_in_sequence', function(callback) {
runSequence(['lint:src', 'lint:test'], 'browserify', callback);
});
gulp.task('test-server', function(){
return connect.server({
root: '',
livereload: false
});
});
// Set up a livereload environment for our spec runner
gulp.task('test:browser', ['test-server', 'build_in_sequence'], function() {
$.livereload.listen({port: 35729, host: 'localhost', start: true});
return gulp.watch(['src/**/*.js', 'test/**/*', '.jshintrc', 'test/.jshintrc', 'config/index.json'], ['build_in_sequence']);
});
// An alias of test
gulp.task('default', ['test']);