forked from voithos/graviton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
231 lines (194 loc) · 5.32 KB
/
gulpfile.js
File metadata and controls
231 lines (194 loc) · 5.32 KB
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
var gulp = require('gulp');
var gutil = require('gulp-util');
var jasmine = require('gulp-jasmine');
var eslint = require('gulp-eslint');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var watchify = require('watchify');
var babelify = require('babelify');
var browserSync = require('browser-sync').create();
var del = require('del');
var glob = require('glob');
var path = require('path');
var extend = require('extend');
// Constants and config.
// --------------------
var VIRT_FILE = 'graviton.js';
var VIRT_MIN_FILE = 'graviton.min.js';
var VIRT_TEST_FILE = 'graviton_spec.js';
var ENTRY_FILE = './src/main.js';
var BUILD_PATH = './build';
var SRC_PATTERN = './src/**/*.js';
var VENDOR_PATTERN = './src/vendor/**/*.js';
var TEST_PATTERN = './test/**/*_spec.js';
var WATCHIFY_CONFIG = {
entries: [],
// For source maps.
debug: true,
// `cache` and `packageCache` required by watchify.
cache: {},
packageCache: {},
plugin: [watchify]
};
var BABELIFY_CONFIG = {
presets: ['es2015']
};
var ESLINT_CONFIG = {
extends: 'eslint:recommended',
env: {
browser: true,
es6: true
},
ecmaFeatures: {
modules: true
},
rules: {
'no-var': 1
}
};
// Tasks
// -----
/**
* Prints the error message.
*/
var onError = function(e) {
if (e && e.codeFrame) {
// Babel error.
gutil.log(
gutil.colors.red(e.filename) + ':' +
gutil.colors.cyan(e.loc.line + ',' + e.loc.column) + '\n' +
e.message + '\n' +
e.codeFrame);
} else {
gutil.log(gutil.colors.red(e));
}
};
var bundler;
/**
* Get the currently configured browserify bundler instance. If none exists,
* instantiate and configure it.
* @param {!Array<string>} entries Entry points for browserify
* @param {boolean=} opt_isWatcher Whether watchify should be used
*/
var getBundler = function(entries, opt_isWatcher) {
if (!bundler) {
bundler = browserify(opt_isWatcher ?
extend({}, WATCHIFY_CONFIG, { entries: entries }) :
entries)
.transform(babelify, BABELIFY_CONFIG);
}
return bundler;
};
/**
* Run the currently configured bundler and save the output in the build
* directory.
*/
var build = function() {
return getBundler([ENTRY_FILE])
.bundle()
.on('error', onError)
// We're using native browserify, which doesn't know about gulp,
// so we pipe it to vinyl-source-stream to convert browserify's
// text stream to an efficient vinyl stream usable by gulp.
.pipe(source(VIRT_FILE))
.pipe(buffer())
.pipe(gulp.dest(BUILD_PATH))
.pipe(rename(VIRT_MIN_FILE))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
// Passing a relative path here forces the source maps to be
// written externally.
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(BUILD_PATH));
};
/**
* Setup a browser-sync server and add a change listener to the currently
* configured bundler.
*/
var watch = function() {
browserSync.init({
server: {
baseDir: './'
}
});
var bundler = getBundler([ENTRY_FILE], /* isWatcher */ true);
bundler.on('update', function() {
gulp.start('watch-reload');
});
return build();
};
/**
* Build ES6 tests into a single bundle in the build directory.
*/
var buildTests = function() {
var specs = glob.sync(TEST_PATTERN);
return getBundler(specs)
.bundle()
.on('error', onError)
.pipe(source(VIRT_TEST_FILE))
.pipe(gulp.dest(BUILD_PATH));
};
/**
* Run built tests with jasmine.
*/
var test = function() {
return gulp.src(path.join(BUILD_PATH, VIRT_TEST_FILE))
.pipe(jasmine());
};
/**
* Setup the watch-test bundler.
*/
var watchTestSetup = function() {
var specs = glob.sync(TEST_PATTERN);
var bundler = getBundler(specs, /* isWatcher */ true);
bundler.on('update', function() {
gulp.start('test');
});
};
/**
* Clean the build directory.
*/
var clean = function() {
return del(BUILD_PATH);
};
/**
* Lint the source files.
*/
var lint = function() {
return gulp.src([SRC_PATTERN, '!' + VENDOR_PATTERN])
.pipe(eslint(ESLINT_CONFIG))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
};
/**
* Lint the test files.
*/
var lintTests = function() {
var eslintConfig = extend({}, ESLINT_CONFIG);
eslintConfig.env['jasmine'] = true;
return gulp.src(TEST_PATTERN)
.pipe(eslint(eslintConfig))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
};
// Build
gulp.task('build', build);
gulp.task('watch', watch);
gulp.task('watch-reload', ['build'], function() {
browserSync.reload();
});
// Tests
gulp.task('build-tests', buildTests);
gulp.task('test', ['build-tests'], test);
gulp.task('watch-test-setup', watchTestSetup);
gulp.task('watch-test', ['watch-test-setup', 'test']);
// Misc
gulp.task('clean', clean);
gulp.task('lint', lint);
gulp.task('lint-tests', lintTests);
gulp.task('validate', ['test', 'lint', 'lint-tests']);
gulp.task('default', ['watch']);