forked from wikimedia/wikimedia-portals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
466 lines (396 loc) · 14.2 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* jshint strict:false */
/* globals require */
/* globals process */
/* globals console */
/* globals JSON */
/* globals Buffer */
/* eslint dot-notation: ["error", { "allowKeywords": false }] */
var gulp = require( 'gulp' ),
gulpLoadPlugins = require( 'gulp-load-plugins' ),
argv = require( 'yargs' ).argv,
imagemin = require( 'gulp-imagemin' ),
imageminPngquant = require( 'imagemin-pngquant' ),
imageminZopfli = require( 'imagemin-zopfli' ),
siteStats = require( './data/site-stats' ),
fs = require( 'fs' ),
exec = require( 'child_process' ).exec,
sprity = require( 'sprity' ),
postCSSNext = require( 'postcss-cssnext' ),
postCSSImport = require( 'postcss-import' ),
postCSSReporter = require( 'postcss-reporter' ),
gulpStylelint = require( 'gulp-stylelint' ),
del = require( 'del' ),
plugins = gulpLoadPlugins(),
gulpSlash = require( 'gulp-slash' ),
portalParam = argv.portal,
getBaseDir, getProdDir, getConfig;
// Help
gulp.task( 'help', function () {
/* eslint-disable no-console */
console.log();
console.log( '+-------------------------------------------------------------------------------------------------+' );
console.log( '| ===== USAGE ===== |' );
console.log( '+-------------------------------------------------------------------------------------------------+' );
console.log( '| GLOBAL TASKS : |' );
console.log( '| |' );
console.log( '| gulp lint - run jslint on all JS files |' );
console.log( '| gulp update-stats - update file containing projects stats |' );
console.log( '+-------------------------------------------------------------------------------------------------+' );
console.log( '| PORTAL-SPECIFIC TASKS : |' );
console.log( '| |' );
console.log( '| gulp lint --portal wikipedia.org - run jslint on JS files on portal JS files |' );
console.log( '| gulp inline-assets --portal wikipedia.org - build inline CSS and JS assets |' );
console.log( '| gulp optimize-images --portal wikipedia.org - run imagemin on image directory |' );
console.log( '| gulp watch --portal wikipedia.org - watches dev directory and generates an index.html |' );
console.log( '| file in it without inlined/minified assets |' );
console.log( '| gulp --portal wikipedia.org - run all of the above on the specified portal page |' );
console.log( '| |' );
console.log( '| gulp fetch-meta --portal wikipedia.org - overwrite the portal page with source from Meta |' );
console.log( '| gulp update-urls-to-purge - creates the urls-to-purge.txt file to purge the server cache |' );
console.log( '+-------------------------------------------------------------------------------------------------+' );
console.log();
/* eslint-enable no-console */
} );
/* Preliminary configuration
=========================================================================== */
/**
* Preliminary task for tasks that require the portal param.
* Also sets config for remaining tasks.
*/
function requirePortalParam() {
if ( !portalParam ) {
/* eslint-disable no-console */
console.log( '\x1b[31m' );
console.log( 'Error: please specify the portal you wish to build.' );
console.log( 'Type gulp help for more information.' );
console.log( '\x1b[0m' );
/* eslint-enable no-console */
process.exit( 1 );
}
}
getBaseDir = function () {
requirePortalParam();
getBaseDir = function () {
return 'dev/' + portalParam + '/';
};
return getBaseDir();
};
getProdDir = function () {
requirePortalParam();
getProdDir = function () {
return 'prod/' + portalParam + '/';
};
return getProdDir();
};
getConfig = function () {
var config = {},
baseDir, prodDir, minifyCss;
baseDir = getBaseDir();
prodDir = getProdDir();
config.hb = {
src: baseDir + 'index.handlebars',
templateData: require( './' + baseDir + 'controller.js' ),
options: {
batch: [ './' + baseDir + '/templates' ],
helpers: require( './hbs-helpers.global' )
}
};
minifyCss = function () {
var options = {
compatibility: 'ie7',
keepSpecialComments: '0'
};
return plugins.cleanCss.call( this, options );
};
config.inline = {
src: baseDir + 'index.html',
options: {
base: baseDir,
css: minifyCss,
disabledTypes: [ 'svg', 'img', 'js' ]
}
};
config.htmlmin = {
src: prodDir + 'index.html',
dest: prodDir + 'index.html',
options: {
preventAttributesEscaping: true,
collapseWhitespace: true,
preserveLineBreaks: true,
collapseBooleanAttributes: false
}
};
config.watch = {
sprites: [ baseDir + 'assets/img/sprite_assets/**/*' ],
postcss: baseDir + 'assets/postcss/*.css',
hb: [ baseDir + '*.handlebars',
baseDir + '.json',
baseDir + 'controller.js',
baseDir + 'templates/**/*'
]
};
config.optImage = {
imageminConf: {
plugins: [
imagemin.svgo(),
imageminPngquant( { quality: '57-95', speed: 1 } ),
imagemin.optipng(),
imageminZopfli()
],
options: { verbose: true }
},
src: [ baseDir + 'assets/img/*', '!' + baseDir + 'assets/img/sprite_assets' ],
dest: prodDir + 'assets/img'
};
getConfig = function () {
return config;
};
return getConfig();
};
/* List of tasks
=========================================================================== */
/**
* Compiles Handlebars templates into dev folder.
* executes 'build' task if config is undefined
*/
gulp.task( 'compile-handlebars', function () {
requirePortalParam();
return gulp.src( getConfig().hb.src )
.pipe( plugins.compileHandlebars( getConfig().hb.templateData, getConfig().hb.options ) )
.pipe( plugins.rename( 'index.html' ) )
.pipe( gulp.dest( getBaseDir() ) );
} );
/**
* Compiles postCSS files into regular CSS and
* outputs them into the CSS dev folder.
*/
gulp.task( 'postcss', function () {
requirePortalParam();
return gulp.src( [ getBaseDir() + 'assets/postcss/*.css', '!' + getBaseDir() + 'assets/postcss/_*.css' ] )
.pipe( plugins.postcss( [
postCSSImport(),
postCSSNext( { browsers: [ 'last 5 versions', 'ie 6-8', 'Firefox >= 3.5', 'iOS >= 4', 'Android >= 2.3' ] } )
],
{ map: { inline: true } }
) )
.pipe( gulp.dest( getBaseDir() + 'assets/css/' ) );
} );
/**
* Inlines assets of index.html in dev folder,
* moves index.html into prod folder
*/
gulp.task( 'inline-assets', [ 'compile-handlebars', 'postcss' ], function () {
requirePortalParam();
return gulp.src( getConfig().inline.src )
.pipe( plugins.inline( getConfig().inline.options ) )
.pipe( gulp.dest( getProdDir() ) );
} );
/**
* Cleans `assets/js/` folder from the production folder.
*/
gulp.task( 'clean-prod-js', [ 'inline-assets' ], function () {
return del( [ getProdDir() + '/assets/js' ] );
} );
gulp.task( 'copy-translation-files', [ 'compile-handlebars' ], function () {
requirePortalParam();
del( getProdDir() + '/assets/l10n/**/*.json' );
return gulp.src( getBaseDir() + '/assets/l10n/**/*.json' )
.pipe( gulp.dest( getProdDir() + '/assets/l10n/' ) );
} );
/**
* Concatenates JS files into a single file and minifies it.
*/
gulp.task( 'concat-minify-js', [ 'clean-prod-js' ], function () {
requirePortalParam();
return gulp.src( getConfig().htmlmin.src )
.pipe( plugins.useref( { searchPath: getBaseDir() } ) )
.pipe( plugins[ 'if' ]( '*.js', plugins.uglify() ) )
.pipe( plugins[ 'if' ]( '*.js', plugins.rev() ) )
.pipe( plugins.revReplace() )
.pipe( gulp.dest( getProdDir() ) )
.pipe( plugins.rev.manifest() )
.pipe( gulp.dest( getBaseDir() + 'assets' ) );
} );
/**
* Minifies index.html file in prod folder,
* depends on inline-assets which moves index.html from dev to prod.
*/
gulp.task( 'minify-html', [ 'inline-assets', 'concat-minify-js' ], function () {
requirePortalParam();
return gulp.src( getConfig().htmlmin.src )
.pipe( plugins.htmlmin( getConfig().htmlmin.options ) )
.pipe( gulp.dest( getProdDir() ) );
} );
/**
* Optimizes images in dev folder and moves them into prod folder
*/
gulp.task( 'optimize-images', function () {
var imgOpt;
requirePortalParam();
imgOpt = getConfig().optImage;
return gulp.src( imgOpt.src )
.pipe( imagemin( imgOpt.imageminConf.plugins, imgOpt.imageminConf.options ) )
.pipe( gulp.dest( imgOpt.dest ) );
} );
/**
* Watches for changes in dev folder and compiles:
* - handlebars templates
* - postCSS files
* into dev folder.
*/
gulp.task( 'watch', [ 'compile-handlebars', 'sprite', 'postcss' ], function () {
requirePortalParam();
gulp.watch( getConfig().watch.hb, [ 'compile-handlebars' ] );
gulp.watch( getConfig().watch.sprites, [ 'sprite' ] );
gulp.watch( getConfig().watch.postcss, [ 'postcss' ] );
} );
/**
* Lints js in dev folder as well as in root folder.
*/
gulp.task( 'lint-js', function () {
var devFolder = 'dev/**/*.js';
if ( portalParam ) {
// only run on this portal files.
devFolder = 'dev/' + portalParam + '/**/*.js';
}
gulp.src( [ '*.js', devFolder ] )
.pipe( plugins.eslint( '.eslintrc.json' ) )
.pipe( plugins.eslint.format() )
.pipe( plugins.eslint.failAfterError() );
} );
gulp.task( 'validate-postCSS', function () {
return gulp
.src( [ 'dev/**/postcss/*.css', '!dev/**/postcss/_*.css' ] )
.pipe( plugins.postcss(
[
postCSSImport(),
postCSSNext( { browsers: [ 'last 5 versions', 'ie 6-8', 'Firefox >= 3.5', 'iOS >= 4', 'Android >= 2.3' ] } ),
postCSSReporter( { clearMessages: true, throwError: true } )
], { map: { inline: true } }
) );
} );
gulp.task( 'lint-css', [ 'validate-postCSS' ], function () {
return gulp
.src( 'dev/**/postcss/*.css' )
.pipe( gulpStylelint( {
reporters: [
{ formatter: 'string', console: true }
]
} ) );
} );
gulp.task( 'update-stats', function () {
siteStats.getSiteStats().then( function ( stats ) {
fs.writeFileSync( './data/site-stats.json', JSON.stringify( stats, null, '\t' ) );
} );
} );
gulp.task( 'fetch-meta', function () {
var portalsFromMeta;
requirePortalParam();
if ( portalParam === 'wikipedia.org' ) {
/* eslint-disable no-console */
console.log( 'Cannot override ' + portalParam + ' portal using fetch-meta.' );
/* eslint-enable no-console */
process.exit( 1 );
return;
}
if ( portalParam === 'all' ) {
portalsFromMeta = [ 'wikibooks.org', 'wikimedia.org', 'wikinews.org', 'wikiquote.org', 'wikiversity.org', 'wikivoyage.org', 'wiktionary.org' ];
portalsFromMeta.forEach( function ( wiki ) {
exec( ' curl -Lo prod/' + wiki + '/index.html "https://meta.wikimedia.org/w/index.php?title=Www.' + wiki + '_template&action=raw" ' );
} );
} else {
exec( ' curl -Lo ' + getProdDir() + 'index.html "https://meta.wikimedia.org/w/index.php?title=Www.' + portalParam + '_template&action=raw" ' );
}
} );
/**
* Creates a 'urls-to-purge.txt' file at the root of the repo that
* contains a list of URL's that must be purged from the server cache.
* These URLs include the root portal urls for all portals and all
* production asset URLs in this repo.
*
* Must be run when after all assets have been versioned, minified &
* copied into the production dir.
*/
gulp.task( 'update-urls-to-purge', [ 'compile-handlebars', 'sprite', 'postcss', 'inline-assets', 'clean-prod-js', 'concat-minify-js', 'minify-html', 'optimize-images', 'copy-translation-files' ], function() {
var UrlsToPurge = [
'https://www.wikibooks.org/',
'https://www.wikimedia.org/',
'https://www.wikinews.org/',
'https://www.wikipedia.org/',
'https://www.wikiquote.org/',
'https://www.wikiversity.org/',
'https://www.wikivoyage.org/',
'https://www.wiktionary.org/'
],
portalAssetDirs = 'prod/**/assets/**/*',
purgeFile = 'urls-to-purge.txt';
function createAssetUrl( file ) {
var domain, urlToPurge;
domain = file.relative.split( '/' )[ 0 ];
urlToPurge = 'https://www.' + domain + '/portal/' + file.relative;
return urlToPurge;
}
function addAssetUrl( url ) {
return UrlsToPurge.push( url );
}
function assetFilesStream( file ) {
var assetUrl;
if ( file.isDirectory() ) { return; }
assetUrl = createAssetUrl( file );
return addAssetUrl( assetUrl );
}
function writePurgeFile( UrlsToPurge ) {
var fileContents = UrlsToPurge.join( '\n' ),
fileBuffer = new Buffer( fileContents );
return fs.writeFile( purgeFile, fileBuffer );
}
return gulp.src( portalAssetDirs, { buffer: false, read: false } )
.pipe( gulpSlash() ) // Because windows slashes are '\' instead of '/'
.pipe( plugins.tap( assetFilesStream ) )
.on( 'end', function() {
writePurgeFile( UrlsToPurge );
} );
} );
/**
* Generates images sprites and accompanying CSS files using Sprity.
* Outputs sprites into dev assets/img folder.
* Outputs css into dev css/sprites.css file.
*
* Sprites are seperated into subfolders in img/sprite_assets.
* The contents of each folder will output a single sprite, named after the folder.
* Sprity will generate @2x, @1.5x, and @1x versions of the sprite.
*
* You should only supply and place the @2x versions of the assets into the sprite folders.
*/
gulp.task( 'sprite', function () {
requirePortalParam();
return sprity.src( {
src: getBaseDir() + 'assets/img/sprite_assets/**/*.{png,jpg}',
cssPath: 'portal/wikipedia.org/assets/img/',
style: getBaseDir() + 'assets/css/sprites.css',
prefix: 'sprite',
dimension: [ { ratio: 1, dpi: 72 },
{ ratio: 1.5, dpi: 144 },
{ ratio: 2, dpi: 192 }
],
split: true,
margin: 0,
cachebuster: true
} )
.pipe( plugins[ 'if' ]( '*.png', gulp.dest( getBaseDir() + 'assets/img/' ), gulp.dest( getBaseDir() + 'assets/css/' ) ) );
} );
gulp.task( 'lint', [ 'lint-js', 'lint-css' ] );
gulp.task( 'test', [ 'lint' ] );
gulp.task( 'default', [
'lint',
'compile-handlebars',
'sprite',
'postcss',
'inline-assets',
'clean-prod-js',
'concat-minify-js',
'minify-html',
'optimize-images',
'copy-translation-files',
'update-urls-to-purge'
] );