-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.babel.js
258 lines (215 loc) · 7.71 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
import del from 'del';
import glob from 'glob';
import path from 'path';
import isparta from 'isparta';
import babelify from 'babelify';
import watchify from 'watchify';
import buffer from 'vinyl-buffer';
import esperanto from 'esperanto';
import browserify from 'browserify';
import runSequence from 'run-sequence';
import source from 'vinyl-source-stream';
import fs from 'fs';
import moment from 'moment';
import docco from 'docco';
import {spawn} from 'child_process';
import manifest from './package.json';
// Load all of our Gulp plugins
const $ = loadPlugins();
// Gather the library data from `package.json`
const config = manifest.babelBoilerplateOptions;
const mainFile = manifest.main;
const destinationFolder = path.dirname(mainFile);
const exportFileName = path.basename(mainFile, path.extname(mainFile));
// Remove a directory
function _clean(dir, done) {
del([dir], done);
}
function cleanDist(done) {
_clean(destinationFolder, done)
}
function cleanTmp() {
_clean('tmp', done)
}
// Send a notification when JSCS fails,
// so that you know your changes didn't build
function _jscsNotify(file) {
if (!file.jscs) { return; }
return file.jscs.success ? false : 'JSCS failed';
}
// Lint a set of files
function lint(files) {
return gulp.src(files)
.pipe($.plumber())
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError())
.pipe($.jscs())
.pipe($.notify(_jscsNotify));
}
function lintSrc() {
return lint('src/**/*.js');
}
function lintTest() {
return lint('test/**/*.js');
}
function build(done) {
esperanto.bundle({
base: 'src',
entry: config.entryFileName,
}).then(bundle => {
const res = bundle.toUmd({
// Don't worry about the fact that the source map is inlined at this step.
// `gulp-sourcemaps`, which comes next, will externalize them.
sourceMap: 'inline',
name: config.mainVarName
});
const head = fs.readFileSync('src/header.js', 'utf8');
$.file(exportFileName + '.js', res.code, { src: true })
.pipe($.plumber())
.pipe($.replace('@@version', manifest.version))
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.babel())
.pipe($.header(head, {pkg: manifest, now: moment()}))
.pipe($.replace('global.$', 'global.jQuery')) // Babel bases itself on the variable name we use. Use jQuery for noconflict users.
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destinationFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(exportFileName + '.min.js'))
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.uglify({preserveComments: 'license'}))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destinationFolder))
.on('end', done);
})
.catch(done);
}
function _runBrowserifyBundle(bundler, dest) {
return bundler.bundle()
.on('error', err => {
console.log(err.message);
this.emit('end');
})
.pipe($.plumber())
.pipe(source(dest || './tmp/__spec-build.js'))
.pipe(buffer())
.pipe(gulp.dest(''))
.pipe($.livereload());
}
function browserifyBundler() {
// Our browserify bundle is made up of our unit tests, which
// should individually load up pieces of our application.
// We also include the browserify setup file.
const testFiles = glob.sync('./test/unit/**/*.js');
const allFiles = ['./test/setup/browserify.js'].concat(testFiles);
// Create our bundler, passing in the arguments required for watchify
watchify.args.debug = true;
const bundler = browserify(allFiles, watchify.args);
// Set up Babelify so that ES6 works in the tests
bundler.transform(babelify.configure({
sourceMapRelative: __dirname + '/src'
}));
return bundler;
}
// Build the unit test suite for running tests
// in the browser
function _browserifyBundle() {
let bundler = browserifyBundler();
// Watch the bundler, and re-bundle it whenever files change
bundler = watchify(bundler);
bundler.on('update', () => _runBrowserifyBundle(bundler));
return _runBrowserifyBundle(bundler);
}
function buildDocTest() {
return _runBrowserifyBundle(browserifyBundler(), './doc/assets/spec-build.js');
}
function _mocha() {
return gulp.src(['test/setup/node.js', 'test/unit/**/*.js'], {read: false})
.pipe($.mocha({reporter: 'dot', globals: config.mochaGlobals}));
}
function _registerBabel() {
require('babel-core/register');
}
function test() {
_registerBabel();
return _mocha();
}
function coverage(done) {
_registerBabel();
gulp.src([exportFileName + '.js'])
.pipe($.istanbul({ instrumenter: isparta.Instrumenter }))
.pipe($.istanbul.hookRequire())
.on('finish', () => {
return test()
.pipe($.istanbul.writeReports())
.on('end', done);
});
}
// These are JS files that should be watched by Gulp. When running tests in the browser,
// watchify is used instead, so these aren't included.
const jsWatchFiles = ['src/**/*', 'test/**/*'];
// These are files other than JS files which are to be watched. They are always watched.
const otherWatchFiles = ['package.json', '**/.eslintrc', '.jscsrc'];
// Run the headless unit tests as you make changes.
function watch() {
const watchFiles = jsWatchFiles.concat(otherWatchFiles);
gulp.watch(watchFiles, ['test']);
}
function testBrowser() {
// Ensure that linting occurs before browserify runs. This prevents
// the build from breaking due to poorly formatted code.
runSequence(['lint-src', 'lint-test'], () => {
_browserifyBundle();
$.livereload.listen({port: 35729, host: 'localhost', start: true});
gulp.watch(otherWatchFiles, ['lint-src', 'lint-test']);
});
}
function gitClean() {
$.git.status({args : '--porcelain'}, (err, stdout) => {
if (err) throw err;
if (/^ ?M/.test(stdout)) throw 'You have uncommitted changes!'
});
}
function npmPublish(done) {
spawn('npm', ['publish'], { stdio: 'inherit' }).on('close', done);
}
function gitPush() {
$.git.push('origin', 'master', {args: '--follow-tags'}, err => { if (err) throw err });
}
function gitPushPages() {
$.git.push('origin', 'master:gh-pages', err => { if (err) throw err });
}
function gitTag() {
$.git.tag(manifest.version, {quiet: false}, err => { if (err) throw err });
}
gulp.task('release-git-clean', gitClean);
gulp.task('release-npm-publish', npmPublish);
gulp.task('release-git-push', gitPush);
gulp.task('release-git-push-pages', gitPushPages);
gulp.task('release-git-tag', gitTag);
gulp.task('release', () => {
runSequence('release-git-clean', 'release-git-tag', 'release-git-push', 'release-git-push-pages', 'release-npm-publish');
});
// Remove the built files
gulp.task('clean', cleanDist);
// Remove our temporary files
gulp.task('clean-tmp', cleanTmp);
// Lint our source code
gulp.task('lint-src', lintSrc);
// Lint our test code
gulp.task('lint-test', lintTest);
// Build two versions of the library
gulp.task('build-src', ['lint-src', 'clean'], build);
gulp.task('build', ['build-src']);
// Lint and run our tests
gulp.task('test', ['lint-src', 'lint-test'], test);
// Set up coverage and run tests
gulp.task('coverage', ['lint-src', 'lint-test'], coverage);
// Set up a livereload environment for our spec runner `test/runner.html`
gulp.task('test-browser', testBrowser);
// Run the headless unit tests as you make changes.
gulp.task('watch', watch);
// An alias of test
gulp.task('default', ['test']);