forked from metabrainz/musicbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
450 lines (371 loc) · 11.8 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
const canonicalJSON = require('canonical-json');
const EventEmitter = require('events');
const fs = require('fs');
const gulp = require('gulp');
const less = require('gulp-less');
const rev = require('gulp-rev');
const streamify = require('gulp-streamify');
const _ = require('lodash');
const mergeStream = require('merge-stream');
const path = require('path');
const Q = require('q');
const shellQuote = require('shell-quote');
const shell = require('shelljs');
const File = require('vinyl');
const source = require('vinyl-source-stream');
const yarb = require('yarb');
const poFile = require('../server/gettext/poFile');
const DBDefs = require('./scripts/common/DBDefs');
const POSIX_LANGUAGES = _(DBDefs.MB_LANGUAGES || '')
.split(/\s+/)
.compact()
.without('en')
.map(langToPosix);
{
let langCount = POSIX_LANGUAGES.size();
let extraLangCount = langCount > 0 ? langCount - 1 : 0;
// This may need to be increased when a new bundle is added, to silence
// warnings of the form "Possible EventEmitter memory leak detected."
EventEmitter.defaultMaxListeners = 11 + 9 * extraLangCount;
}
process.env.NODE_ENV = DBDefs.DEVELOPMENT_SERVER ? 'development' : 'production';
const SCRIPT_BUNDLES = {};
const CHECKOUT_DIR = path.resolve(__dirname, '../../');
const PO_DIR = path.resolve(CHECKOUT_DIR, 'po');
const ROOT_DIR = path.resolve(CHECKOUT_DIR, 'root');
const STATIC_DIR = path.resolve(ROOT_DIR, 'static');
const STYLES_DIR = path.resolve(STATIC_DIR, 'styles');
const BUILD_DIR = process.env.MBS_STATIC_BUILD_DIR || path.resolve(STATIC_DIR, 'build');
const SCRIPTS_DIR = path.resolve(STATIC_DIR, 'scripts');
const IMAGES_DIR = path.resolve(STATIC_DIR, 'images');
const revManifestContents = {};
// This file must exist for any task that runs.
const REV_MANIFEST_PATH = path.join(BUILD_DIR, 'rev-manifest.json');
if (!fs.existsSync(REV_MANIFEST_PATH)) {
fs.writeFileSync(REV_MANIFEST_PATH, '{}');
}
const revManifestBundle = runYarb('rev-manifest.js', function (b) {
b.expose(path.join(BUILD_DIR, 'rev-manifest.json'), 'rev-manifest.json');
});
const JED_DATA_PATH = path.join(SCRIPTS_DIR, 'common/i18n/jedData.json');
const jedDataBundle = runYarb('common/i18n/jedData.json', function (b) {
b.expose(JED_DATA_PATH, 'jed-data');
});
function writeResources(stream, writeManifest = true) {
var deferred = Q.defer();
stream
.pipe(streamify(rev()))
.pipe(gulp.dest(BUILD_DIR))
// The rev-manifest path must be absolute for the `merge` option to work.
.pipe(rev.manifest(path.resolve(BUILD_DIR, 'rev-manifest.json'), {
// By default, `base` is the current working directory, so this ensures
// the manifest is saved directly under BUILD_DIR, rather than
// $BUILD_DIR/root/static/build/.
base: BUILD_DIR,
merge: true,
transformer: {
parse: JSON.parse,
stringify: function (contents) {
return canonicalJSON(_.assign(revManifestContents, contents));
},
},
}))
.pipe(gulp.dest(BUILD_DIR))
.on('finish', function () {
if (writeManifest) {
writeResources(bundleScripts(revManifestBundle, 'rev-manifest.js'), false)
.done(deferred.resolve);
} else {
deferred.resolve();
}
});
return deferred.promise;
}
const STYLE_GLOBS = [
'common.less',
'icons.less',
'statistics.less',
];
function buildStyles() {
return gulp.src(
STYLE_GLOBS.map(x => path.resolve(STYLES_DIR, x)),
{base: STATIC_DIR}
).pipe(less({
rootpath: '/static/',
relativeUrls: true,
plugins: [
new (require('less-plugin-clean-css'))({compatibility: 'ie8'})
]
}));
}
function bundleScripts(bundle, name) {
return bundle
.bundle()
.on('error', console.error)
.pipe(source('scripts/' + name));
}
function runYarb(resourceName, vinyl, callback) {
if (!vinyl || typeof vinyl === 'function') {
callback = vinyl;
vinyl = new File({
base: STATIC_DIR,
path: path.resolve(SCRIPTS_DIR, resourceName),
});
vinyl.contents = fs.createReadStream(vinyl.path);
}
var bundle = yarb(vinyl, {
debug: DBDefs.DEVELOPMENT_SERVER,
});
bundle.transform(require('./lib/babelify'));
bundle.transform('envify', {global: true});
bundle.transform('insert-module-globals', {
global: true,
vars: {
L: function (file) {
if (/leaflet\.markercluster/.test(file)) {
return "require('leaflet/dist/leaflet-src')";
}
},
},
});
if (!DBDefs.DEVELOPMENT_SERVER) {
bundle.transform('uglifyify', {
// See https://github.com/substack/node-browserify#btransformtr-opts
global: true,
// Uglify options
output: {
comments: /@preserve|@license/,
},
sourcemap: false
});
}
if (callback) {
callback(bundle);
}
SCRIPT_BUNDLES[resourceName] = bundle;
return bundle;
}
function langToPosix(lang) {
return lang.replace(/^([a-zA-Z]+)-([a-zA-Z]+)$/, function (match, l, c) {
return l + '_' + c.toUpperCase();
});
}
const commonBundle = runYarb('common.js', function (b) {
b.external(revManifestBundle);
b.external(jedDataBundle);
// Map DBDefs.js to DBDefs-client.js on disk. (The actual requires have to
// remain constant for the node renderer.)
b.require(
new File({
path: path.resolve(SCRIPTS_DIR, 'common', 'DBDefs.js'),
contents: fs.readFileSync(
path.resolve(SCRIPTS_DIR, 'common', 'DBDefs-client.js')),
})
);
});
const GETTEXT_DOMAINS = [
'attributes',
'countries',
'instrument_descriptions',
'instruments',
'languages',
'mb_server',
'relationships',
'scripts',
'statistics',
];
_(POSIX_LANGUAGES)
.each(function (lang) {
// We handle the mb_server domain specially by filtering out strings that
// don't appear in any JavaScript file.
var srcPo = shellQuote.quote([poFile.find('mb_server', lang)]);
var tmpPo = shellQuote.quote([path.resolve(PO_DIR, `javascript.${lang}.po`)]);
// msggrep's -N option supports wildcards which use fnmatch internally.
// The '*' cannot match path separators, so we must generate a list of
// possible terminal paths.
let scriptsDir = shellQuote.quote([SCRIPTS_DIR]);
let nestedDirs = shell.exec(`find ${scriptsDir} -type d`, {silent: true}).output.split('\n');
let msgLocations = _(nestedDirs)
.compact()
.map(dir => '-N ' + shellQuote.quote(['..' + dir.replace(CHECKOUT_DIR, '') + '/*.js']))
.join(' ');
// Create a temporary .po file containing only the strings used by root/static/scripts.
shell.exec(`msggrep ${msgLocations} ${srcPo} -o ${tmpPo}`);
GETTEXT_DOMAINS.forEach(function (domain) {
let jedData;
if (domain === 'mb_server') {
jedData = poFile.load('javascript', lang, 'mb_server');
fs.unlinkSync(tmpPo);
} else {
jedData = poFile.load(domain, lang);
jedData.domain = 'mb_server';
}
const langString = JSON.stringify(lang);
const source = `
const jedData = require('jed-data');
const newData = ${JSON.stringify(jedData)};
if (jedData[${langString}]) {
jedData[${langString}].locale_data.${domain} = newData.locale_data.${domain};
} else {
jedData[${langString}] = newData;
}
jedData.locale = ${langString};`;
const bundleName = `jed-${lang}-${domain}.js`;
const langVinyl = new File({
path: path.resolve(SCRIPTS_DIR, bundleName),
contents: new Buffer(source),
});
runYarb(bundleName, langVinyl, function (b) {
b.external(jedDataBundle);
});
});
});
runYarb('account/applications/register.js', function (b) {
b.external(commonBundle);
});
runYarb('account/preferences.js', function (b) {
b.external(commonBundle);
});
runYarb('area/index.js', function (b) {
b.external(commonBundle);
});
runYarb('area/places-map.js', function (b) {
b.external(commonBundle);
});
runYarb('artist/index.js', function (b) {
b.external(commonBundle);
});
runYarb('debug.js', function (b) {
b.external(commonBundle);
});
const editBundle = runYarb('edit.js', function (b) {
b.external(commonBundle);
});
runYarb('edit/notes-received.js', function (b) {
b.external(commonBundle);
});
runYarb('event/index.js', function (b) {
b.external(commonBundle);
});
const guessCaseBundle = runYarb('guess-case.js', function (b) {
b.external(commonBundle);
});
runYarb('instrument/index.js', function (b) {
b.external(commonBundle);
});
runYarb('label/index.js', function (b) {
b.external(commonBundle);
});
runYarb('place/index.js', function (b) {
b.external(commonBundle);
});
const placeMapBundle = runYarb('place/map.js', function (b) {
b.external(commonBundle);
});
runYarb('place.js', function (b) {
b.external(placeMapBundle).external(editBundle).external(guessCaseBundle);
});
runYarb('release-editor.js', function (b) {
b.external(commonBundle).external(editBundle);
});
runYarb('release-group/index.js', function (b) {
b.external(commonBundle);
});
runYarb('series/index.js', function (b) {
b.external(commonBundle);
});
runYarb('series.js', function (b) {
b.external(editBundle).external(guessCaseBundle);
});
runYarb('statistics.js', function (b) {
b.external(commonBundle);
});
runYarb('timeline.js', function (b) {
b.external(commonBundle);
});
runYarb('url.js', function (b) {
b.external(editBundle);
});
runYarb('voting.js', function (b) {
b.external(commonBundle);
});
runYarb('work/index.js', function (b) {
b.external(commonBundle);
});
runYarb('work.js', function (b) {
b.external(editBundle).external(guessCaseBundle);
});
gulp.task('watch', ['default'], function () {
let watch = require('gulp-watch');
watch(path.resolve(STATIC_DIR, '**/*.less'), function () {
process.stdout.write('Rebuilding styles ... ');
writeResources(buildStyles()).done(function () {
process.stdout.write('done.\n');
});
});
function shouldRebuild(b, resourceName, file) {
switch (file.event) {
case 'add':
return true;
case 'change':
case 'unlink':
return b.has(file.path);
}
return false;
}
watch(path.resolve(SCRIPTS_DIR, '**/*.js'), function (file) {
const changed = {};
_.each(SCRIPT_BUNDLES, function (bundle, resourceName) {
if (shouldRebuild(bundle, resourceName, file)) {
changed[resourceName] = bundle;
}
});
if (!_.isEmpty(changed)) {
const changedNames = Object.keys(changed).sort().join(', ');
process.stdout.write(`Rebuilding ${changedNames} ... `);
writeResources(mergeStream.apply(null, _.map(changed, bundleScripts)))
.done(function () {
process.stdout.write('done.\n');
});
}
});
});
function createTestsTask(name, source) {
gulp.task(name, function () {
process.env.NODE_ENV = 'development';
var deferred = Q.defer();
bundleScripts(
runYarb(source)
.expose(path.join(BUILD_DIR, 'rev-manifest.json'), 'rev-manifest.json'),
name + '.js'
)
.pipe(gulp.dest(BUILD_DIR))
.on('finish', function () { deferred.resolve() });
return deferred.promise;
});
}
createTestsTask('tests', 'tests/browser-runner.js');
createTestsTask('web-tests', 'tests/index-web.js');
gulp.task('default', function () {
const IMAGE_GLOBS = [
'entity/*',
'icons/*',
'image404-125.png',
'layout/*',
'leaflet/*',
'licenses/*',
'logos/*',
];
return writeResources(mergeStream.apply(null, [
gulp.src(
IMAGE_GLOBS.map(x => path.join(IMAGES_DIR, x)),
{base: STATIC_DIR}
),
// The rev-manifest.js bundle can't be written until all other resources
// are written (because we obviously don't know its contents otherwise,
// which includes the final hashes of every resource). This is handled
// by writeResources.
_.map(_.omit(SCRIPT_BUNDLES, 'rev-manifest.js'), bundleScripts),
buildStyles()
]));
});