forked from mozilla/learning.mozilla.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
366 lines (315 loc) · 10 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var path = require('path');
var PassThrough = require('stream').PassThrough;
var _ = require('underscore');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var s3 = require('gulp-s3');
var gzip = require('gulp-gzip');
var less = require('gulp-less');
var cssmin = require('gulp-minify-css');
var webpack = require('gulp-webpack');
var plumber = require('gulp-plumber');
var merge = require('merge-stream');
var sourcemaps = require('gulp-sourcemaps');
var sitemap = require('gulp-sitemap');
var jscs = require('gulp-jscs');
var jshint = require('gulp-jshint');
var autoprefixer = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var IndexFileStream = require('./lib/gulp-index-file-stream');
var imageConverter = require('./lib/gulp-image-converter');
var imageConvertConfig = require('./image-convert.config');
var webpackConfig = require('./webpack.config');
var config = require('./lib/config');
var travis = require('./lib/travis');
var staticServer = require('./test/browser/server');
var lightweightDynamicServer = require('./app');
var indexStaticWatcher = require('./lib/index-static-watcher').create();
var MINIMAL_BUILD_TASKS = [
'copy-static-files',
'less',
'webpack'
];
var BUILD_TASKS = MINIMAL_BUILD_TASKS.concat([
'sitemap'
]);
var LINT_DIRS = [
'*.js',
'lib/**/*.js',
'test/**/*.js',
// Google analytics contains code from GA's snippet, which
// is intentionally uglified and obfuscated and crap.
'!lib/googleanalytics.js',
// TODO let's figure out how to let our linters handle the test suite: delete the line below when we're ready
'!test/**/*.js'
];
var IMG_FILE_TYPES = [
'img/**/*.jpg',
'img/**/*.png',
'img/**/*.svg',
'img/favicon.ico'
];
var LESS_FILES = './less/**/*.less';
var WATCH_DELAY = 200;
function onError(err) {
gutil.log(gutil.colors.red(err));
gutil.beep();
this.emit('end');
}
function handleError() {
return plumber({
errorHandler: onError
});
}
function createIndexFileStream() {
var stream = new PassThrough({ objectMode: true });
var meta = {};
var execSync = require('child_process').execSync;
try {
meta['git-rev'] = execSync('git rev-parse HEAD', {
cwd: __dirname,
encoding: 'utf8'
}).slice(0, 40);
} catch (e) {}
indexStaticWatcher.build(function(err, indexStatic) {
if (err) {
return stream.emit('error', err);
}
new IndexFileStream(indexStatic, {
meta: meta
}).on('error', function(err) {
stream.emit('error', err);
}).pipe(stream);
});
return stream;
}
gulp.task('sitemap', ['generate-index-files'], function() {
gulp.src('dist/**/*.html')
.pipe(sitemap({
siteUrl: config.ORIGIN
}))
.pipe(gulp.dest('./dist'));
});
gulp.task('copy-static-files', [
'copy-test-dirs',
'copy-images',
'copy-bootstrap',
'copy-fontawesome'
]);
gulp.task('copy-test-dirs', function() {
return merge(
gulp.src('test/browser/static/**', {
base: './test/browser/static'
}),
gulp.src('node_modules/mocha/mocha.*', {
base: './node_modules/mocha'
})
).pipe(gulp.dest('./dist/test'));
});
gulp.task('copy-images', function () {
imageConvertConfig.reload();
return gulp.src(IMG_FILE_TYPES, {
base: '.'
}).pipe(imageConverter(imageConvertConfig.patterns))
.on('unconverted-patterns', function(patterns) {
gutil.log(gutil.colors.red.bold(
'No images matched the pattern(s): ' + patterns
));
gutil.log('Please edit image-convert.config.js to fix this.');
})
.pipe(gulp.dest('./dist'));
});
gulp.task('copy-bootstrap', function () {
return gulp.src(['node_modules/bootstrap/dist/css/**', 'node_modules/bootstrap/dist/fonts/**'], {
base: 'node_modules/bootstrap/dist'
}).pipe(gulp.dest('./dist/vendor/bootstrap'));
});
gulp.task('copy-fontawesome', function () {
return gulp.src(['node_modules/font-awesome/css/**', 'node_modules/font-awesome/fonts/**'], {
base: 'node_modules/font-awesome'
}).pipe(gulp.dest('./dist/vendor/font-awesome'));
});
gulp.task('less', function() {
return gulp.src('./less/index.less')
.pipe(handleError())
.pipe(sourcemaps.init())
.pipe(less({
paths: [path.join(__dirname, 'less')],
filename: 'styles.css'
}))
.pipe(gulpif(process.env.LESS_AUTOPREFIXER != 'off', autoprefixer({
browsers: ['last 2 versions'],
cascade: false,
remove: true
})))
.pipe(gulpif(process.env.NODE_ENV === 'production', cssmin()))
.pipe(rename('styles.css'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist'));
});
gulp.task('webpack', function() {
return gulp.src(webpackConfig.entry.app)
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('./dist'));
});
gulp.task('smoketest', BUILD_TASKS.concat([
'test-react-warnings'
]), function() {
gutil.log(gutil.colors.green.bold('Yay, smoke test passes!'));
});
gulp.task('test-react-warnings', function() {
var oldWarn = console.warn;
var warnings = 0;
console.warn = function(message) {
warnings++;
gutil.log(gutil.colors.red.bold(message));
};
return createIndexFileStream()
.on('end', function() {
console.warn = oldWarn;
if (warnings) {
this.emit('error', new Error('At least one warning was logged.'));
}
})
.on('data', function() {
// Drain the stream. We don't actually need to do anything with
// the data, we just want to make sure no warnings are logged while
// the stream's data is being generated.
});
});
gulp.task('generate-index-files', function() {
return createIndexFileStream().pipe(gulp.dest('./dist'));
});
gulp.task('jshint', function() {
return gulp.src(LINT_DIRS)
.pipe(jshint({ lookup: 'node_modules/mofo-style/linters/.jshintrc' }))
.pipe(jshint.reporter('default'));
});
gulp.task('jscs', function () {
// jscs doesn't play nice with *.jsx files so we're avoiding lib/*.jsx
return gulp.src(LINT_DIRS)
.pipe(jscs({ configPath: 'node_modules/mofo-style/linters/.jscsrc' }));
});
gulp.task('lint-test', ['jscs', 'jshint']);
gulp.task('default', BUILD_TASKS);
gulp.task('watch-webpack', function() {
// We're specifically not returning the stream here,
// because if we do that, this task would never end,
// and tasks that depend on this one would never be run.
gulp.src(webpackConfig.entry.app)
.pipe(webpack(_.extend({
watch: true
}, webpackConfig)))
.pipe(gulp.dest('./dist'));
});
gulp.task('watch-static-files', function() {
gulp.watch(IMG_FILE_TYPES.concat([
'image-convert.config.js'
]), ['copy-images']);
gulp.watch('test/browser/static/**', ['copy-test-dirs']);
});
gulp.task('watch-non-reloadable-files', function() {
gulp.watch([
'gulpfile.js',
'package.json',
'webpack.config.js',
'app.js'
], function(event) {
var filename = path.basename(event.path);
var cmd = process.argv[2] === 'app' ? 'npm run app' : 'npm start';
gutil.log(gutil.colors.red.bold(filename + ' was ' + event.type + '.'));
gutil.log(gutil.colors.red.bold('Please restart the server ' +
'with "' + cmd + '".'));
process.exit(0);
});
});
gulp.task('watch', _.without(BUILD_TASKS, 'webpack').concat([
'watch-webpack',
'watch-static-files',
'watch-non-reloadable-files'
]), function() {
require('./lib/developer-help')();
indexStaticWatcher.watch(WATCH_DELAY, function() {
createIndexFileStream()
.on('error', function(err) {
gutil.log('Error rebuilding index HTML files.');
gutil.log(gutil.colors.red.bold(err.stack));
})
.on('end', function() {
gutil.log('Index HTML files rebuilt.');
})
.pipe(gulp.dest('./dist'));
});
gulp.watch(LESS_FILES, ['less']);
staticServer.create().listen(config.DEV_SERVER_PORT, function() {
gutil.log('Development server listening at ' +
gutil.colors.green.bold(config.ORIGIN) + '.');
});
});
gulp.task('app', _.without(MINIMAL_BUILD_TASKS, 'webpack').concat([
'watch-webpack',
'watch-static-files',
'watch-non-reloadable-files'
]), function() {
require('./lib/developer-help')();
indexStaticWatcher.watch(WATCH_DELAY, function(newIndexStatic) {
lightweightDynamicServer.updateIndexStatic(newIndexStatic);
console.log('Rebuilt server-side bundle.');
});
gulp.watch(LESS_FILES, ['less']);
lightweightDynamicServer.listen(config.DEV_SERVER_PORT, function() {
gutil.log('Lightweight dynamic server listening at ' +
gutil.colors.green.bold(config.ORIGIN) + '.');
});
});
gulp.task('travis-after-success', function(cb) {
var env = travis.getS3Env();
if (env === null) {
gutil.log('Current build does not need to be pushed to S3.');
return;
}
require('child_process')
.spawn(process.execPath, [process.argv[1], 's3'], {
env: _.extend({}, process.env, env),
stdio: 'inherit'
}).on('close', function(code) {
if (code !== 0) {
gutil.log(gutil.colors.red.bold('Error deploying to S3!'));
cb(new Error('gulp s3 failed with exit code ' + code));
} else {
gutil.log('Site deployed to S3.');
cb(null);
}
});
});
gulp.task('s3', BUILD_TASKS, function() {
var key = process.env.AWS_ACCESS_KEY;
var secret = process.env.AWS_SECRET_KEY;
gutil.log('NODE_ENV is ' + process.env.NODE_ENV + '.');
if (!key || !secret) {
throw new Error('Please set AWS_ACCESS_KEY and AWS_SECRET_KEY ' +
'in your environment.');
}
// WARNING: Even if deploying to S3 fails, no errors will be raised.
// https://github.com/nkostelnik/gulp-s3/issues/47
return gulp.src('./dist/**')
.pipe(gzip())
.pipe(s3({
key: key,
secret: secret,
bucket: process.env.AWS_BUCKET || 'teach.mofostaging.net',
region: process.env.AWS_REGION || 'us-east-1'
}, {
gzippedOnly: true,
headers: {
'Cache-Control': 'max-age=600, public'
}
}));
});
if (process.env.NODE_ENV === 'production') {
gulp.task('postinstall', MINIMAL_BUILD_TASKS);
} else {
gulp.task('postinstall');
}
module.exports.LESS_FILES = LESS_FILES;